-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
129 additions
and
30 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pathlib import Path | ||
|
||
from tests.factories import AttrFactory, ClassFactory, FactoryTestCase | ||
from xsdata.formats.plantuml.generator import PlantUmlGenerator | ||
from xsdata.models.elements import Schema | ||
|
||
|
||
class PlantUmlGeneratorTests(FactoryTestCase): | ||
def test_render(self): | ||
schema = Schema.create(location=Path("foo.xsd")) | ||
package = "some.Foo.Some.ThugLife" | ||
classes = [ | ||
ClassFactory.create(attrs=AttrFactory.list(2)), | ||
ClassFactory.create(attrs=AttrFactory.list(3)), | ||
] | ||
|
||
iterator = PlantUmlGenerator().render(schema, classes, package) | ||
|
||
actual = [(file, output) for file, output in iterator] | ||
self.assertEqual(1, len(actual)) | ||
self.assertEqual(2, len(actual[0])) | ||
self.assertIsInstance(actual[0][0], Path) | ||
self.assertTrue(actual[0][0].is_absolute()) | ||
self.assertEqual( | ||
"some/Foo/Some/ThugLife/foo.pu", | ||
str(actual[0][0].relative_to(Path.cwd())), | ||
) | ||
|
||
output = """@startuml | ||
class class_B { | ||
+attr_B : xs:string | ||
+attr_C : xs:string | ||
} | ||
class class_C { | ||
+attr_D : xs:string | ||
+attr_E : xs:string | ||
+attr_F : xs:string | ||
} | ||
@enduml""" | ||
self.assertEqual(output, actual[0][1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from pathlib import Path | ||
from typing import Iterator, List, Tuple | ||
|
||
from xsdata.generators import AbstractGenerator | ||
from xsdata.models.codegen import Class | ||
from xsdata.models.elements import Schema | ||
|
||
|
||
class PlantUmlGenerator(AbstractGenerator): | ||
templates_dir = Path(__file__).parent.joinpath("templates") | ||
|
||
def render_module(self, output: str) -> str: | ||
return self.template("module").render(output=output) | ||
|
||
def render_class(self, obj: Class) -> str: | ||
template = "enum" if obj.is_enumeration else "class" | ||
return self.template(template).render(obj=obj) | ||
|
||
def render( | ||
self, schema: Schema, classes: List[Class], package: str | ||
) -> Iterator[Tuple[Path, str]]: | ||
"""Given a schema, a list of classes and a target package return to the | ||
writer factory the target file path and the rendered output.""" | ||
module = schema.module | ||
package_arr = package.split(".") | ||
package = "{}.{}".format(".".join(package_arr), module) | ||
target = Path.cwd().joinpath(*package_arr) | ||
file_path = target.joinpath(f"{module}.pu") | ||
|
||
self.resolver.process(classes=classes, schema=schema, package=package) | ||
|
||
output = self.render_classes() | ||
|
||
yield file_path, self.render_module(output=output) | ||
|
||
def render_classes(self) -> str: | ||
"""Sort classes by name and return the rendered output.""" | ||
classes = sorted(self.resolver.sorted_classes(), key=lambda x: x.name) | ||
output = "\n".join(map(self.render_class, classes)).strip() | ||
return f"\n{output}\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class {{ obj.name }} { | ||
{%- for attr in obj.attrs %} | ||
+{{ attr.name }} : {{ attr.type }}{{ "[]" if attr.is_list else "" }} | ||
{%- endfor %} | ||
} | ||
{%- for ext in obj.extensions %} | ||
{{ obj.name }} *- {{ ext.name }} | ||
{%- endfor %} | ||
{%- for inner in obj.inner %} | ||
{{ obj.name }} +-- {{ inner.name }} | ||
{% with obj=inner -%} | ||
{%- include "class.jinja2" %} | ||
{%- endwith -%} | ||
{%- endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum {{ obj.name }} { | ||
{%- for attr in obj.attrs %} | ||
{{ attr.name }} = {{ attr.default }} | ||
{%- endfor %} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@startuml | ||
{{ output }} | ||
@enduml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters