Skip to content

Commit 5ff2fc1

Browse files
committed
using jinja2 instead of two files
1 parent 348d4a7 commit 5ff2fc1

File tree

4 files changed

+17
-78
lines changed

4 files changed

+17
-78
lines changed

pipeline/src/module_template.py.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class {{ class_name }}({{ base_class }}):
1616
"""
1717
type_ = "{{ openminds_type }}"
1818
context = {
19-
"@vocab": "https://openminds.om-i.org/props/"
19+
"@vocab": "{{ context_vocab }}"
2020
}
2121
schema_version = "{{ schema_version }}"
2222

pipeline/src/module_template_old.py.txt

-64
This file was deleted.

pipeline/tests/test_regressions.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_issue_0003():
3838
)
3939
# on export, a single item should be wrapped in a list, where the property expects an array
4040
expected = {
41-
"@context": {"@vocab": "https://openminds.om-i.org/vocab/"},
41+
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
4242
"@type": "https://openminds.om-i.org/types/FileArchive",
4343
"IRI": "http://example.com/archive.zip",
4444
"format": {
@@ -90,7 +90,7 @@ def test_issue0007():
9090

9191
actual = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=False, with_context=True)
9292
expected = {
93-
"@context": {"@vocab": "https://openminds.om-i.org/vocab/"},
93+
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
9494
"@id": "_:001",
9595
"@type": "https://openminds.om-i.org/types/Person",
9696
"familyName": "Professor",
@@ -120,7 +120,7 @@ def test_issue0007():
120120
saved_data = json.load(fp)
121121
os.remove("issue0007.jsonld")
122122
expected_saved_data = {
123-
"@context": {"@vocab": "https://openminds.om-i.org/vocab/"},
123+
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
124124
"@graph": [
125125
{
126126
"@id": "_:001",
@@ -171,7 +171,7 @@ def test_issue0008():
171171
)
172172
actual = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=False, with_context=True)
173173
expected = {
174-
"@context": {"@vocab": "https://openminds.om-i.org/vocab/"},
174+
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
175175
"@id": "_:002",
176176
"@type": "https://openminds.om-i.org/types/Person",
177177
"affiliation": [

pipeline/translator.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ def __init__(self, schema_file_path: str, root_path: str, instances: Optional[di
6464
schema_file_path[len(root_path) + 1 :].replace(".schema.omi.json", "").split("/")
6565
)
6666
self.version = _relative_path_without_extension[0]
67+
self.template_name = "src/module_template.py.txt"
6768
if self.version in ["v3.0" , "v2.0" , "v1.0"]:
68-
self.template_name = "src/module_template_old.py.txt"
69+
self.context_vocab = "https://openminds.ebrains.eu/vocab/"
6970
else:
70-
self.template_name = "src/module_template.py.txt"
71+
self.context_vocab = "https://openminds.om-i.org/props/"
72+
7173
self.relative_path_without_extension = [
7274
generate_python_name(part) for part in _relative_path_without_extension[1:]
7375
]
@@ -86,7 +88,7 @@ def _version_module(self):
8688
def _target_file_without_extension(self) -> str:
8789
return os.path.join(self._version_module, "/".join(self.relative_path_without_extension))
8890

89-
def translate(self, embedded=None, class_module_dict=None):
91+
def translate(self, embedded=None, class_to_module_map=None):
9092
def get_type(property):
9193
type_map = {
9294
"string": "str",
@@ -104,8 +106,8 @@ def get_type(property):
104106
types = []
105107
for item in property["_linkedTypes"]:
106108
openminds_module_from_type, class_name = item.split("/")[-2:]
107-
if isinstance(class_module_dict,dict) and (class_name in class_module_dict):
108-
openminds_module = generate_python_name(class_module_dict[class_name])
109+
if isinstance(class_to_module_map,dict) and (class_name in class_to_module_map):
110+
openminds_module = generate_python_name(class_to_module_map[class_name])
109111
else:
110112
openminds_module = generate_python_name(openminds_module_from_type)
111113
types.append(f"openminds.{self._version_module}.{openminds_module}.{class_name}")
@@ -116,8 +118,8 @@ def get_type(property):
116118
types = []
117119
for item in property["_embeddedTypes"]:
118120
openminds_module_from_type, class_name = item.split("/")[-2:]
119-
if isinstance(class_module_dict,dict) and (class_name in class_module_dict):
120-
openminds_module = generate_python_name(class_module_dict[class_name])
121+
if isinstance(class_to_module_map,dict) and (class_name in class_to_module_map):
122+
openminds_module = generate_python_name(class_to_module_map[class_name])
121123
else:
122124
openminds_module = generate_python_name(openminds_module_from_type)
123125
types.append(f"openminds.{self._version_module}.{openminds_module}.{class_name}")
@@ -210,6 +212,7 @@ def filter_instance(instance):
210212
"class_name": class_name,
211213
"openminds_type": openminds_type,
212214
"schema_version": self.version,
215+
"context_vocab": self.context_vocab,
213216
"properties": properties,
214217
"additional_methods": "",
215218
"instances": instances
@@ -242,11 +245,11 @@ def filter_instance(instance):
242245
if extra_imports:
243246
self.context["preamble"] = "\n".join(sorted(extra_imports))
244247

245-
def build(self, embedded=None, class_module_dict=None):
248+
def build(self, embedded=None, class_to_module_map=None):
246249
target_file_path = os.path.join("target", "openminds", f"{self._target_file_without_extension()}.py")
247250
os.makedirs(os.path.dirname(target_file_path), exist_ok=True)
248251

249-
self.translate(embedded=embedded, class_module_dict=class_module_dict)
252+
self.translate(embedded=embedded, class_to_module_map=class_to_module_map)
250253

251254
with open(target_file_path, "w") as target_file:
252255
contents = self.env.get_template(self.template_name).render(self.context)

0 commit comments

Comments
 (0)