xsdata.formats.dataclass.filters module

class xsdata.formats.dataclass.filters.Filters(class_aliases=<factory>, field_aliases=<factory>, package_aliases=<factory>, module_aliases=<factory>, class_case=<function pascal_case>, field_case=<function snake_case>, package_case=<function snake_case>, module_case=<function snake_case>, class_safe_prefix='type', field_safe_prefix='value', package_safe_prefix='pkg', module_safe_prefix='mod', docstring_style=<DocstringStyle.RST: 'reStructuredText'>, max_line_length=79)[source]

Bases: object

Parameters
  • class_aliases (Dict) –

  • field_aliases (Dict) –

  • package_aliases (Dict) –

  • module_aliases (Dict) –

  • class_case (Callable) –

  • field_case (Callable) –

  • package_case (Callable) –

  • module_case (Callable) –

  • class_safe_prefix (str) –

  • field_safe_prefix (str) –

  • package_safe_prefix (str) –

  • module_safe_prefix (str) –

  • docstring_style (DocstringStyle) –

  • max_line_length (int) –

class_aliases: Dict
field_aliases: Dict
package_aliases: Dict
module_aliases: Dict
class_case()

Convert the given string to pascal case.

Parameters

string (str) –

Return type

str

field_case()

Convert the given string to snake case.

Parameters

string (str) –

Return type

str

package_case()

Convert the given string to snake case.

Parameters

string (str) –

Return type

str

module_case()

Convert the given string to snake case.

Parameters

string (str) –

Return type

str

class_safe_prefix: str = 'type'
field_safe_prefix: str = 'value'
package_safe_prefix: str = 'pkg'
module_safe_prefix: str = 'mod'
docstring_style: xsdata.models.config.DocstringStyle = 'reStructuredText'
max_line_length: int = 79
cache: Dict
register(env)[source]
Parameters

env (Environment) –

class_name(name)[source]

Convert the given string to a class name according to the selected conventions or use an existing alias.

Parameters

name (str) –

Return type

str

class_params(obj)[source]
Parameters

obj (Class) –

field_name(name)[source]

Convert the given string to a field name according to the selected conventions or use an existing alias.

Parameters

name (str) –

Return type

str

constant_name(name)[source]

Apply python conventions for constant names.

Parameters

name (str) –

Return type

str

module_name(name)[source]

Convert the given string to a module name according to the selected conventions or use an existing alias.

Parameters

name (str) –

Return type

str

package_name(name)[source]

Convert the given string to a package name according to the selected conventions or use an existing alias.

Parameters

name (str) –

Return type

str

type_name(attr_type)[source]

Return native python type name or apply class name conventions.

Parameters

attr_type (AttrType) –

Return type

str

field_metadata(attr, parent_namespace, parents)[source]

Return a metadata dictionary for the given attribute.

Parameters
Return type

Dict

field_choices(attr, parent_namespace, parents)[source]

Return a list of metadata dictionaries for the choices of the given attribute.

Return None if attribute has no choices.

Parameters
Return type

Optional[List]

classmethod filter_metadata(data)[source]
Parameters

data (Dict) –

Return type

Dict

format_metadata(data, indent=0, key='')[source]

Prettify field metadata for code generation.

Parameters
  • data (Any) –

  • indent (int) –

  • key (str) –

Return type

str

format_dict(data, indent)[source]

Return a pretty string representation of a dict.

Parameters
Return type

str

format_iterable(data, indent)[source]

Return a pretty string representation of an iterable.

Parameters
Return type

str

format_string(data, indent, key='', pad=0)[source]

Return a pretty string representation of a string.

If the total length of the input string plus indent plus the key length and the additional pad is more than the max line length, wrap the text into multiple lines, avoiding breaking long words

Parameters
  • data (str) –

  • indent (int) –

  • key (str) –

  • pad (int) –

Return type

str

text_wrap(string, offset=0)[source]

Wrap text in respect to the max line length and the given offset.

Parameters
  • string (str) –

  • offset (int) –

Return type

str

classmethod text_strip(string)[source]

Remove all whitespace from every line of the string.

Parameters

string (Optional[str]) –

Return type

str

format_docstring(doc_string, level)[source]

Format doc strings.

Parameters
  • doc_string (str) –

  • level (int) –

Return type

str

field_default_value(attr, ns_map=None)[source]

Generate the field default value/factory for the given attribute.

Parameters
Return type

Any

field_default_enum(attr)[source]
Parameters

attr (Attr) –

Return type

str

field_default_tokens(attr, types)[source]
Parameters
Return type

str

field_type(attr, parents)[source]

Generate type hints for the given attribute.

Parameters
Return type

str

choice_type(choice, parents)[source]

Generate type hints for the given choice.

Choices support a subset of features from normal attributes. First of all we don’t have a proper type hint but a type metadata key. That’s why we always need to wrap as Type[xxx]. The second big difference is that our choice belongs to a compound field that might be a list, that’s why list restriction is also ignored.

Parameters
Return type

str

field_type_name(attr_type, parents)[source]
Parameters
Return type

str

constant_value(attr)[source]

Return the attr default value or type as constant value.

Parameters

attr (Attr) –

Return type

str

classmethod prepare_default_value(value)[source]
Parameters

value (Any) –

Return type

Any

classmethod type_is_included(output, type_name)[source]
Parameters
  • output (str) –

  • type_name (str) –

Return type

bool

classmethod default_imports(output)[source]

Generate the default imports for the given package output.

Parameters

output (str) –

Return type

str

classmethod from_config(config)[source]
Parameters

config (GeneratorConfig) –

Return type

Filters