Skip to content

flatten_attribute_groups

xsdata.codegen.handlers.flatten_attribute_groups

FlattenAttributeGroups

Bases: RelativeHandlerInterface

Replace groups and attGroups with the source class attributes.

Source code in xsdata/codegen/handlers/flatten_attribute_groups.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class FlattenAttributeGroups(RelativeHandlerInterface):
    """Replace groups and attGroups with the source class attributes."""

    __slots__ = ()

    def process(self, target: Class):
        """Iterate over all group attributes and apply handler logic.

        Group attributes can refer to attributes or other group
        attributes, repeat until there is no group attribute left.

        Args:
            target: The target class instance to inspect and process
        """
        repeat = False
        for attr in list(target.attrs):
            if attr.is_group:
                repeat = True
                self.process_attribute(target, attr)

        if repeat:
            self.process(target)

    def process_attribute(self, target: Class, attr: Attr):
        """Process a group/attributeGroup attr.

        Steps:
            1. Find the source class by the attr type and tag
            2. If the attr is circular reference, remove the attr
            3. Otherwise, copy all source attrs to the target class

        Args:
            target: The target class instance
            attr: The group attr to flatten

        Raises:
            AnalyzerValueError: if source class is not found.
        """
        qname = attr.types[0].qname  # group attributes have one type only.
        source = self.container.find(qname, condition=lambda x: x.tag == attr.tag)

        if not source:
            raise CodegenError("Unknown group reference", tag=attr.tag, qname=qname)

        if source is target:
            ClassUtils.remove_attribute(target, attr)
        else:
            is_circular_ref = source.status == Status.UNGROUPING
            ClassUtils.copy_group_attributes(source, target, attr, is_circular_ref)

process(target)

Iterate over all group attributes and apply handler logic.

Group attributes can refer to attributes or other group attributes, repeat until there is no group attribute left.

Parameters:

Name Type Description Default
target Class

The target class instance to inspect and process

required
Source code in xsdata/codegen/handlers/flatten_attribute_groups.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def process(self, target: Class):
    """Iterate over all group attributes and apply handler logic.

    Group attributes can refer to attributes or other group
    attributes, repeat until there is no group attribute left.

    Args:
        target: The target class instance to inspect and process
    """
    repeat = False
    for attr in list(target.attrs):
        if attr.is_group:
            repeat = True
            self.process_attribute(target, attr)

    if repeat:
        self.process(target)

process_attribute(target, attr)

Process a group/attributeGroup attr.

Steps
  1. Find the source class by the attr type and tag
  2. If the attr is circular reference, remove the attr
  3. Otherwise, copy all source attrs to the target class

Parameters:

Name Type Description Default
target Class

The target class instance

required
attr Attr

The group attr to flatten

required

Raises:

Type Description
AnalyzerValueError

if source class is not found.

Source code in xsdata/codegen/handlers/flatten_attribute_groups.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def process_attribute(self, target: Class, attr: Attr):
    """Process a group/attributeGroup attr.

    Steps:
        1. Find the source class by the attr type and tag
        2. If the attr is circular reference, remove the attr
        3. Otherwise, copy all source attrs to the target class

    Args:
        target: The target class instance
        attr: The group attr to flatten

    Raises:
        AnalyzerValueError: if source class is not found.
    """
    qname = attr.types[0].qname  # group attributes have one type only.
    source = self.container.find(qname, condition=lambda x: x.tag == attr.tag)

    if not source:
        raise CodegenError("Unknown group reference", tag=attr.tag, qname=qname)

    if source is target:
        ClassUtils.remove_attribute(target, attr)
    else:
        is_circular_ref = source.status == Status.UNGROUPING
        ClassUtils.copy_group_attributes(source, target, attr, is_circular_ref)