xsdata.codegen.sanitizer module

class xsdata.codegen.sanitizer.ClassSanitizer(container, config)[source]

Bases: object

Prepare all the classes attributes for code generation and cleanup after the analyzer processors.

Parameters
container: xsdata.codegen.container.ClassContainer
config: xsdata.models.config.GeneratorConfig
classmethod process(container, config)[source]

Iterate through all classes and run the sanitizer procedure.

Parameters
process_class(target)[source]

Sanitize the attributes of the given class. After applying all the flattening handlers the attributes need to be further sanitized to squash common issues like duplicate attribute names.

Steps:
  1. Sanitize inner classes

  2. Sanitize attributes default value

  3. Sanitize attributes name

  4. Sanitize attributes sequential flag

  5. Sanitize duplicate attribute names

Parameters

target (Class) –

group_compound_fields(target)[source]

Group and process target attributes by the choice group.

Parameters

target (Class) –

group_fields(target, attrs)[source]

Group attributes into a new compound field.

Parameters
process_attribute_default(target, attr)[source]

Sanitize attribute default value.

Cases:
  1. Ignore enumerations.

  2. List fields can not have a fixed value.

  3. Optional fields or xsi:type can not have a default or fixed value.

  4. Convert string literal default value for enum fields.

Parameters
process_attribute_default_enum(target, attr)[source]

Convert string literal default value for enum fields.

Loop through all attributes types and search for enum sources. If an enum source exist map the default string literal value to a qualified name. If the source class in inner promote it to root classes.

Parameters
promote_inner_class(parent, inner)[source]

Convert inner class to root class.

Steps:
  1. Remove inner class from parent

  2. Prepend parent name to inner class name.

  3. Search and replace all matching attribute types.

  4. Add inner class to the global class index.

Parameters
resolve_conflicts()[source]

Find classes with the same case insensitive qualified name and rename them.

rename_classes(classes)[source]

Rename all the classes in the list.

Protect classes derived from xs:element if there is only one in the list.

Parameters

classes (List[Class]) –

rename_class(target)[source]

Find the next available class identifier, save the original name in the class metadata and update the class qualified name and all classes that depend on the target class.

Parameters

target (Class) –

next_qname(namespace, name)[source]

Append the next available index number for the given namespace and local name.

Parameters
  • namespace (str) –

  • name (str) –

Return type

str

rename_dependency(target, search, replace)[source]

Search and replace the old qualified attribute type name with the new one if it exists in the target class attributes, extensions and inner classes.

Parameters
  • target (Class) –

  • search (str) –

  • replace (str) –

find_enum(source, attr_type)[source]

Find the enumeration source class for the given class and attribute type.

Search in root classes an inner class and exclude native types.

Parameters
Return type

Optional[Class]

classmethod process_attribute_restrictions(attr)[source]

Sanitize attribute required flag by comparing the min/max occurrences restrictions.

Parameters

attr (Attr) –

classmethod process_attribute_sequence(target, attr)[source]

Reset the attribute at the given index if it has no siblings with the sequential restriction.

Parameters
classmethod process_attribute_name(attr)[source]

Sanitize attribute name in preparation for duplicate attribute names handler.

Steps:
  1. Remove non alpha numerical values

  2. Handle Enum negative numerical values

  3. Remove namespaces prefixes

  4. Ensure name not empty

  5. Ensure name starts with a letter

Parameters

attr (Attr) –

classmethod process_duplicate_attribute_names(attrs)[source]

Sanitize duplicate attribute names that might exist by applying rename strategies.

Parameters

attrs (List[Attr]) –

Return type

None

classmethod rename_attributes_with_index(all_attrs, rename)[source]

Append the next available index number to all the rename attributes names.

Parameters
classmethod rename_attribute_by_preference(a, b)[source]

Decide and rename one of the two given attributes.

When both attributes are derived from the same xs:tag and one of the two fields has a specific namespace prepend it to the name. Preferable rename the second attribute.

Otherwise append the derived from tag to the name of one of the two attributes. Preferably rename the second field or the field derived from xs:attribute.

Parameters
classmethod build_attr_choice(attr)[source]

Converts the given attr to a choice.

The most important part is the reset of certain restrictions that don’t make sense as choice metadata like occurrences.

Parameters

attr (Attr) –

Return type

AttrChoice