Esempio n. 1
0
def generate(**kwargs: Any):
    """
    Generate code from xml schemas, webservice definitions and any xml or json
    document.

    The input source can be either a filepath, uri or a directory
    containing xml, json, xsd and wsdl files.
    """
    if kwargs["print"]:
        logger.setLevel(logging.ERROR)

    config_file = Path(kwargs["config"])
    if config_file.exists():
        config = GeneratorConfig.read(config_file)
        if kwargs["package"] != "generated":
            config.output.package = kwargs["package"]
    else:
        config = GeneratorConfig()
        config.output.format = kwargs["output"]
        config.output.package = kwargs["package"]
        config.output.compound_fields = kwargs["compound_fields"]
        config.output.docstring_style = DocstringStyle(
            kwargs["docstring_style"])

        if kwargs["ns_struct"]:
            config.output.structure = OutputStructure.NAMESPACES

    uris = resolve_source(kwargs["source"])
    transformer = SchemaTransformer(config=config, print=kwargs["print"])
    transformer.process(list(uris))
Esempio n. 2
0
def generate(**kwargs: Any):
    """
    Convert schema definitions to code.

    SOURCE can be either a filepath, directory or url
    """
    if kwargs["print"]:
        logger.setLevel(logging.ERROR)

    config_file = Path(kwargs["config"])
    if config_file.exists():
        config = GeneratorConfig.read(config_file)
    else:
        config = GeneratorConfig()
        config.output.format = OutputFormat(kwargs["output"])
        config.output.package = kwargs["package"]
        config.output.wsdl = kwargs["wsdl"]
        config.output.compound_fields = kwargs["compound_fields"]
        config.output.docstring_style = DocstringStyle(
            kwargs["docstring_style"])

        if kwargs["ns_struct"]:
            config.output.structure = OutputStructure.NAMESPACES

    uris = resolve_source(kwargs["source"], wsdl=config.output.wsdl)
    transformer = SchemaTransformer(config=config, print=kwargs["print"])

    if config.output.wsdl:
        transformer.process_definitions(next(uris))
    else:
        transformer.process_schemas(list(uris))
Esempio n. 3
0
    def test_generate_with_configuration_file_and_overriding_args(
            self, mock_init, _):
        file_path = Path(tempfile.mktemp())
        config = GeneratorConfig()
        config.output.package = "foo.bar"
        config.output.structure = OutputStructure.FILENAMES
        with file_path.open("w") as fp:
            config.write(fp, config)

        source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd")
        result = self.runner.invoke(
            cli,
            [
                str(source),
                "--config",
                str(file_path),
                "--package",
                "foo",
                "--ns-struct",
            ],
        )
        config = mock_init.call_args[1]["config"]

        self.assertIsNone(result.exception)
        self.assertEqual("foo", config.output.package)
        self.assertEqual(OutputStructure.NAMESPACES, config.output.structure)
        file_path.unlink()
Esempio n. 4
0
    def test_init_config(self, mock_info):
        output = tempfile.mktemp()
        output_path = Path(output)
        result = self.runner.invoke(cli, ["init-config", str(output_path)])

        self.assertIsNone(result.exception)
        self.assertEqual(GeneratorConfig.create(),
                         GeneratorConfig.read(output_path))
        mock_info.assert_called_once_with("Initializing configuration file %s",
                                          str(output_path))
        output_path.unlink()
Esempio n. 5
0
    def test_from_config(self):
        config = GeneratorConfig()
        config.conventions.package_name.safe_prefix = "safe_package"
        config.conventions.package_name.case = NameCase.MIXED
        config.conventions.class_name.safe_prefix = "safe_class"
        config.conventions.class_name.case = NameCase.CAMEL
        config.conventions.field_name.safe_prefix = "safe_field"
        config.conventions.field_name.case = NameCase.PASCAL
        config.conventions.module_name.safe_prefix = "safe_module"
        config.conventions.module_name.case = NameCase.SNAKE
        config.aliases.class_name.append(GeneratorAlias("a", "b"))
        config.aliases.class_name.append(GeneratorAlias("c", "d"))
        config.aliases.field_name.append(GeneratorAlias("e", "f"))
        config.aliases.package_name.append(GeneratorAlias("g", "h"))
        config.aliases.module_name.append(GeneratorAlias("i", "j"))

        filters = Filters.from_config(config)

        self.assertEqual("safe_class", filters.class_safe_prefix)
        self.assertEqual("safe_field", filters.field_safe_prefix)
        self.assertEqual("safe_package", filters.package_safe_prefix)
        self.assertEqual("safe_module", filters.module_safe_prefix)

        self.assertEqual("cAb", filters.class_name("cAB"))
        self.assertEqual("CAb", filters.field_name("cAB", "cls"))
        self.assertEqual("cAB", filters.package_name("cAB"))
        self.assertEqual("c_ab", filters.module_name("cAB"))

        self.assertEqual({"a": "b", "c": "d"}, filters.class_aliases)
        self.assertEqual({"e": "f"}, filters.field_aliases)
        self.assertEqual({"g": "h"}, filters.package_aliases)
        self.assertEqual({"i": "j"}, filters.module_aliases)
Esempio n. 6
0
    def test_create(self):
        file_path = Path(tempfile.mktemp())
        version = get_distribution("xsdata").version
        obj = GeneratorConfig.create()
        with file_path.open("w") as fp:
            obj.write(fp, obj)

        expected = (
            '<?xml version="1.0" encoding="UTF-8"?>\n'
            f'<Config xmlns="http://pypi.org/project/xsdata" version="{version}">\n'
            '  <Output maxLineLength="79">\n'
            "    <Package>generated</Package>\n"
            "    <Format>dataclasses</Format>\n"
            "    <Structure>filenames</Structure>\n"
            "    <DocstringStyle>reStructuredText</DocstringStyle>\n"
            "    <CompoundFields>false</CompoundFields>\n"
            "  </Output>\n"
            "  <Conventions>\n"
            '    <ClassName case="pascalCase" safePrefix="type"/>\n'
            '    <FieldName case="snakeCase" safePrefix="value"/>\n'
            '    <ConstantName case="screamingSnakeCase" safePrefix="value"/>\n'
            '    <ModuleName case="snakeCase" safePrefix="mod"/>\n'
            '    <PackageName case="snakeCase" safePrefix="pkg"/>\n'
            "  </Conventions>\n"
            "  <Aliases>\n"
            '    <ClassName source="fooType" target="Foo"/>\n'
            '    <ClassName source="ABCSomething" target="ABCSomething"/>\n'
            '    <FieldName source="ChangeofGauge" target="change_of_gauge"/>\n'
            '    <PackageName source="http://www.w3.org/1999/xhtml" target="xtml"/>\n'
            '    <ModuleName source="2010.1" target="2020a"/>\n'
            "  </Aliases>\n"
            "</Config>\n")
        self.assertEqual(expected, file_path.read_text())
        file_path.unlink()
Esempio n. 7
0
    def test_init_config_when_file_exists(self, mock_info):
        output = tempfile.mktemp()
        output_path = Path(output).resolve()

        config = GeneratorConfig.create()
        config.version = "20.8"

        with output_path.open("w") as fp:
            config.write(fp, config)

        result = self.runner.invoke(cli, ["init-config", str(output_path)])

        self.assertIsNone(result.exception)
        self.assertNotEqual("20.8", GeneratorConfig.read(output_path))
        mock_info.assert_called_once_with("Updating configuration file %s",
                                          str(output_path))
        output_path.unlink()
Esempio n. 8
0
    def test_from_config(self):
        config = GeneratorConfig()
        config.output.format = OutputFormat.DATACLASS

        writer = CodeWriter.from_config(config)
        self.assertIsInstance(writer.generator, DataclassGenerator)

        config.output.format = OutputFormat.PLANTUML
        writer = CodeWriter.from_config(config)
        self.assertIsInstance(writer.generator, PlantUmlGenerator)
Esempio n. 9
0
def init_config(**kwargs: Any):
    """Create or update a configuration file."""

    if kwargs["print"]:
        logger.setLevel(logging.ERROR)

    file_path = Path(kwargs["output"])
    if file_path.exists():
        config = GeneratorConfig.read(file_path)
        logger.info("Updating configuration file %s", kwargs["output"])
    else:
        logger.info("Initializing configuration file %s", kwargs["output"])
        config = GeneratorConfig.create()

    if kwargs["print"]:
        config.write(sys.stdout, config)
    else:
        with file_path.open("w") as fp:
            config.write(fp, config)
Esempio n. 10
0
    def test_generate_with_configuration_file(self, mock_init, mock_process):
        file_path = Path(tempfile.mktemp())
        config = GeneratorConfig()
        config.output.package = "foo.bar"
        config.output.structure = OutputStructure.NAMESPACES
        with file_path.open("w") as fp:
            config.write(fp, config)

        source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd")
        result = self.runner.invoke(
            cli,
            [str(source), "--config", str(file_path)],
            catch_exceptions=False)
        config = mock_init.call_args[1]["config"]

        self.assertIsNone(result.exception)
        self.assertFalse(mock_init.call_args[1]["print"])
        self.assertEqual("foo.bar", config.output.package)
        self.assertEqual("dataclasses", config.output.format)
        self.assertEqual(OutputStructure.NAMESPACES, config.output.structure)
        self.assertEqual([source.as_uri()], mock_process.call_args[0][0])
        file_path.unlink()
Esempio n. 11
0
    def test_from_config(self):
        CodeWriter.unregister_generator("dataclasses")
        config = GeneratorConfig()

        with self.assertRaises(CodeGenerationError) as cm:
            CodeWriter.from_config(config)

        self.assertEqual("Unknown output format: 'dataclasses'",
                         str(cm.exception))

        CodeWriter.register_generator("dataclasses", DataclassGenerator)
        writer = CodeWriter.from_config(config)
        self.assertIsInstance(writer.generator, DataclassGenerator)
Esempio n. 12
0
    def test_read(self):
        existing = (
            '<?xml version="1.0" encoding="UTF-8"?>\n'
            '<Config xmlns="http://pypi.org/project/xsdata" version="20.8">\n'
            '  <Output maxLineLength="79">\n'
            "    <Package>foo.bar</Package>\n"
            "  </Output>\n"
            "  <Conventions>\n"
            '    <ClassName case="pascalCase" safePrefix="type"/>\n'
            "  </Conventions>\n"
            "  <Aliases/>\n"
            "</Config>\n")
        version = get_distribution("xsdata").version
        file_path = Path(tempfile.mktemp())
        file_path.write_text(existing, encoding="utf-8")
        config = GeneratorConfig.read(file_path)
        with file_path.open("w") as fp:
            GeneratorConfig.write(fp, config)

        expected = (
            '<?xml version="1.0" encoding="UTF-8"?>\n'
            f'<Config xmlns="http://pypi.org/project/xsdata" version="{version}">\n'
            '  <Output maxLineLength="79">\n'
            "    <Package>foo.bar</Package>\n"
            "    <Format>dataclasses</Format>\n"
            "    <Structure>filenames</Structure>\n"
            "    <DocstringStyle>reStructuredText</DocstringStyle>\n"
            "    <CompoundFields>false</CompoundFields>\n"
            "  </Output>\n"
            "  <Conventions>\n"
            '    <ClassName case="pascalCase" safePrefix="type"/>\n'
            '    <FieldName case="snakeCase" safePrefix="value"/>\n'
            '    <ConstantName case="screamingSnakeCase" safePrefix="value"/>\n'
            '    <ModuleName case="snakeCase" safePrefix="mod"/>\n'
            '    <PackageName case="snakeCase" safePrefix="pkg"/>\n'
            "  </Conventions>\n"
            "  <Aliases/>\n"
            "</Config>\n")
        self.assertEqual(expected, file_path.read_text())
Esempio n. 13
0
    def test_process(
        self,
        mock_validator_process,
        mock_container_process,
        mock_container_filter_classes,
        mock_sanitizer_process,
        mock_validate_references,
    ):
        config = GeneratorConfig()
        classes = ClassFactory.list(2)
        container = ClassContainer(config=config)
        container.extend(classes)

        result = ClassAnalyzer.process(container)

        self.assertEqual(classes, result)

        mock_validator_process.assert_called_once_with()
        mock_container_process.assert_called_once_with()
        mock_container_filter_classes.assert_called_once_with()
        mock_sanitizer_process.assert_called_once_with()
        mock_validate_references.assert_called_once_with(classes)
Esempio n. 14
0
def generate_schema(schema_filename, config_filename, outloc  ):

    from xsdata.codegen.transformer import SchemaTransformer
    from xsdata.exceptions import CodeGenerationError
    from xsdata.logger import logger
    from xsdata.models.config import GeneratorConfig
    from xsdata.models.config import OutputFormat
    from xsdata.models.config import  OutputStructure

    def to_uri(filename):
        return Path(filename).absolute().as_uri()

    subpackage_name = 'ismrmrdschema'
    logger.setLevel(logging.INFO)
    config = GeneratorConfig.read(Path(config_filename))
    config.output.format = OutputFormat("pydata")
    config.output.package = subpackage_name 
    transformer = SchemaTransformer(config=config,print=False)
    transformer.process_schemas([to_uri(schema_filename)])
    fix_init_file(subpackage_name,f"{subpackage_name}/__init__.py")
    shutil.rmtree(os.path.join(outloc,subpackage_name),ignore_errors=True)
    shutil.move(subpackage_name,outloc)
Esempio n. 15
0
    def setUp(self):
        super().setUp()

        self.config = GeneratorConfig()
        self.container = ClassContainer()
        self.sanitizer = ClassSanitizer(container=self.container, config=self.config)
Esempio n. 16
0
 def setUp(self):
     super().setUp()
     self.maxDiff = None
     config = GeneratorConfig()
     self.generator = DataclassGenerator(config)
Esempio n. 17
0
 def setUp(self):
     config = GeneratorConfig()
     self.generator = NoneGenerator(config)
     super().setUp()
Esempio n. 18
0
    os.system("git pull")
    os.chdir(current_path)
else:
    # fresh (sparse) checkout
    os.chdir(sdf_location)
    os.system(f"git clone {sdf_repo} --no-checkout . --depth 1")
    os.system("git sparse-checkout init --cone")
    os.system("git sparse-checkout set sdf")
    os.chdir(current_path)

# convert the SDF templates to XSD
xsd_location = Path("skbot/ignition/sdformat/schema/")
for version in sdf_versions:
    source_path = sdf_location / "sdf" / version
    out_dir = xsd_location / version
    out_dir.mkdir(exist_ok=True, parents=True)

    gen_bindings(source_path, out_dir, ns_prefix=f"sdformat/v{version}")

# build python bindings from XSD
config = GeneratorConfig.read(
    Path(__file__).parent / "sdf_bindings_config.xml")
for version in sdf_versions:
    config.output.package = (
        f"skbot.ignition.sdformat.bindings.v{version.replace('.', '')}")
    source_path = xsd_location / version
    uris = [x.absolute().as_uri() for x in source_path.iterdir()]
    if uris:
        tf = SchemaTransformer(print=False, config=config)
        tf.process(uris)
Esempio n. 19
0
 def setUp(self):
     config = GeneratorConfig()
     self.transformer = SchemaTransformer(print=True, config=config)
     super().setUp()
Esempio n. 20
0
 def setUp(self):
     super().setUp()
     config = GeneratorConfig()
     self.generator = PlantUmlGenerator(config)