Commit 23a9a5ad authored by Noé Gaumont's avatar Noé Gaumont 🐙
Browse files

feat: generate postcreate.py

parent e26019e0a636
......@@ -16,6 +16,8 @@
register_base_types,
)
from owl2yams.generate_postcreate import generate_postcreate
LITERAL_TYPES_TO_YAMS_TYPES = {
RDFS.Literal: "String",
......@@ -269,6 +271,14 @@
with open(f"{cube_master_folder}/{cube_subfolder}/schema.py", "a") as f:
f.write(serialize_to_python(schema))
with open(
f"{cube_master_folder}/{cube_subfolder}/migration/postcreate.py", "a"
) as f:
f.write(
generate_postcreate(
entitytype_fragment_to_uri, relationtype_fragment_to_uri
)
)
if args.only_cube:
print("Cube correctly initialized")
......
from typing import Dict
import jinja2
postcreate_template = jinja2.Template(
"""
yams_schema_mapping = {{ mapping }}
for name, uri, entity_type in yams_schema_mapping:
equivalent_class = find(entity_type,name=name).one()
rset = find('ExternalUri', uri=uri)
if len(rset) == 1:
rset.one().cw_set(reverse_equivalent_class=equivalent_class)
elif len(rset) == 0:
create_entity('ExternalUri', reverse_equivalent_class=equivalent_class, uri=uri)
commit()
"""
)
def generate_postcreate(
entitytype_to_uri: Dict[str, str], relationtype_to_uri: Dict[str, str]
):
mapping = []
for yams_class_name, uri_with_namespace in entitytype_to_uri.items():
mapping.append((yams_class_name, str(uri_with_namespace), "CWEType"))
for yams_relation_name, uri_with_namespace in relationtype_to_uri.items():
mapping.append((yams_relation_name, str(uri_with_namespace), "CWRelation"))
return postcreate_template.render(mapping=mapping)
......@@ -17,6 +17,7 @@
"cubicweb": ">=3.30",
"yams": None,
"rdflib": None,
"Jinja2": None,
}
install_requires = ["{0} {1}".format(d, v or "").strip() for d, v in requires.items()]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment