Skip to content

Commit

Permalink
Clean up of tests and draw io error logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Semprini committed Sep 25, 2024
1 parent 9fe07e7 commit 440068f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
39 changes: 28 additions & 11 deletions mdg/parse/drawio_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
logger = logging.getLogger(__name__)


def get_label_name(element):
tree = html.fromstring( element.get("label"))
def get_label_name(element) -> Optional[str]:
label_element = element.get("label")
if label_element == "":
return None
tree = html.fromstring(label_element)
label = "".join(list(tree.itertext()))
return label

Expand All @@ -28,7 +31,7 @@ def find_label_name(element, name):

for object in objects:
label = get_label_name(object)
if name in label:
if label and name in label:
return object
return None

Expand All @@ -37,7 +40,7 @@ def parse_uml() -> Tuple[UMLPackage, List[UMLInstance]]:
test_cases: List[UMLInstance] = []

# Parse into etree and grab root package
root = etree.parse(settings['source']).getroot()
root = etree.parse(settings['source']).getroot() # type: ignore
model = root.find('./diagram/mxGraphModel/root')
if model is None:
raise ValueError(f"Cannot find model named {settings['source']} in XML at path ./diagram/mxGraphModel/root")
Expand All @@ -50,20 +53,25 @@ def parse_uml() -> Tuple[UMLPackage, List[UMLInstance]]:
if model_element is None:
raise ValueError("Model element not found. Settings has:{}".format(settings['model_package']))

model_package: UMLPackage = package_parse(model_element, root, None)
model_package = package_parse(model_element, root, None)
if model_package is None:
raise ValueError("Could not parse packages from model element. Settings has:{}".format(settings['model_package']))

enumeration_link(model_package)

return model_package, test_cases


def package_parse(element, root_element, parent_package: Optional[UMLPackage]) -> UMLPackage:
def package_parse(element, root_element, parent_package: Optional[UMLPackage]) -> Optional[UMLPackage]:
""" Extract package details, call class parser for classes and self parser for sub-packages.
Association linking is not done here, but in a 2nd pass using the parse_associations function.
"""

name = get_label_name(element)
id = element.get('id')
if name is None or id is None:
logger.warn(f"Could not find valid name of package in XML node 'label'. Node has keys of {element.keys()} and values of {element.values()}")
return None

package = UMLPackage(id, name, parent_package)
package._root_element = root_element
Expand All @@ -80,15 +88,18 @@ def package_parse(element, root_element, parent_package: Optional[UMLPackage]) -

if element_type == "Class":
cls = class_parse(package, object_element, root_element)
package.classes.append(cls)
if cls:
package.classes.append(cls)

elif element_type == "Enumeration":
enum = enumeration_parse(package, object_element, root_element)
package.enumerations.append(enum)
if enum:
package.enumerations.append(enum)

elif element_type == "Package":
pkg = package_parse(object_element, package._root_element, package)
package.children.append(pkg)
if pkg:
package.children.append(pkg)

# Classes are needed to parse generalisations and associations
for element in child_elements:
Expand Down Expand Up @@ -194,9 +205,12 @@ def association_parse(package: UMLPackage, element, root):
association.source_name += 's'


def class_parse(package: UMLPackage, element, root) -> UMLClass:
def class_parse(package: UMLPackage, element, root) -> Optional[UMLClass]:
stereotypes = []
label = get_label_name(element)
if label is None:
logger.warn(f"Could not find valid name of class in XML node 'label'. Node has keys of {element.keys()} and values of {element.values()}")
return None
label_split = label.split(">>")
if len(label_split) > 1:
name = label[-1]
Expand Down Expand Up @@ -234,8 +248,11 @@ def class_parse(package: UMLPackage, element, root) -> UMLClass:
return cls


def enumeration_parse(package: UMLPackage, element, root) -> UMLEnumeration:
def enumeration_parse(package: UMLPackage, element, root) -> Optional[UMLEnumeration]:
label = get_label_name(element)
if label is None:
logger.warn(f"Could not find valid name of enumeration in XML node 'label'. Node has keys of {element.keys()} and values of {element.values()}")
return None
label_split = label.split(">>")
if len(label_split) > 1:
name = label[-1]
Expand Down
2 changes: 1 addition & 1 deletion sample_recipes/drawio/config-drawio-django.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
root_package: SampleIndustry
root_package: SampleDomain
model_package: Model
test_package: Test
source: ./sample_recipes/drawio/sample.xml
Expand Down
2 changes: 1 addition & 1 deletion sample_recipes/drawio/config-drawio-java.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
root_package: SampleIndustry
root_package: SampleDomain
model_package: Model
test_package: Test
source: ./sample_recipes/drawio/sample.xml
Expand Down

0 comments on commit 440068f

Please sign in to comment.