diff --git a/src/caosadvancedtools/models/parser.py b/src/caosadvancedtools/models/parser.py index 434535d5b25d5d806fdc05e9edede705fc86b5a1..60021a8048af3c2f8d26409f3a278fbe8762f989 100644 --- a/src/caosadvancedtools/models/parser.py +++ b/src/caosadvancedtools/models/parser.py @@ -140,18 +140,18 @@ class JsonSchemaDefinitionError(RuntimeError): super().__init__(msg) -def parse_model_from_yaml(filename): +def parse_model_from_yaml(filename, existing_model=None): """Shortcut if the Parser object is not needed.""" parser = Parser() - return parser.parse_model_from_yaml(filename) + return parser.parse_model_from_yaml(filename, existing_model=existing_model) -def parse_model_from_string(string): +def parse_model_from_string(string, existing_model=None): """Shortcut if the Parser object is not needed.""" parser = Parser() - return parser.parse_model_from_string(string) + return parser.parse_model_from_string(string, existing_model=existing_model) def parse_model_from_json_schema( @@ -207,7 +207,7 @@ class Parser(object): self.model = {} self.treated = [] - def parse_model_from_yaml(self, filename): + def parse_model_from_yaml(self, filename, existing_model=None): """Create and return a data model from the given file. Parameters @@ -223,9 +223,9 @@ class Parser(object): with open(filename, 'r') as outfile: ymlmodel = yaml.load(outfile, Loader=SafeLineLoader) - return self._create_model_from_dict(ymlmodel) + return self._create_model_from_dict(ymlmodel, existing_model=existing_model) - def parse_model_from_string(self, string): + def parse_model_from_string(self, string, existing_model=None): """Create and return a data model from the given YAML string. Parameters @@ -240,9 +240,9 @@ class Parser(object): """ ymlmodel = yaml.load(string, Loader=SafeLineLoader) - return self._create_model_from_dict(ymlmodel) + return self._create_model_from_dict(ymlmodel, existing_model=existing_model) - def _create_model_from_dict(self, ymlmodel): + def _create_model_from_dict(self, ymlmodel, existing_model=None): """Create and return a data model out of the YAML dict `ymlmodel`. Parameters @@ -250,6 +250,9 @@ class Parser(object): ymlmodel : dict The dictionary parsed from a YAML file. + existing_model : dict, optional + An existing model to which the ymlmodel shall be added. + Returns ------- out : DataModel @@ -259,6 +262,9 @@ class Parser(object): if not isinstance(ymlmodel, dict): raise ValueError("Yaml file should only contain one dictionary!") + if existing_model is not None: + self.model.update(existing_model) + # Extern keyword: # The extern keyword can be used to include Properties and RecordTypes # from existing CaosDB datamodels into the current model.