caosadvancedtools.models.parser module#
This module (and script) provides methods to read a DataModel from a YAML file.
If a file name is passed to parse_model_from_yaml it is parsed and a DataModel is created. The yaml file needs to be structured in a certain way which will be described in the following.
The file should only contain a dictionary. The keys are the names of RecordTypes or Properties. The values are again dictionaries describing the entities. This information can be defined via the keys listed in KEYWORDS. Notably, properties can be given in a dictionary under the xxxx_properties keys and will be added with the respective importance. These properties can be RecordTypes or Properties and can be defined right there. Every Property or RecordType only needs to be defined once anywhere. When it is not defined, simply the name can be supplied with no value. Parents can be provided under the ‘inherit_from_xxxx’ keywords. The value needs to be a list with the names. Here, NO NEW entities can be defined.
- exception caosadvancedtools.models.parser.JsonSchemaDefinitionError(msg)#
Bases:
RuntimeError
- class caosadvancedtools.models.parser.JsonSchemaParser(types_for_missing_array_items=None, ignore_unspecified_array_items=False)#
Bases:
ParserExtends the yaml parser to read in datamodels defined in a json schema.
EXPERIMENTAL: While this class can already be used to create data models from basic json schemas, there are the following limitations and missing features:
Due to limitations of json-schema itself, we currently do not support inheritance in the imported data models
The same goes for suggested properties of RecordTypes
Already defined RecordTypes and (scalar) Properties can’t be re-used as list properties
Reference properties that are different from the referenced RT. (Although this is possible for list of references)
Values
Roles
The extern keyword from the yaml parser
- parse_model_from_json_schema(filename: str, top_level_recordtype: bool = True)#
Return a datamodel created from the definition in the json schema in filename.
- Parameters:
filename (str) – The path to the json-schema file containing the datamodel definition
top_level_recordtype (bool, optional) – Whether there is a record type defined at the top level of the schema. Default is true.
- Returns:
out – The created DataModel
- Return type:
- class caosadvancedtools.models.parser.Parser(debug: bool = False)#
Bases:
object- parse_model_from_string(string, existing_model: dict | None = None) DataModel#
Create and return a data model from the given YAML string.
- Parameters:
string (str) – The YAML string.
existing_model (dict, optional) – An existing model to which the created model shall be added.
- Returns:
out – The created DataModel
- Return type:
- parse_model_from_yaml(filename, existing_model: dict | None = None, add_enums: bool = False) DataModel#
Create and return a data model from the given file.
- Parameters:
filename (str) – The path to the YAML file.
existing_model (dict, optional) – An existing model to which the created model shall be added.
add_enums (bool, default=False) –
If True, add enums to the result. This enables
enum-nameselements which contain arrays of enum names, so that this keyword is recognized by the parser.For each of the values in this array, a simple Record is created, with the RecordType as parent and the value as the name. These records can conveniently be used as enum like references. The values (and thus enum names) do not have to be unique across RecordTypes: for example there may be
Otherenum values for different RecordTypes.For more information, refer to https://docs.linkahead.org/.
- Returns:
out – The created DataModel
- Return type:
- class caosadvancedtools.models.parser.SafeLineLoader(stream)#
Bases:
SafeLoaderLoad a line and keep meta-information.
Note that this will add a __line__ element to all the dicts.
- construct_mapping(node, deep=False)#
Overwritung the parent method.
- exception caosadvancedtools.models.parser.TwiceDefinedException(name)#
Bases:
Exception
- exception caosadvancedtools.models.parser.YamlDefinitionError(line, template=None)#
Bases:
RuntimeError
- caosadvancedtools.models.parser.main()#
- caosadvancedtools.models.parser.parse_model_from_json_schema(filename: str, top_level_recordtype: bool = True, types_for_missing_array_items: dict | None = None, ignore_unspecified_array_items: bool = False, existing_model: dict | None = None) DataModel#
Return a datamodel parsed from a json schema definition.
- Parameters:
filename (str) – The path of the json schema file that is to be parsed
top_level_recordtype (bool, optional) – Whether there is a record type defined at the top level of the schema. Default is true.
types_for_missing_array_items (dict, optional) – dictionary containing fall-back types for json entries with type: array but without items specification. Default is an empty dict.
ignore_unspecified_array_items (bool, optional) – Whether to ignore type: array entries the type of which is not specified by their items property or given in types_for_missing_array_items. An error is raised if they are not ignored. Default is False.
existing_model (dict, optional) – An existing model to which the created model shall be added. Not fully implemented yet.
- Returns:
out – The datamodel generated from the input schema which then can be used for synchronizing with LinkAhead.
- Return type:
Datamodel
Note
This is an experimental feature, see
JsonSchemaParserfor information about the limitations of the current implementation.
- caosadvancedtools.models.parser.parse_model_from_string(string, existing_model: dict | None = None, debug: bool = False) DataModel#
Parse a data model from a YAML string
This is a convenience function if the Parser object is not needed, it calls
Parser.parse_model_from_string(...)internally.- 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.
- caosadvancedtools.models.parser.parse_model_from_yaml(filename, existing_model: dict | None = None, debug: bool = False, add_enums: bool = False) DataModel#
Parse a data model from a YAML file.
This is a convenience function if the Parser object is not needed, it calls
Parser.parse_model_from_yaml(...)internally.- 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.
add_enums (bool, default=False) – If True, add enums to the result. This enables
enum-nameselements, so that this keyword is recognized by the parser. Refer to https://docs.linkahead.org/ for more information.