Code Generator¶
$ xsdata --help
Usage: xsdata [OPTIONS] COMMAND [ARGS]...
Options:
--version Show the version and exit.
-v, --verbosity LVL Either CRITICAL, ERROR, WARNING, INFO or DEBUG
--help Show this message and exit.
Commands:
generate* Convert schema definitions to code.
init-config Create or update a configuration file.
Generate¶
$ xsdata generate --help
Usage: xsdata generate [OPTIONS] SOURCE
Convert schema definitions to code.
SOURCE can be either a filepath, directory or url
Options:
--config TEXT Configuration file
--package TEXT Target Package
--output [pydata|plantuml] Output Format
--wsdl WSDL Mode (experimental)
--print Print output
--ns-struct Use namespaces to group classes in the same
module. Useful against circular import errors.
--help Show this message and exit.
Generate is the default subcommand of the command line interface and can be omitted.
$ xsdata amadeus/schemas --package amadeus.models
$ xsdata air_v48_0/AirReqRsp.xsd rail_v48_0/RailReqRsp.xsd --package travelport.models
$ xsdata http://www.gstatic.com/localfeed/local_feed.xsd --package feeds --print
SOURCE¶
You can pass the file path or uri to a schema or a whole directory with multiple definitions.
config¶
Specify a configuration file with more advance options.
package¶
Specify where the target module(s) will be created inside the current working directory. eg –package api.models
Note
The cli is relying on the os.path.commonpath of the schemas locations to create the final package structure. If you prefer a more flat structure or you have circular import errors check the option ns-struct.
output¶
Specify the output format
pydata
: Python lib dataclassesplantuml
: PlantUML class diagram
verbosity¶
Specify the log level, default is INFO
Available options: CRITICAL
, ERROR
, WARNING
, INFO
or DEBUG
wsdl¶
Generate models and services from a wsdl source.
Experimental
This feature is experimental and only a small subset of features have been implemented, WSDL 1.1 & SOAP 1.1 bindings.
print¶
Redirect generated code to stdOut instead of writing the output to the target files.
ns-struct¶
Group classes by the target namespace they were defined. This option creates a more flat package structure and solves many circular import errors.
Configuration¶
Alternatively to the cli flags and options you can provide a project
xsdata.models.config
document.
$ xsdata amadeus/schemas --config amadeus/.xsdata.xml
The configuration offers more advance options to further tail the output to your needs, like naming conventions and aliases.
$ xsdata init-config --help
Usage: xsdata init-config [OPTIONS] [OUTPUT]
Create or update a configuration file.
Options:
--print Print output
--help Show this message and exit.
$ xsdata init-config --print
<?xml version='1.0' encoding='UTF-8'?>
<Config xmlns="http://pypi.org/project/xsdata" version="20.10">
<Output wsdl="false">
<Package>generated</Package>
<Format>pydata</Format>
<Structure>filenames</Structure>
</Output>
<Conventions>
<ClassName case="pascalCase" safePrefix="type"/>
<FieldName case="snakeCase" safePrefix="value"/>
<ModuleName case="snakeCase" safePrefix="mod"/>
<PackageName case="snakeCase" safePrefix="pkg"/>
</Conventions>
<Aliases>
<ClassName source="fooType" target="Foo"/>
<ClassName source="ABCSomething" target="ABCSomething"/>
<FieldName source="ChangeofGauge" target="change_of_gauge"/>
<PackageName source="http://www.w3.org/1999/xhtml" target="xtml"/>
<ModuleName source="2010.1" target="2020a"/>
</Aliases>
</Config>
Hint
The sample output has the default naming conventions and some example aliases.
Conventions¶
Case schemes
All schemes are using a processor that splits a string into words when it encounters non alphanumerical characters or when an upper case letter follows a lower case letter.
Original |
Pascal Case |
Camel Case |
Snake Case |
Mixed Case |
Mixed Snake Case |
---|---|---|---|---|---|
p00p |
P00P |
p00P |
p00p |
p00p |
p00p |
USERName |
Username |
username |
username |
USERName |
USERName |
UserNAME |
UserName |
userName |
user_name |
UserNAME |
User_NAME |
USER_name |
UserName |
userName |
user_name |
USERname |
USER_name |
USER-NAME |
UserName |
userName |
user_name |
USERNAME |
USER_NAME |
User_Name |
UserName |
userName |
user_name |
UserName |
User_Name |
user_name |
UserName |
userName |
user_name |
username |
user_name |
SUserNAME |
SuserName |
suserName |
suser_name |
SUserNAME |
SUser_NAME |
Hint
The mixed case is joining the words without changing the original upper/lower case
The mixed underscore case is joining the words with an underscore.
The pascal/camel cases are using python’s str.title()
method!
Safe Prefixes
They are used to neutralize classes and fields names that match reserved keywords.
and, except, lambda, with, as, finally, nonlocal, while, assert, false, none,
yield, break, for, not, class, from, or, continue, global, pass, def, if, raise,
del, import, return, elif, in, true, else, is, try, str, int, bool, float, list,
optional, dict, field
Aliases¶
The aliases allow users to override global naming conventions for class, fields package and module names. Each alias has a source attribute that refers to the original type name in the schema definition and the target attribute for output name.
For package and module aliases the source refers to the schemas filenames or target namespaces depending the output structure strategy selected.
Danger
The generator doesn’t validate user defined target names.
Examples
Check the Demos and the samples repo for more ✨✨✨