Skip to content

Commit

Permalink
Fixes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
Semprini committed Feb 5, 2024
1 parent b53e071 commit df34254
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 50 deletions.
6 changes: 3 additions & 3 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The gitgub project has a sample_recipes which has some examples. Each recipe con
parser: # <sparx, sparxdb or drawio>
dest_root: # <base path where to put each generated artifact>
templates_folder: # <base path where to find custom templates>
dialect: # <language to translate attributes into optios are: default, spring data rest, django, marshmallow, sqlalchemy, python, ddl>
model_templates: # <list of artifacts to generate, see below for details>
default_dialect: # <language to translate attributes into optios are: default, spring data rest, django, marshmallow, sqlalchemy, python, ddl>
generation_artifacts: # <list of artifacts to generate, see below for details>
- dest: # <jinja template string which parses to the output file name>
level: # <package, class, root, copy>
source: # <path to jinja template file to render>
Expand All @@ -22,7 +22,7 @@ Model Templates Detail

In the config yaml for your project there is a list called::

model_templates:
generation_artifacts:

This is a list of files to render based on the parsed classes as discussed in the Metamodel page

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial-sparx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ pyMDG parses the XMI into the internal classes shown in the metamodel section of
| dest_root: ./build
| templates_folder: ./mdg/templates
| dialect: default
| default_dialect: default
| Lastly we add a list of the artifacts we want to generate::
| model_templates:
| generation_artifacts:
| # Avro Schema
| - dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc"
| level: class
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/sparx/schemagen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ source: ./tutorial1.xmi
parser: sparx
dest_root: ./build
templates_folder: ./mdg/templates
dialect: default
model_templates:
default_dialect: default
generation_artifacts:
# Avro Schema
- dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc"
level: class
Expand Down
6 changes: 3 additions & 3 deletions mdg/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

logger = logging.getLogger(__name__)

model_templates: List[Dict] = []
generation_artifacts: List[Dict] = []

defaults: Dict = {
"dialect": "default",
"default_dialect": "default",
"root_package": "default",
"model_templates": model_templates,
"generation_artifacts": generation_artifacts,
"case_package": "CamelCase",
"case_class": "CamelCase",
"case_attribute": "snake_case",
Expand Down
6 changes: 3 additions & 3 deletions mdg/generate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def validate_settings():
logger.debug("Validating settings")

errors = []
if settings['dialect'] not in generation_fields.keys():
errors.append( f"Settings dialect of '{settings['dialect']}' is not valid. Generation types are a dictionay of how to map UML attrribute types to physical types. Options are: {generation_fields.keys()}" )
if settings['default_dialect'] not in generation_fields.keys():
errors.append( f"Settings default_dialect of '{settings['default_dialect']}' is not valid. Generation types are a dictionay of how to map UML attrribute types to physical types. Options are: {generation_fields.keys()}" )

if settings['parser'] not in PARSERS.keys():
errors.append( f"Settings parser of '{settings['parser']}' is not valid. Parser must match the input modelling tool type defined in 'source'. Options are: {PARSERS.keys()}" )

for template_definition in settings['model_templates']:
for template_definition in settings['generation_artifacts']:
levels = ["root","copy","class","package","enumeration","assocication"]
if template_definition["level"] not in levels:
errors.append( f"Template level of '{template_definition['level']}' is not valid. Level defines how many times the output will be rendered and the objects passed to the template. Options are: {levels}" )
Expand Down
2 changes: 1 addition & 1 deletion mdg/generate/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def output_model(package: UMLPackage) -> None:

# Loop through all template definitions in the config file
template_definition: Dict
for template_definition in settings['model_templates']:
for template_definition in settings['generation_artifacts']:
dest_file_template: Template = dest_env.from_string(os.path.join(settings['dest_root'], template_definition['dest']))

if template_definition['level'] == 'copy':
Expand Down
8 changes: 4 additions & 4 deletions mdg/tools/mdg_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def dumps(args):


def startproject(args):
""" mdg-tool startproject --source="sqlite:///Customer Model.qea" --parser=sparx --model_package=<GUID> --dialect=django config-customer-graphql.yaml
""" mdg-tool startproject --source="sqlite:///Customer Model.qea" --parser=sparx --model_package=<GUID> --default_dialect=django config-customer-graphql.yaml
"""

with open(args.project_path, "w") as f:
Expand All @@ -46,8 +46,8 @@ def startproject(args):
f.write(f"model_package: {args.model_package}\n")
f.write("dest_root: ./build\n")
f.write(f"templates_folder: ./templates\n")
f.write(f"dialect: {args.dialect}\n")
f.write("model_templates:\n")
f.write(f"default_dialect: {args.default_dialect}\n")
f.write("generation_artifacts:\n")


def addtemplate(args):
Expand Down Expand Up @@ -97,7 +97,7 @@ def main():
parser_c = subparsers.add_parser('startproject', help='Create project with recipe and templates')
parser_c.add_argument('-s', '--source', type=str, help="The source model file/DB connection")
parser_c.add_argument('-p', '--parser', type=str, choices=['sparx', 'drawio'], help='The format of the source.')
parser_c.add_argument('-d', '--dialect', type=str, choices=['default', 'django', 'schema', 'java'], help='The type of project')
parser_c.add_argument('-d', '--default_dialect', type=str, choices=['default', 'django', 'schema', 'java'], help='The dialect to use if not specified per artifact')
parser_c.add_argument('-m', '--model_package', type=str, help='The ID of the root package in ther source.')
parser_c.add_argument('project_path', type=str, help='The path to the project')
parser_c.set_defaults(func=startproject)
Expand Down
12 changes: 6 additions & 6 deletions mdg/uml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,13 @@ def __init__(self, parent: Union[UMLClass, UMLEnumeration, UMLComponent, UMLInst
def __str__(self) -> str:
return f"{self.name}"

def get_type(self, translator: Optional[str] = None) -> str:
def get_type(self, dialect: Optional[str] = None) -> str:
""" Returns either the attribute type or a translated type if the dialect is specified in the args
"""
if not translator:
if not dialect:
return f"{self.dest_type}"
if self.type in generation_fields[translator].keys():
return generation_fields[translator][f"{self.type}"]
if self.type in generation_fields[dialect].keys():
return generation_fields[dialect][f"{self.type}"]
return f"{self.type}"

def set_type(self, source_type: str):
Expand All @@ -450,8 +450,8 @@ def set_type(self, source_type: str):
self.length = 100

self.type = source_type
if source_type in generation_fields[settings['dialect']].keys():
self.dest_type = generation_fields[settings['dialect']][source_type]
if source_type in generation_fields[settings['default_dialect']].keys():
self.dest_type = generation_fields[settings['default_dialect']][source_type]
else:
self.dest_type = source_type

Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/bouml/config-bouml-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ source: ./sample_recipes/bouml/sample.xmi
parser: bouml
dest_root: ./build/sample_bouml_schema
templates_folder: ./mdg/templates
dialect: default
model_templates:
default_dialect: default
generation_artifacts:
# Avro Schema
- dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc"
level: class
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/config-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ source: ./sample_recipes/sample-test.xml
parser: drawio
dest_root: ./build/test
templates_folder: does_not_exist_so_will_use_default
dialect: django
default_dialect: django
case_package: CamelCase
case_class: CamelCase
case_attribute: snake_case
model_templates:
generation_artifacts:
- dest: "{{ cls.name | case_class }}.avsc"
level: class
source: "Schema/avro.avsc.jinja"
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/drawio/config-drawio-django.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source: ./sample_recipes/drawio/sample.xml
parser: drawio
dest_root: ./build/sample_drawio_django
templates_folder: ./mdg/templates
dialect: django
model_templates:
default_dialect: django
generation_artifacts:
# Avro Schema
- dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc"
level: class
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/drawio/config-drawio-java.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source: ./sample_recipes/drawio/sample.xml
parser: drawio
dest_root: ./build/sample_drawio_java
templates_folder: ./mdg/templates
dialect: spring data rest
model_templates:
default_dialect: spring data rest
generation_artifacts:
# Java: JHipster
- dest: jdl{{package.path}}{{package.name | case_package}}.jdl
level: package
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/erwin/config-erwin-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ source: ./sample_recipes/erwin/erwin_sample.xmi
parser: erwin
dest_root: ./build/sample_erwin_schema
templates_folder: ./mdg/templates
dialect: default
model_templates:
default_dialect: default
generation_artifacts:
# Avro Schema
- dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc"
level: class
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/sparx/config-sparx-django.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source: ./sample_recipes/sparx/sample_industry.xmi
parser: sparx
dest_root: ./build/sample_sparx_django
templates_folder: ./mdg/templates
dialect: django
model_templates:
default_dialect: django
generation_artifacts:
# Avro Schema
- dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc"
level: class
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/sparx/config-sparx-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source: ./sample_recipes/sparx/sample_industry.xmi
parser: sparx
dest_root: ./build/sample_sparx_docs
templates_folder: ./mdg/templates
dialect: default
model_templates:
default_dialect: default
generation_artifacts:
- dest: base.txt
level: root
source: base.txt.jinja
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/sparx/config-sparx-python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source: ./sample_recipes/sparx/sample_industry.xmi
parser: sparx
dest_root: ./build/sample_sparx_python
templates_folder: ./mdg/templates
dialect: django
model_templates:
default_dialect: django
generation_artifacts:
# Python: Django App
- dest: django{{package.path}}/models.py
level: package
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/sparx/config-sparx-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source: ./sample_recipes/sparx/sample_industry.xmi
parser: sparx
dest_root: ./build/sample_sparx_schema
templates_folder: ./mdg/templates
dialect: default
model_templates:
default_dialect: default
generation_artifacts:
# Avro Schema
- dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc"
level: class
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/sparx/config-sparx-sqlalchemy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source: ./sample_recipes/sparx/sample_industry.xmi
parser: sparx
dest_root: ./build/
templates_folder: ./templates
dialect: sqlalchemy
model_templates:
default_dialect: sqlalchemy
generation_artifacts:
# Python: SQL Alchemy
- dest: "sqlalchemy{{package.path | case_package}}/models.py"
level: package
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/sparxdb/config-sparxdb-graphql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ source: sqlite:///./sample_recipes/sparxdb/sample.qea
parser: sparxdb
dest_root: ./build/sample_sparxdb_graphql
templates_folder: ./mdg/templates
dialect: django
model_templates:
default_dialect: django
generation_artifacts:
# Hasura Schema
- dest: "{{package.name | camelcase}}-hasura_metadata.json"
level: root
Expand Down
4 changes: 2 additions & 2 deletions sample_recipes/sparxdb/config-sparxdb-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ source: sqlite:///./sample_recipes/sparxdb/sample.qea
parser: sparxdb
dest_root: ./build/sample_sparxdb_schema
templates_folder: ./mdg/templates
dialect: default
model_templates:
default_dialect: default
generation_artifacts:
# Avro Schema
- dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc"
level: class
Expand Down
4 changes: 2 additions & 2 deletions test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ parser: sparx
model_package: AEB30CD7-DECA-4310-BFD6-9225F9251D9A
dest_root: ./build
templates_folder: ./templates
dialect: django
model_templates:
default_dialect: django
generation_artifacts:

- dest: "{{package.root_package.name | camelcase}}/{{package.name | camelcase}}/models.py"
level: package
Expand Down

0 comments on commit df34254

Please sign in to comment.