def package_parse(element, root_element, parent_package: Optional[UMLPackage]) -> 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') package = UMLPackage(id, name, parent_package) package._root_element = root_element model = root_element.find('./diagram/mxGraphModel/root') child_elements = model.findall('object/mxCell[@parent="{}"]'.format(id)) logger.debug("Added UMLPackage {}".format(package.path)) # Parse classes for element in child_elements: object_element = element.find("..") element_type = object_element.get("UMLType") if element_type == "Class": cls = class_parse(package, object_element, root_element) package.classes.append(cls) elif element_type == "Enumeration": enum = enumeration_parse(package, object_element, root_element) package.enumerations.append(enum) elif element_type == "Package": pkg = package_parse(object_element, package._root_element, package) package.children.append(pkg) # Classes are needed to parse generalisations and associations for element in child_elements: object_element = element.find("..") element_type = object_element.get("UMLType") if element_type == "Generalization": generalization_parse(package, object_element, root_element) elif element_type == "Association": association_parse(package, object_element, root_element) elif element_type == "Composition": association_parse(package, object_element, root_element) return package
def package_parse(element, root_element, parent_package): """ Extract package details, call class parser for classes and self parser for sub-packages. Associations are not done here, but in a 2nd pass using the parse_associations function. :param element: :param root_element: :return: :rtype: UMLPackage """ name = element.get('name') id = element.get('{%s}id' % ns['xmi']) package = UMLPackage(id, name, parent_package) package._element = element package._root_element = root_element # Detail is Sparx specific # TODO: Put modelling tool in settings and use tool specific parser here detail = root_element.xpath("//element[@xmi:idref='%s']" % package.id, namespaces=ns)[0] properties = detail.find('properties') package.stereotype = properties.get('stereotype') if package.stereotype is not None: package.inherited_stereotypes.append([package.stereotype, package]) package.documentation = properties.get('documentation') if package.documentation is None: package.documentation = "" project = detail.find('project') package.status = project.get('status') diagram_elements = root_element.xpath( "//diagrams/diagram/model[@package='%s']" % package.id) for diagram_model in diagram_elements: diagram = diagram_model.getparent() package.diagrams.append(diagram.get('{%s}id' % ns['xmi'])) logger.debug("Added UMLPackage {}".format(package.path)) package_parse_children(element, package) return package