Skip to content
Snippets Groups Projects

Add to existing model

Merged Daniel Hornung requested to merge f-add-to-existing-model into dev
1 file
+ 15
9
Compare changes
  • Side-by-side
  • Inline
@@ -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.
Loading