Пример #1
0
    def render(self, classes: List[Class]) -> Iterator[Tuple[Path, str, str]]:
        """Given  a list of classes return to the writer factory the target
        file path full module path and the rendered code."""

        packages = {obj.source_qname(): obj.target_module for obj in classes}
        resolver = DependenciesResolver(packages=packages)

        for target_package, cluster in self.group_by_package(classes).items():
            yield target_package, "init", self.render_package(cluster)

        for target_module, cluster in self.group_by_module(classes).items():
            file_path = Path.cwd().joinpath(target_module.replace(".", "/") + ".py")
            yield file_path, target_module, self.render_module(resolver, cluster)
Пример #2
0
    def render(self, classes: List[Class]) -> Iterator[Tuple[Path, str, str]]:
        """Given  a list of classes return to the writer factory the target
        file path full module path and the rendered code."""

        packages = {obj.source_qname(): obj.target_module for obj in classes}
        resolver = DependenciesResolver(packages=packages)

        groups: Dict[str, List] = defaultdict(list)
        for obj in classes:
            groups[obj.target_module].append(obj)

        for target_module, classes in groups.items():
            resolver.process(classes)
            output = self.render_classes(resolver.sorted_classes())
            file_path = Path.cwd().joinpath(target_module.replace(".", "/") + ".pu")

            yield file_path, target_module, self.render_module(output=output)
Пример #3
0
    def test_render_module(self):
        classes = [ClassFactory.enumeration(2), ClassFactory.elements(2)]
        resolver = DependenciesResolver()

        actual = DataclassGenerator().render_module(resolver, classes)
        expected = (
            "from enum import Enum\n"
            "from dataclasses import dataclass, field\n"
            "from typing import Optional\n\n"
            '__NAMESPACE__ = "xsdata"\n\n\n'
            "class ClassB(Enum):\n"
            '    """\n'
            "    :cvar NONE_VALUE:\n"
            '    """\n'
            "    NONE_VALUE = None\n\n\n"
            "@dataclass\n"
            "class ClassC:\n"
            '    """\n'
            "    :ivar attr_d:\n"
            "    :ivar attr_e:\n"
            '    """\n'
            "    class Meta:\n"
            '        name = "class_C"\n\n'
            "    attr_d: Optional[str] = field(\n"
            "        default=None,\n"
            "        metadata=dict(\n"
            '            name="attr_D",\n'
            '            type="Element"\n'
            "        )\n"
            "    )\n"
            "    attr_e: Optional[str] = field(\n"
            "        default=None,\n"
            "        metadata=dict(\n"
            '            name="attr_E",\n'
            '            type="Element"\n'
            "        )\n"
            "    )\n"
        )
        self.assertEqual(expected, actual)
Пример #4
0
 def setUp(self):
     super(DependenciesResolverTest, self).setUp()
     self.resolver = DependenciesResolver()