Skip to content

Commit

Permalink
Remove generators print interface
Browse files Browse the repository at this point in the history
Notes:
A generator should only be responsible to render the file path and
the content the writer should write.
  • Loading branch information
tefra committed Jan 8, 2020
1 parent d68417f commit eda4df4
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 23 deletions.
3 changes: 0 additions & 3 deletions tests/formats/dataclass/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class DataclassGeneratorTests(FactoryTestCase):
@mock.patch.object(Path, "mkdir")
@mock.patch.object(DependenciesResolver, "process")
@mock.patch.object(DataclassGenerator, "render_module")
@mock.patch.object(DataclassGenerator, "render_classes")
Expand All @@ -19,7 +18,6 @@ def test_render(
mock_render_classes,
mock_render_module,
mock_resolved_process,
mock_mkdir,
):
schema = Schema.create(location=Path("foo.xsd"))
package = "some.Foo.Some.ThugLife"
Expand Down Expand Up @@ -56,7 +54,6 @@ def test_render(
imports=mock_prepare_imports.return_value,
output=mock_render_classes.return_value,
)
mock_mkdir.assert_called_once_with(parents=True, exist_ok=True)

@mock.patch.object(DataclassGenerator, "render_class")
@mock.patch.object(DataclassGenerator, "process_class")
Expand Down
12 changes: 0 additions & 12 deletions xsdata/formats/dataclass/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,12 @@ def render(
file_path = target.joinpath(f"{module}.py")

self.resolver.process(classes=classes, schema=schema, package=package)
target.mkdir(parents=True, exist_ok=True)

imports = self.prepare_imports()
output = self.render_classes()

yield file_path, self.render_module(imports=imports, output=output)

def print(
self, schema: Schema, classes: List[Class], package: str
) -> Iterator[Tuple[str, Class]]:
module = snake_case(schema.module)
package_arr = list(map(snake_case, package.split(".")))
package = "{}.{}".format(".".join(package_arr), module)
self.resolver.process(classes=classes, schema=schema, package=package)

for obj in sorted(self.prepare_classes(), key=lambda x: x.name):
yield package, obj

def render_classes(self) -> str:
"""Get a list of sorted classes from the imports resolver, apply the
python code conventions and return the rendered output."""
Expand Down
6 changes: 0 additions & 6 deletions xsdata/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ def render(
) -> Iterator[Tuple[Path, str]]:
pass

@abstractmethod
def print(
self, schema: Schema, classes: List[Class], package: str
) -> Iterator[Tuple[str, Class]]:
pass


class PythonAbstractGenerator(AbstractGenerator, ABC):
@classmethod
Expand Down
5 changes: 3 additions & 2 deletions xsdata/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def write(
engine = self.get_renderer(renderer)
for file, output in engine.render(schema, classes, package):
logger.info(f"Generating package: `{package}`")
with open(str(file), "w") as fp:
fp.write(output)

file.parent.mkdir(parents=True, exist_ok=True)
file.write_text(output)

def print(
self, schema: Schema, classes: List[Class], package: str, renderer: str
Expand Down

0 comments on commit eda4df4

Please sign in to comment.