Skip to content
Snippets Groups Projects
Verified Commit 87773e8f authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: Debugging for yaml parser: Add __line__ to datamodel.

parent 92721f11
No related branches found
No related tags found
2 merge requests!89ENH: JsonSchemaExporter accepts do_not_create parameter.,!88jsex / yaml parser
Pipeline #43864 passed with warnings
...@@ -140,7 +140,7 @@ class JsonSchemaDefinitionError(RuntimeError): ...@@ -140,7 +140,7 @@ class JsonSchemaDefinitionError(RuntimeError):
super().__init__(msg) 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. """Parse a data model from a YAML file.
This is a convenience function if the Parser object is not needed, it calls This is a convenience function if the Parser object is not needed, it calls
...@@ -152,13 +152,16 @@ Parameters ...@@ -152,13 +152,16 @@ Parameters
existing_model : dict, optional existing_model : dict, optional
An existing model to which the created model shall be added. 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) 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 """Parse a data model from a YAML string
This is a convenience function if the Parser object is not needed, it calls This is a convenience function if the Parser object is not needed, it calls
...@@ -169,8 +172,11 @@ Parameters ...@@ -169,8 +172,11 @@ Parameters
existing_model : dict, optional existing_model : dict, optional
An existing model to which the created model shall be added. 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) return parser.parse_model_from_string(string, existing_model=existing_model)
...@@ -232,13 +238,20 @@ def parse_model_from_json_schema( ...@@ -232,13 +238,20 @@ def parse_model_from_json_schema(
class Parser(object): 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 """Initialize an empty parser object and initialize the dictionary of entities and the list of
treated elements. treated elements.
Parameters
----------
debug : bool, optional
If True, turn on miscellaneous debugging. Default is False.
""" """
self.model = {} self.model = {}
self.treated = [] self.treated = []
self.debug = debug
def parse_model_from_yaml(self, filename, existing_model: Optional[dict] = None): def parse_model_from_yaml(self, filename, existing_model: Optional[dict] = None):
"""Create and return a data model from the given file. """Create and return a data model from the given file.
...@@ -450,6 +463,9 @@ class Parser(object): ...@@ -450,6 +463,9 @@ class Parser(object):
raise YamlDefinitionError(line) from None raise YamlDefinitionError(line) from None
raise 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): def _add_to_recordtype(self, ent_name, props, importance):
"""Add properties to a RecordType. """Add properties to a RecordType.
...@@ -624,9 +640,13 @@ class Parser(object): ...@@ -624,9 +640,13 @@ class Parser(object):
dtype = value.datatype dtype = value.datatype
is_list = False is_list = False
if _get_listdatatype(value.datatype) is not None: try:
dtype = _get_listdatatype(value.datatype) if _get_listdatatype(value.datatype) is not None:
is_list = True 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 dtype in self.model:
if is_list: if is_list:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment