Skip to content

reset_attribute_sequence_numbers

xsdata.codegen.handlers.reset_attribute_sequence_numbers

ResetAttributeSequenceNumbers

Bases: RelativeHandlerInterface

Reset attrs sequence numbers.

The sequence numbers are the ids of xs:sequence elements, because up until now it was important to determine which child/parent attrs belong to different sequence numbers.

Before we generate the classes let's reset them to simple auto increment numbers per class.

Source code in xsdata/codegen/handlers/reset_attribute_sequence_numbers.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
class ResetAttributeSequenceNumbers(RelativeHandlerInterface):
    """Reset attrs sequence numbers.

    The sequence numbers are the ids of xs:sequence elements, because
    up until now it was important to determine which child/parent
    attrs belong to different sequence numbers.

    Before we generate the classes let's reset them to simple auto
    increment numbers per class.
    """

    __slots__ = ()

    def process(self, target: Class):
        """Process entrypoint for classes.

        Args:
            target: The target class instance
        """
        groups = defaultdict(list)
        for attr in target.attrs:
            if attr.restrictions.sequence:
                groups[attr.restrictions.sequence].append(attr)

        if groups:
            next_sequence_number = self.find_next_sequence_number(target)
            for attrs in groups.values():
                for attr in attrs:
                    attr.restrictions.sequence = next_sequence_number

                next_sequence_number += 1

    def find_next_sequence_number(self, target: Class) -> int:
        """Calculate the next sequence number from the base classes.

        Args:
            target: The target class instance

        Returns:
            The next sequence number
        """
        base_attrs = self.base_attrs(target)
        sequences = (attr.restrictions.sequence or 0 for attr in base_attrs)
        max_sequence = max(sequences, default=0)
        return max_sequence + 1

process(target)

Process entrypoint for classes.

Parameters:

Name Type Description Default
target Class

The target class instance

required
Source code in xsdata/codegen/handlers/reset_attribute_sequence_numbers.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def process(self, target: Class):
    """Process entrypoint for classes.

    Args:
        target: The target class instance
    """
    groups = defaultdict(list)
    for attr in target.attrs:
        if attr.restrictions.sequence:
            groups[attr.restrictions.sequence].append(attr)

    if groups:
        next_sequence_number = self.find_next_sequence_number(target)
        for attrs in groups.values():
            for attr in attrs:
                attr.restrictions.sequence = next_sequence_number

            next_sequence_number += 1

find_next_sequence_number(target)

Calculate the next sequence number from the base classes.

Parameters:

Name Type Description Default
target Class

The target class instance

required

Returns:

Type Description
int

The next sequence number

Source code in xsdata/codegen/handlers/reset_attribute_sequence_numbers.py
39
40
41
42
43
44
45
46
47
48
49
50
51
def find_next_sequence_number(self, target: Class) -> int:
    """Calculate the next sequence number from the base classes.

    Args:
        target: The target class instance

    Returns:
        The next sequence number
    """
    base_attrs = self.base_attrs(target)
    sequences = (attr.restrictions.sequence or 0 for attr in base_attrs)
    max_sequence = max(sequences, default=0)
    return max_sequence + 1