Skip to content

Commit

Permalink
Merge branch 'sqlalchemy' of https://github.com/zaphiro-technologies/…
Browse files Browse the repository at this point in the history
…cimgen into sqlalchemy
  • Loading branch information
chicco785 committed Aug 9, 2023
2 parents bb65697 + 25665ec commit cd87a01
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 108 deletions.
28 changes: 18 additions & 10 deletions CIMgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def addOrigin(self, origin):

def superClass(self):
return self.super

def rootClass(self):
return self.root

Expand Down Expand Up @@ -704,19 +704,23 @@ def addSubClassesOfSubClassesClean(class_dict, source):
addSubClassesOfSubClassesClean(temp, source)
class_dict.update(temp)


def addRootClassOfClean(class_dict):
temp = {}
for className in class_dict:
if class_dict[className].super:
temp[className] = class_dict[className]
temp[className].setRootClass(findRootClass(class_dict[className].super,class_dict))
temp[className].setRootClass(
findRootClass(class_dict[className].super, class_dict)
)
class_dict.update(temp)


def findRootClass(superClassName, class_dict):
for className in class_dict:
if className == superClassName:
if class_dict[className].super:
return findRootClass(class_dict[className].super,class_dict)
return findRootClass(class_dict[className].super, class_dict)
else:
return className
return None
Expand All @@ -728,21 +732,25 @@ def addInverseMultiplicity(class_dict):
temp[className] = class_dict[className]
to_update = False
for attribute in _find_multiple_attributes(temp[className].attributes()):
if 'inverseRole' in attribute:
if "inverseRole" in attribute:
to_update = True
attribute['inverseMultiplicity'] = findInverseMultiplicity(attribute['inverseRole'], class_dict)
attribute["inverseMultiplicity"] = findInverseMultiplicity(
attribute["inverseRole"], class_dict
)
if not to_update:
del temp[className]
class_dict.update(temp)


def findInverseMultiplicity(inverseRole, class_dict):
className = inverseRole.split('.')[0]
attributeLabel = inverseRole.split('.')[1]
for attribute in _find_multiple_attributes(class_dict[className].attributes()):
if attribute['label'] == attributeLabel:
return attribute['multiplicity']
className = inverseRole.split(".")[0]
attributeLabel = inverseRole.split(".")[1]
for attribute in _find_multiple_attributes(class_dict[className].attributes()):
if attribute["label"] == attributeLabel:
return attribute["multiplicity"]
return None


def cim_generate(directory, outputPath, version, langPack):
"""Generates cgmes python classes from cgmes ontology
Expand Down
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# cimgen Release Notes

## 0.0.1-dev - 2023-08-03
## 0.0.1-dev - 2023-08-09

### Features

- Support sql alchemy code generation (PR #8 by @chicco785)
- Support pydantic code generation (PR #5 by @chicco785)

### Continuous Integration
Expand Down
3 changes: 1 addition & 2 deletions pydantic/Base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@


class Base(BaseModel):

model_config = ConfigDict(populate_by_name=True,defer_build=True)
model_config = ConfigDict(populate_by_name=True, defer_build=True)
"""
Base Class for CIM
"""
Expand Down
18 changes: 10 additions & 8 deletions pydantic/langPack.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ def _set_instances(text, render):


def _lower_case_first_char(str):
return str[:1].lower() + str[1:] if str else ''
return str[:1].lower() + str[1:] if str else ""


def _set_lower_case(text, render):
return _lower_case_first_char(render(text))


# called by chevron, text contains the label {{dataType}}, which is evaluated by the renderer (see class template)
def _set_attribute(text, render):
attribute = eval(render(text))
Expand All @@ -99,19 +101,19 @@ def _set_default(attribute):
if multiplicity in ["M:1"] and multiplicity_by_name:
# Most probably there is a bug in the RDF that states multiplicity
# M:1 but should be M:1..N
return ' = Field(default=[], alias="'+attribute["label"]+'")'
return ' = Field(default=[], alias="' + attribute["label"] + '")'
if multiplicity in ["M:1", "M:1..1"]:
return ' = Field(alias="'+attribute["label"]+'")'
return ' = Field(alias="' + attribute["label"] + '")'
if multiplicity in ["M:0..1"]:
return ' = Field(default=None, alias="'+attribute["label"]+'")'
return ' = Field(default=None, alias="' + attribute["label"] + '")'
elif multiplicity in ["M:0..n"] or "M:0.." in multiplicity:
return ' = Field(default=[], alias="'+attribute["label"]+'")'
return ' = Field(default=[], alias="' + attribute["label"] + '")'
elif multiplicity in ["M:1..n"] or "M:1.." in multiplicity:
return ' = Field(default=[], alias="'+attribute["label"]+'")'
return ' = Field(default=[], alias="' + attribute["label"] + '")'
else:
return ' = Field(default=[], alias="'+attribute["label"]+'")'
return ' = Field(default=[], alias="' + attribute["label"] + '")'
else:
return ' = Field(alias="'+attribute["label"]+'")'
return ' = Field(alias="' + attribute["label"] + '")'


def _is_primitive(datatype):
Expand Down
2 changes: 1 addition & 1 deletion pydantic/schema_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PositionPoint(Base):
}
)

location: "Location" = Field(alias = "Location")
location: "Location" = Field(alias="Location")
sequenceNumber: Optional[int] = Field(default=None)
point: Point = Field(
repr=False
Expand Down
Loading

0 comments on commit cd87a01

Please sign in to comment.