Skip to content

Commit a8df009

Browse files
committed
adopting to namespace
1 parent f0ed26f commit a8df009

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

build.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
instances[version][instance_data["@type"]].append(instance_data)
4848

4949
python_modules = defaultdict(list)
50+
51+
class_module_dict={}
52+
5053
for schema_version in schema_loader.get_schema_versions():
5154

5255
# Step 3 - find all involved schemas for the current version
@@ -55,8 +58,10 @@
5558
# Step 4a - figure out which schemas are embedded and which are linked
5659
embedded = set()
5760
linked = set()
61+
class_module_dict={}
5862
for schema_file_path in schemas_file_paths:
5963
emb, lnk = PythonBuilder(schema_file_path, schema_loader.schemas_sources).get_edges()
64+
class_module_dict=PythonBuilder(schema_file_path, schema_loader.schemas_sources).get_module_dict(class_module_dict)
6065
embedded.update(emb)
6166
linked.update(lnk)
6267
conflicts = linked.intersection(embedded)
@@ -76,7 +81,7 @@
7681
schema_loader.schemas_sources,
7782
instances=instances.get(schema_version, None),
7883
additional_methods=additional_methods,
79-
).build(embedded=embedded)
84+
).build(embedded=embedded,class_module_dict=class_module_dict)
8085

8186
parts = module_path.split(".")
8287
parent_path = ".".join(parts[:-1])

pipeline/translator.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def generate_python_name(json_name, allow_multiple=False):
2525
python_name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", json_name.strip())
2626
python_name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", python_name).lower()
2727
replacements = [
28-
("-", "_"), (".", "_"), ("+", "plus"), ("#", "sharp"), (",", "comma"), ("(", ""), (")", "")
28+
("-", "_"), (".", "_"),("'","_prime_"), ("+", "plus"), ("#", "sharp"), (",", "comma"), ("(", ""), (")", "")
2929
]
3030
for before, after in replacements:
3131
python_name = python_name.replace(before, after)
@@ -83,7 +83,7 @@ def _version_module(self):
8383
def _target_file_without_extension(self) -> str:
8484
return os.path.join(self._version_module, "/".join(self.relative_path_without_extension))
8585

86-
def translate(self, embedded=None):
86+
def translate(self, embedded=None, class_module_dict=None):
8787
def get_type(property):
8888
type_map = {
8989
"string": "str",
@@ -100,17 +100,23 @@ def get_type(property):
100100
if "_linkedTypes" in property:
101101
types = []
102102
for item in property["_linkedTypes"]:
103-
openminds_module, class_name = item.split("/")[-2:]
104-
openminds_module = generate_python_name(openminds_module)
103+
openminds_module_from_type, class_name = item.split("/")[-2:]
104+
if isinstance(class_module_dict,dict) and (class_name in class_module_dict):
105+
openminds_module = generate_python_name(class_module_dict[class_name])
106+
else:
107+
openminds_module = generate_python_name(openminds_module_from_type)
105108
types.append(f"openminds.{self._version_module}.{openminds_module}.{class_name}")
106109
if len(types) == 1:
107110
types = f'"{types[0]}"'
108111
return types
109112
elif "_embeddedTypes" in property:
110113
types = []
111114
for item in property["_embeddedTypes"]:
112-
openminds_module, class_name = item.split("/")[-2:]
113-
openminds_module = generate_python_name(openminds_module)
115+
openminds_module_from_type, class_name = item.split("/")[-2:]
116+
if isinstance(class_module_dict,dict) and (class_name in class_module_dict):
117+
openminds_module = generate_python_name(class_module_dict[class_name])
118+
else:
119+
openminds_module = generate_python_name(openminds_module_from_type)
114120
types.append(f"openminds.{self._version_module}.{openminds_module}.{class_name}")
115121
if len(types) == 1:
116122
types = f'"{types[0]}"'
@@ -233,11 +239,11 @@ def filter_instance(instance):
233239
if extra_imports:
234240
self.context["preamble"] = "\n".join(sorted(extra_imports))
235241

236-
def build(self, embedded=None):
242+
def build(self, embedded=None, class_module_dict=None):
237243
target_file_path = os.path.join("target", "openminds", f"{self._target_file_without_extension()}.py")
238244
os.makedirs(os.path.dirname(target_file_path), exist_ok=True)
239245

240-
self.translate(embedded=embedded)
246+
self.translate(embedded=embedded, class_module_dict=class_module_dict)
241247

242248
with open(target_file_path, "w") as target_file:
243249
contents = self.env.get_template(self.template_name).render(self.context)
@@ -252,3 +258,16 @@ def get_edges(self):
252258
embedded.update(property.get("_embeddedTypes", []))
253259
linked.update(property.get("_linkedTypes", []))
254260
return embedded, linked
261+
262+
def get_module_dict(self,class_module_dict):
263+
264+
schema_type=self._schema_payload["_type"]
265+
class_name=schema_type.split("/")[-1]
266+
if "_module" in self._schema_payload:
267+
module=self._schema_payload["_module"]
268+
else:
269+
module=schema_type.split("/")[-2]
270+
271+
class_module_dict[class_name]=module
272+
273+
return class_module_dict

0 commit comments

Comments
 (0)