Skip to content

Commit

Permalink
Merge pull request #297 from gaphor/coregen
Browse files Browse the repository at this point in the history
Refactor Code Generator to New Module and add CLI
  • Loading branch information
amolenaar committed Apr 12, 2020
2 parents 1fe1427 + 1e182b2 commit 2ad44ee
Show file tree
Hide file tree
Showing 33 changed files with 173 additions and 231 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ icons: ## Generate icons from stensil (requires Inkscape)
translate: ## Translate and update .po and .mo files (requires PyBabel)
$(MAKE) -C po

model: gaphor/core/modeling/coremodel.py gaphor/UML/uml2.py ## Generate Python model files from Gaphor models (requires Black, MyPy)
model: gaphor/core/modeling/coremodel.py gaphor/UML/uml.py ## Generate Python model files from Gaphor models (requires Black, MyPy)

gaphor/core/modeling/coremodel.py: models/Core.gaphor models/Core.override utils/model/gen_uml.py utils/model/override.py utils/model/writer.py
utils/model/build_core.py && black $@ && mypy gaphor/core/modeling
gaphor/core/modeling/coremodel.py: models/Core.gaphor models/Core.override gaphor/codegen/autocoder.py gaphor/codegen/override.py gaphor/codegen/writer.py
gaphor/codegen/codegen.py models/Core.gaphor gaphor/core/modeling/coremodel.py models/Core.override && black $@ && mypy gaphor/core/modeling && isort gaphor/core/modeling/coremodel.py

gaphor/UML/uml2.py: models/UML.gaphor models/UML.override utils/model/gen_uml.py utils/model/override.py utils/model/writer.py
utils/model/build_uml.py && black $@ && mypy gaphor/UML
gaphor/UML/uml.py: models/UML.gaphor models/UML.override gaphor/codegen/autocoder.py gaphor/codegen/override.py gaphor/codegen/writer.py
gaphor/codegen/codegen.py models/UML.gaphor uml.py models/UML.override && black $@ && mypy gaphor/UML && isort gaphor/UML/UML.gaphor

.PHONY: help dist test docs icons translate model
4 changes: 2 additions & 2 deletions gaphor/UML/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Here, order matters
from gaphor.UML.uml2 import * # noqa: isort:skip
from gaphor.UML.uml import * # noqa: isort:skip
from gaphor.UML import modelfactory as model # noqa: isort:skip

from gaphor.UML.umlfmt import format
from gaphor.UML.umllex import parse

import gaphor.UML.uml2overrides # noqa: isort:skip
import gaphor.UML.umloverrides # noqa: isort:skip
2 changes: 1 addition & 1 deletion gaphor/UML/modelfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import itertools
from typing import Iterable, Sequence

from gaphor.UML.uml2 import (
from gaphor.UML.uml import (
Association,
Class,
Classifier,
Expand Down
2 changes: 1 addition & 1 deletion gaphor/UML/tests/test_umlfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

import gaphor.UML.uml2 as UML
import gaphor.UML.uml as UML
from gaphor.core.eventmanager import EventManager
from gaphor.core.modeling import ElementFactory
from gaphor.UML import model
Expand Down
48 changes: 18 additions & 30 deletions gaphor/UML/uml2.py → gaphor/UML/uml.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
# This file is generated by build_uml.py. DO NOT EDIT!
# This file is generated by codegen.py. DO NOT EDIT!

from __future__ import annotations

from typing import List, Callable, Optional
import uuid
from typing import Callable, Optional

# 23: override Diagram
# 20: override PackageableElement
# 17: override NamedElement
# 14: override Element
from gaphor.core.modeling import Diagram, Element, NamedElement, PackageableElement
from gaphor.core.modeling.properties import (
association,
attribute,
enumeration,
derived,
derivedunion,
relation_one,
relation_many,
enumeration,
redefine,
relation_many,
relation_one,
)
from gaphor.core.modeling.collection import collection

# 14: override Element
from gaphor.core.modeling import Element


# 17: override NamedElement
from gaphor.core.modeling import NamedElement


# 20: override PackageableElement
from gaphor.core.modeling import PackageableElement


class InstanceSpecification(PackageableElement):
Expand Down Expand Up @@ -360,10 +352,6 @@ class Stereotype(Class):
pass


# 23: override Diagram
from gaphor.core.modeling import Diagram


class DeployedArtifact(NamedElement):
pass

Expand Down Expand Up @@ -777,7 +765,7 @@ class BehaviorExecutionSpecification(ExecutionSpecification):
"visibility", ("public", "private", "package", "protected"), "public"
)
# 106: override Message.messageKind: property
# defined in uml2overrides.py
# defined in umloverrides.py

Message.messageSort = enumeration(
"messageSort",
Expand Down Expand Up @@ -1264,7 +1252,7 @@ class BehaviorExecutionSpecification(ExecutionSpecification):
)

# 88: override Property.navigability(Property.opposite, Property.association): derived[Optional[bool]]
# defined in uml2overrides.py
# defined in umloverrides.py

RedefinableElement.redefinedElement = derivedunion(
RedefinableElement,
Expand Down Expand Up @@ -1322,7 +1310,7 @@ class BehaviorExecutionSpecification(ExecutionSpecification):
Operation.interface_,
)
# 79: override Property.opposite(Property.association, Association.memberEnd): relation_one[Optional[Property]]
# defined in uml2overrides.py
# defined in umloverrides.py

BehavioralFeature.parameter = derivedunion(
BehavioralFeature,
Expand Down Expand Up @@ -1449,7 +1437,7 @@ class BehaviorExecutionSpecification(ExecutionSpecification):
Operation.type = derivedunion(Operation, "type", DataType, 0, 1)

# 64: override Extension.metaclass(Extension.ownedEnd, Association.memberEnd): property
# defined in uml2overrides.py
# defined in umloverrides.py

# 52: override Class.extension(Extension.metaclass): property
# See https://www.omg.org/spec/UML/2.5/PDF, section 11.8.3.6, page 219
Expand Down Expand Up @@ -1540,7 +1528,7 @@ class BehaviorExecutionSpecification(ExecutionSpecification):
StructuredClassifier.role,
)
# 103: override Component.required: property
# defined in uml2overrides.py
# defined in umloverrides.py

# 76: override Namespace.importedMember: derivedunion[PackageableElement]
Namespace.importedMember = derivedunion(
Expand All @@ -1549,7 +1537,7 @@ class BehaviorExecutionSpecification(ExecutionSpecification):

Action.input = derivedunion(Action, "input", InputPin, 0, "*", SendSignalAction.target)
# 100: override Component.provided: property
# defined in uml2overrides.py
# defined in umloverrides.py

Element.owner = derivedunion(
Element,
Expand Down Expand Up @@ -1712,7 +1700,7 @@ class BehaviorExecutionSpecification(ExecutionSpecification):
OccurrenceSpecification, "covered", Lifeline, 1, InteractionFragment.covered
)
# 94: override Lifeline.parse: Callable[[Lifeline, str], None]
# defined in uml2overrides.py
# defined in umloverrides.py

# 97: override Lifeline.render: Callable[[Lifeline], str]
# defined in uml2overrides.py
# defined in umloverrides.py
2 changes: 1 addition & 1 deletion gaphor/UML/umlfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
from functools import singledispatch

from gaphor.UML import uml2 as UML
from gaphor.UML import uml as UML


@singledispatch
Expand Down
32 changes: 16 additions & 16 deletions gaphor/UML/umllex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import re
from functools import singledispatch

import gaphor.UML.uml2 as uml2
from gaphor.UML import uml


@singledispatch
Expand Down Expand Up @@ -124,7 +124,7 @@ def compile(regex):
lifeline_pat = compile("^" + name_subpat + type_subpat + garbage_subpat)


def _set_visibility(el: uml2.Feature, vis: str):
def _set_visibility(el: uml.Feature, vis: str):
if vis == "+":
el.visibility = "public"
elif vis == "#":
Expand All @@ -140,7 +140,7 @@ def _set_visibility(el: uml2.Feature, vis: str):
pass


def parse_attribute(el: uml2.Property, s: str) -> None:
def parse_attribute(el: uml.Property, s: str) -> None:
"""
Parse string s in the property. Tagged values, multiplicity and stuff
like that is altered to reflect the data in the property string.
Expand Down Expand Up @@ -170,7 +170,7 @@ def parse_attribute(el: uml2.Property, s: str) -> None:
el.defaultValue = g("default")


def parse_association_end(el: uml2.Property, s: str) -> None:
def parse_association_end(el: uml.Property, s: str) -> None:
"""
Parse the text at one end of an association. The association end holds
two strings. It is automatically figured out which string is fed to the
Expand Down Expand Up @@ -215,16 +215,16 @@ def parse_association_end(el: uml2.Property, s: str) -> None:
el.upperValue = g("mult_u")


@parse.register(uml2.Property)
def parse_property(el: uml2.Property, s: str) -> None:
@parse.register(uml.Property)
def parse_property(el: uml.Property, s: str) -> None:
if el.association:
parse_association_end(el, s)
else:
parse_attribute(el, s)


@parse.register(uml2.Operation)
def parse_operation(el: uml2.Operation, s: str) -> None:
@parse.register(uml.Operation)
def parse_operation(el: uml.Operation, s: str) -> None:
"""
Parse string s in the operation. Tagged values, parameters and
visibility is altered to reflect the data in the operation string.
Expand All @@ -233,15 +233,15 @@ def parse_operation(el: uml2.Operation, s: str) -> None:
if not m or m.group("garbage"):
el.name = s
del el.visibility
list(map(uml2.Parameter.unlink, list(el.returnResult)))
list(map(uml2.Parameter.unlink, list(el.formalParameter)))
list(map(uml.Parameter.unlink, list(el.returnResult)))
list(map(uml.Parameter.unlink, list(el.formalParameter)))
else:
g = m.group
create = el.model.create
_set_visibility(el, g("vis"))
el.name = g("name")
if not el.returnResult:
el.returnResult = create(uml2.Parameter)
el.returnResult = create(uml.Parameter)
p = el.returnResult[0]
p.direction = "return"
p.typeValue = g("type")
Expand All @@ -258,7 +258,7 @@ def parse_operation(el: uml2.Operation, s: str) -> None:
try:
p = el.formalParameter[pindex]
except IndexError:
p = create(uml2.Parameter)
p = create(uml.Parameter)
p.direction = g("dir") or "in"
p.name = g("name")
p.typeValue = g("type")
Expand All @@ -276,7 +276,7 @@ def parse_operation(el: uml2.Operation, s: str) -> None:
fp.unlink()


def parse_lifeline(el: uml2.Lifeline, s: str) -> None:
def parse_lifeline(el: uml.Lifeline, s: str) -> None:
"""
Parse string s in a lifeline. If a class is defined and can be found
in the datamodel, then a class is connected to the lifelines 'represents'
Expand All @@ -295,14 +295,14 @@ def parse_lifeline(el: uml2.Lifeline, s: str) -> None:
# Lifeline.represents: ConnectableElement


def render_lifeline(el: uml2.Lifeline) -> str:
def render_lifeline(el: uml.Lifeline) -> str:
"""
"""
return el.name or ""


@parse.register(uml2.NamedElement)
def parse_namedelement(el: uml2.NamedElement, text: str) -> None:
@parse.register(uml.NamedElement)
def parse_namedelement(el: uml.NamedElement, text: str) -> None:
"""
Parse named element by simply assigning text to its name.
"""
Expand Down

0 comments on commit 2ad44ee

Please sign in to comment.