diff --git a/cimgen/languages/json_ld/langPack.py b/cimgen/languages/json_ld/langPack.py deleted file mode 100644 index bad246b..0000000 --- a/cimgen/languages/json_ld/langPack.py +++ /dev/null @@ -1,53 +0,0 @@ -import os -import chevron -import logging - -logger = logging.getLogger(__name__) - - -# This makes sure we have somewhere to write the generated files -def setup(version_path): - if not os.path.exists(version_path): - os.makedirs(version_path) - - -def location(version): - return "cimpy." + version + ".Base" - - -base = {"base_class": "Base", "class_location": location} - -template_files = [{"filename": "json_ld_template.mustache", "ext": ".json"}] - - -def get_class_location(class_name, class_map, version): - pass - - -def _set_default(text, render): - return "0.0" - - -def set_enum_classes(new_enum_classes): - return - - -def set_float_classes(new_float_classes): - return - - -def run_template(version_path, class_details): - for template_info in template_files: - class_file = os.path.join(version_path, class_details["class_name"] + template_info["ext"]) - if not os.path.exists(class_file): - with open(class_file, "w") as file: - template_path = os.path.join(os.getcwd(), "json_ld/templates", template_info["filename"]) - class_details["setDefault"] = _set_default - with open(template_path) as f: - args = {"data": class_details, "template": f} - output = chevron.render(**args) - file.write(output) - - -def resolve_headers(path): - pass diff --git a/cimgen/languages/json_ld/lang_pack.py b/cimgen/languages/json_ld/lang_pack.py new file mode 100644 index 0000000..2978db2 --- /dev/null +++ b/cimgen/languages/json_ld/lang_pack.py @@ -0,0 +1,86 @@ +from importlib.resources import files +import os +from typing import Callable +import chevron +import logging + +logger = logging.getLogger(__name__) + + +# This makes sure we have somewhere to write the generated files +def setup(output_path: str, cgmes_profile_details: list[dict], cim_namespace: str) -> None: + if not os.path.exists(output_path): + os.makedirs(output_path) + else: + for filename in os.listdir(output_path): + os.remove(os.path.join(output_path, filename)) + + +def get_base_class() -> str: + return "Base" + +# called by chevron, text contains the label {{datatype}}, which is evaluated by the renderer (see class template) +def _set_default(text: str, render: Callable[[str], str]) -> str: + result = render(text) + + # the field {{datatype}} either contains the multiplicity of an attribute if it is a reference or otherwise the + # datatype of the attribute. If no datatype is set and there is also no multiplicity entry for an attribute, the + # default value is set to None. The multiplicity is set for all attributes, but the datatype is only set for basic + # data types. If the data type entry for an attribute is missing, the attribute contains a reference and therefore + # the default value is either None or [] depending on the multiplicity. See also write_python_files + if result in ["M:1", "M:0..1", "M:1..1", ""]: + return "None" + elif result in ["M:0..n", "M:1..n"] or "M:" in result: + return "[]" + + result = result.split("#")[1] + if result in ["integer", "Integer"]: + return "0" + elif result in ["String", "DateTime", "Date"]: + return "''" + elif result == "Boolean": + return "False" + else: + # everything else should be a float + return "0.0" + + +# These are the template files that are used to generate the class files. +class_template_file = {"filename": "json_ld_template.mustache", "ext": ".json"} + + +def run_template(output_path: str, class_details: dict) -> None: + class_file = os.path.join(output_path, class_details["class_name"] + class_template_file["ext"]) + with open(class_file, "w") as file: + class_details["set_default"] = _set_default + templates = files("cimgen.languages.json_ld.templates") + with templates.joinpath(class_template_file["filename"]).open(encoding="utf-8") as f: + args = { + "data": class_details, + "template": f, + } + output = chevron.render(**args) + file.write(output) + + +def location(version): + return "cimpy." + version + ".Base" + +base = {"base_class": "Base", "class_location": location} + +template_files = [{"filename": "json_ld_template.mustache", "ext": ".json"}] + + +def get_class_location(class_name, class_map, version): + pass + + +def set_enum_classes(new_enum_classes): + return + + +def set_float_classes(new_float_classes): + return + +def resolve_headers(path: str, version: str) -> None: # NOSONAR + pass diff --git a/cimgen/languages/json_ld/templates/json_ld_template.mustache b/cimgen/languages/json_ld/templates/json_ld_template.mustache index 8a3bea9..860f233 100644 --- a/cimgen/languages/json_ld/templates/json_ld_template.mustache +++ b/cimgen/languages/json_ld/templates/json_ld_template.mustache @@ -1,33 +1,18 @@ { "$schema": "http://json-schema.org/schema#", + "$id": "schema:{{class_name}}", + "$ref": "schema:{{subclass_of}}", "$schemaVersion": "0.0.1", "modelTags": "", - "title": "Smart Data Models - {{class_name}}", + "title": "{{class_name}}", "description": "{{class_comment}}", "type": "object", - "allOf": [ - { - "$ref": "https://smart-data-models.github.io/data-models/common-schema.json#/definitions/GSMA-Commons" - }, - { - "$ref": "https://smart-data-models.github.io/data-models/common-schema.json#/definitions/Location-Commons" - }, - { - "properties": { - "type": { - "type": "string", - "enum": [ - "{{class_name}}" - ], - "description": "Property. NGSI type. It has to be {{class_name}}" - }, + "properties": { {{#attributes}} - "{{label}}": { - "description": "Property. Model:'https://schema.org/Number'. {{comment}} Default: {{#setDefault}}{{dataType}}{{/setDefault}}", - "type": "number" - }, + "{{label}}": { + "description": "{{comment}} Default: {{#set_default}}{{datatype}}{{/set_default}}", + "type": "{{datatype}}" + }, {{/attributes}} - } - } - ] + } }