From 87773e8fdd1f38b2323bf8d120b4855d50b51667 Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Fri, 17 Nov 2023 11:21:48 +0100 Subject: [PATCH] ENH: Debugging for yaml parser: Add __line__ to datamodel. --- src/caosadvancedtools/models/parser.py | 36 ++++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/caosadvancedtools/models/parser.py b/src/caosadvancedtools/models/parser.py index ba63c5cd..579ece11 100644 --- a/src/caosadvancedtools/models/parser.py +++ b/src/caosadvancedtools/models/parser.py @@ -140,7 +140,7 @@ class JsonSchemaDefinitionError(RuntimeError): super().__init__(msg) -def parse_model_from_yaml(filename, existing_model: Optional[dict] = None): +def parse_model_from_yaml(filename, existing_model: Optional[dict] = None, debug: bool = False): """Parse a data model from a YAML file. This is a convenience function if the Parser object is not needed, it calls @@ -152,13 +152,16 @@ Parameters existing_model : dict, optional An existing model to which the created model shall be added. + +debug : bool, optional + If True, turn on miscellaneous debugging. Default is False. """ - parser = Parser() + parser = Parser(debug=debug) return parser.parse_model_from_yaml(filename, existing_model=existing_model) -def parse_model_from_string(string, existing_model: Optional[dict] = None): +def parse_model_from_string(string, existing_model: Optional[dict] = None, debug: bool = False): """Parse a data model from a YAML string This is a convenience function if the Parser object is not needed, it calls @@ -169,8 +172,11 @@ Parameters existing_model : dict, optional An existing model to which the created model shall be added. + +debug : bool, optional + If True, turn on miscellaneous debugging. Default is False. """ - parser = Parser() + parser = Parser(debug=debug) return parser.parse_model_from_string(string, existing_model=existing_model) @@ -232,13 +238,20 @@ def parse_model_from_json_schema( class Parser(object): - def __init__(self): + def __init__(self, debug: bool = False): """Initialize an empty parser object and initialize the dictionary of entities and the list of treated elements. +Parameters +---------- + +debug : bool, optional + If True, turn on miscellaneous debugging. Default is False. + """ self.model = {} self.treated = [] + self.debug = debug def parse_model_from_yaml(self, filename, existing_model: Optional[dict] = None): """Create and return a data model from the given file. @@ -450,6 +463,9 @@ class Parser(object): raise YamlDefinitionError(line) from None raise + if self.debug and self.model[name] is not None: + self.model[name].__line__ = definition["__line__"] + def _add_to_recordtype(self, ent_name, props, importance): """Add properties to a RecordType. @@ -624,9 +640,13 @@ class Parser(object): dtype = value.datatype is_list = False - if _get_listdatatype(value.datatype) is not None: - dtype = _get_listdatatype(value.datatype) - is_list = True + try: + if _get_listdatatype(value.datatype) is not None: + dtype = _get_listdatatype(value.datatype) + is_list = True + except TypeError as err: + err.args = (*err.args, f"yaml line: {value.__dict__.get('__line__')}") + raise(err) if dtype in self.model: if is_list: -- GitLab