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))
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))
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()
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()
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)
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())
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)
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)