Skip to content

Commit

Permalink
Updated drawio parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Semprini committed Sep 29, 2024
1 parent 355e2b1 commit e218d18
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
26 changes: 16 additions & 10 deletions mdg/parse/drawio_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from lxml import etree, html
import logging


from mdg.config import settings
from mdg.uml import (
UMLAssociation,
Expand All @@ -22,7 +23,10 @@ def get_label_name(element) -> Optional[str]:
if label_element == "":
return None
tree = html.fromstring(label_element)
label = "".join(list(tree.itertext()))
label = tree.text_content().replace(" ", " ")

# label = "".join(list(tree.itertext()))
# print(label)
return label


Expand Down Expand Up @@ -145,8 +149,8 @@ def generalization_parse(package: UMLPackage, element, root):
source: Optional[UMLClass] = package.find_class_by_id(cell.get("source"))
target: Optional[UMLClass] = package.find_class_by_id(cell.get("target"))
if source == None or target == None:
s = cell.get("source")
t = cell.get("target")
s = cell.get("source") # Kludge for flake8
t = cell.get("target") # Kludge for flake8
logger.warn(f"Cannot find generalization class. Source Id:{s}, Target Id: {t}")
else:
source.generalization = target
Expand All @@ -163,8 +167,8 @@ def association_parse(package: UMLPackage, element, root):
element_type = element.get("UMLType")

if source == None or target == None:
s = cell.get("source")
t = cell.get("target")
s = cell.get("source") # Kludge for flake8
t = cell.get("target") # Kludge for flake8
logger.warn(f"Cannot find association class. Source Id:{s}, Target Id: {t}")
return
else:
Expand Down Expand Up @@ -217,7 +221,7 @@ def class_parse(package: UMLPackage, element, root) -> Optional[UMLClass]:
return None
label_split = label.split(">>")
if len(label_split) > 1:
name = label[-1]
name = label_split[-1]
stereotypes = label[0].split(',')
else:
name = label
Expand Down Expand Up @@ -259,7 +263,7 @@ def enumeration_parse(package: UMLPackage, element, root) -> Optional[UMLEnumera
return None
label_split = label.split(">>")
if len(label_split) > 1:
name = label[-1]
name = label_split[-1]
else:
name = label

Expand All @@ -276,8 +280,10 @@ def enumeration_parse(package: UMLPackage, element, root) -> Optional[UMLEnumera


def attr_parse(parent: UMLClass, element, root, stereotypes) -> UMLAttribute:
value = element.get("value").strip("<div>").strip("</div>").strip("<br")
# height = int(element.find("mxGeometry").get("y"))
a = element.get("value")
tree = html.fromstring(a)
node = tree.xpath("//div")
value = ''.join(node[0].itertext()).replace(" ", " ")

dq = []
if "{dq_even}" in value:
Expand All @@ -292,8 +298,8 @@ def attr_parse(parent: UMLClass, element, root, stereotypes) -> UMLAttribute:
visibility: bool = False
if value.startswith("+"):
visibility = True
value = value.strip("+").strip("-").strip()

value = value.strip("+").strip("-").strip()
name, attr_type = value.split(":")

attr = UMLAttribute(parent, name, element.get('id'))
Expand Down
26 changes: 13 additions & 13 deletions mdg/templates/Schema/openapi.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ servers:
paths:{% for cls in package.classes %}{% if cls.is_abstract == False and cls.id_attribute %}
"{{ package.path | snakecase }}{{ cls.get_name() | snakecase }}/":
get:
operationId: {{ package.path[1:] | snakecase | replace('/', '_') }}{{ cls.get_name() | snakecase }}_list
operationId: {{ package.path[1:] | snakecase | replace('/', '_') }}{{ cls.get_name() | snakecase | replace(' ', '?') }}_list
parameters:
- in: query
name: page_key
Expand All @@ -41,7 +41,7 @@ paths:{% for cls in package.classes %}{% if cls.is_abstract == False and cls.id_
content:
application/json:
schema:
"$ref": "#/components/schemas/{{ cls.get_name() | camelcase }}_list"
"$ref": "#/components/schemas/{{ cls.get_name() | pascalcase }}_list"

"{{ package.path | snakecase }}{{ cls.get_name() | snakecase }}/{% raw %}{{% endraw %}{{ cls.id_attribute.name | snakecase }}{% raw %}}{% endraw %}/":
get:
Expand All @@ -60,7 +60,7 @@ paths:{% for cls in package.classes %}{% if cls.is_abstract == False and cls.id_
content:
application/json:
schema:
"$ref": "#/components/schemas/{{ cls.get_name() | camelcase }}"{% if cls.associations_from != [] or cls.associations_to != [] %}
"$ref": "#/components/schemas/{{ cls.get_name() | pascalcase }}"{% if cls.associations_from != [] or cls.associations_to != [] %}
links:{% for assoc in cls.associations_from %}{% if assoc.destination.is_abstract == False %}
{{ assoc.destination_name | snakecase }}:
operationId: {{ assoc.destination.package.path[1:] | snakecase | replace('/', '_') }}{{ assoc.destination.name | snakecase }}_entity
Expand All @@ -73,18 +73,18 @@ paths:{% for cls in package.classes %}{% if cls.is_abstract == False and cls.id_
{% endif %}{% endfor %}
components:
schemas:{% for cls in package.classes %}
{{ cls.get_name() | camelcase }}_list:
{{ cls.get_name() | pascalcase }}_list:
type: object
description: 'List of {{ cls.get_name() }}'
properties:
result:
type: array
items:
$ref: "#/components/schemas/{{ cls.get_name() | camelcase }}"
$ref: "#/components/schemas/{{ cls.get_name() | pascalcase }}"
next_page_key:
type: string

{{ cls.get_name() | camelcase }}_basic:
{{ cls.get_name() | pascalcase }}_basic:
type: object
description: 'Basic view of {{ cls.get_name() }}'
properties:{% if cls.id_attribute %}
Expand All @@ -105,7 +105,7 @@ components:
- {{ enum }}{% endfor %}{% elif attr.dest_type == "float" %}number
format: float{% else %}{{ attr.dest_type }}{% endif %}{% endfor %}

{{ cls.get_name() | camelcase }}:
{{ cls.get_name() | pascalcase }}:
{% if not cls.supertype %}type: object
description: 'Basic view of {{ cls.get_name() }}'
properties:
Expand All @@ -126,17 +126,17 @@ components:
- {{ enum }}{% endfor %}{% elif attr.dest_type == "float" %}number
format: float{% else %}{{ attr.dest_type }}{% endif %}{% endfor %}{% for assoc in cls.associations_from %}
{{ assoc.destination_name | snakecase }}:
{% if assoc.cardinality.name not in [ "MANY_TO_MANY", "ONE_TO_MANY" ] %}{% if assoc.destination.name != cls.get_name() %}{% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | camelcase }}.yaml#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% endif %}{% else %} type: {{ assoc.destination.id_attribute.dest_type }}{% endif %}{% else %}type: array
{% if assoc.cardinality.name not in [ "MANY_TO_MANY", "ONE_TO_MANY" ] %}{% if assoc.destination.name != cls.get_name() %}{% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | pascalcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | pascalcase }}.yaml#/components/schemas/{{ assoc.destination.name | pascalcase }}_basic"{% endif %}{% else %} type: {{ assoc.destination.id_attribute.dest_type }}{% endif %}{% else %}type: array
items:
oneOf:
- type: {{ assoc.destination.id_attribute.dest_type }}
format: uri
- {% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | camelcase }}.yaml#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% endif %}{% endif %}{% endfor %}{% for assoc in cls.associations_to %}{% if assoc.association_type.name == "COMPOSITION" %}
- {% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | pascalcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | pascalcase }}.yaml#/components/schemas/{{ assoc.destination.name | pascalcase }}_basic"{% endif %}{% endif %}{% endfor %}{% for assoc in cls.associations_to %}{% if assoc.association_type.name == "COMPOSITION" %}
{{ assoc.source_name | snakecase }}:
{% if assoc.cardinality.name == "MANY_TO_ONE" %}type: array
items:
{% endif %}$ref: "#/components/schemas/{{ assoc.source.name | camelcase }}_basic"{% endif %}{% endfor %}{% else %}allOf:
- $ref: "#/components/schemas/{{ cls.supertype.name | camelcase }}"
{% endif %}$ref: "#/components/schemas/{{ assoc.source.name | pascalcase }}_basic"{% endif %}{% endfor %}{% else %}allOf:
- $ref: "#/components/schemas/{{ cls.supertype.name | pascalcase }}"
- type: object{% if cls.attributes|length !=0 %}
properties:{% endif %}{% for attr in cls.attributes %}
{{ attr.name | snakecase }}:
Expand All @@ -146,10 +146,10 @@ components:
- {{ enum }}{% endfor %}{% elif attr.dest_type == "float" %}number
format: float{% else %}{{ attr.dest_type }}{% endif %}{% endfor %}{% for assoc in cls.associations_from %}
{{ assoc.destination_name | snakecase }}:
{% if assoc.cardinality.name not in [ "MANY_TO_MANY", "ONE_TO_MANY" ] %}{% if assoc.destination.name != cls.get_name() %}{% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | camelcase }}.yaml#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% endif %}{% else %} type: {{ assoc.destination.id_attribute.dest_type }}{% endif %}{% else %}type: array
{% if assoc.cardinality.name not in [ "MANY_TO_MANY", "ONE_TO_MANY" ] %}{% if assoc.destination.name != cls.get_name() %}{% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | pascalcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | pascalcase }}.yaml#/components/schemas/{{ assoc.destination.name | pascalcase }}_basic"{% endif %}{% else %} type: {{ assoc.destination.id_attribute.dest_type }}{% endif %}{% else %}type: array
items:
oneOf:
- type: {{ assoc.destination.id_attribute.dest_type }}
format: uri
- {% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | camelcase }}.yaml#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% endif %}{% endif %}{% endfor %}{% endif %}
- {% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | pascalcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | pascalcase }}.yaml#/components/schemas/{{ assoc.destination.name | pascalcase }}_basic"{% endif %}{% endif %}{% endfor %}{% endif %}
{% endfor %}

0 comments on commit e218d18

Please sign in to comment.