Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json ld specification #11

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
89 changes: 89 additions & 0 deletions cimgen/languages/json_ld/lang_pack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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
18 changes: 18 additions & 0 deletions cimgen/languages/json_ld/templates/json_ld_template.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/schema#",
"$id": "schema:{{class_name}}",
"$ref": "schema:{{subclass_of}}",
"$schemaVersion": "0.0.1",
"modelTags": "",
"title": "{{class_name}}",
"description": "{{class_comment}}",
"type": "object",
"properties": {
{{#attributes}}
"{{label}}": {
"description": "{{comment}} Default: {{#set_default}}{{datatype}}{{/set_default}}",
"type": "{{datatype}}"
},
{{/attributes}}
}
}
Loading