xsdata.codegen.sanitizer module

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

Bases: object

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

Parameters

container (ClassContainer) –

container: ClassContainer
classmethod process(container)[source]

Iterate through all classes and run the sanitizer procedure.

Parameters

container (ClassContainer) –

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

process_attribute_default(target, attr)[source]

Sanitize attribute default value.

Cases:
  1. List fields can not have a fixed value.

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

  3. 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
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.

Steps:

1. If more than two attributes share the same name or if they are enumerations append a numerical index to the attribute names. 2. If one of the two fields has a specific namespace prepend it to the name. If possible rename the second field. 3. Append the xml type to the name of one of the two attributes. if possible rename the second field or the field with xml type attribute.

Parameters

attrs (List[Attr]) –

Return type

None

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 find_inner(target, condition)[source]

Find the first inner class that matches the given condition.

Parameters
Return type

Optional[Class]