Skip to content

reset_attribute_sequences

xsdata.codegen.handlers.reset_attribute_sequences

ResetAttributeSequences

Bases: HandlerInterface

Inspect a class for non-repeatable choices and unset the sequence number.

Source code in xsdata/codegen/handlers/reset_attribute_sequences.py
 6
 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
class ResetAttributeSequences(HandlerInterface):
    """Inspect a class for non-repeatable choices and unset the sequence number."""

    __slots__ = ()

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

        Reset Cases:
            - A sequence only contains one attr
            - The sequence includes attrs with max_occurs==1

        Args:
            target: The target class instance
        """
        groups = collections.group_by(target.attrs, get_restriction_sequence)
        for sequence, attrs in groups.items():
            if not sequence:
                continue

            if len(attrs) == 1:
                attrs[0].restrictions.sequence = None
            else:
                for attr in attrs:
                    if not self.is_repeatable_sequence(attr):
                        attr.restrictions.sequence = None

    @classmethod
    def is_repeatable_sequence(cls, attr: Attr) -> bool:
        """Determine if the given attr is repeatable.

        Repeatable means max_occurs > 1

        Args:
            attr: The attr instance

        Returns:
            The bool result
        """
        seq = attr.restrictions.sequence
        if seq:
            for path in attr.restrictions.path:
                if path[0] == "s" and path[1] == seq:
                    return path[3] > 1 if path else False

                if path[3] > 1:
                    return True

        return False

process(target)

Process entrypoint for classes.

Reset Cases
  • A sequence only contains one attr
  • The sequence includes attrs with max_occurs==1

Parameters:

Name Type Description Default
target Class

The target class instance

required
Source code in xsdata/codegen/handlers/reset_attribute_sequences.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def process(self, target: Class):
    """Process entrypoint for classes.

    Reset Cases:
        - A sequence only contains one attr
        - The sequence includes attrs with max_occurs==1

    Args:
        target: The target class instance
    """
    groups = collections.group_by(target.attrs, get_restriction_sequence)
    for sequence, attrs in groups.items():
        if not sequence:
            continue

        if len(attrs) == 1:
            attrs[0].restrictions.sequence = None
        else:
            for attr in attrs:
                if not self.is_repeatable_sequence(attr):
                    attr.restrictions.sequence = None

is_repeatable_sequence(attr) classmethod

Determine if the given attr is repeatable.

Repeatable means max_occurs > 1

Parameters:

Name Type Description Default
attr Attr

The attr instance

required

Returns:

Type Description
bool

The bool result

Source code in xsdata/codegen/handlers/reset_attribute_sequences.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@classmethod
def is_repeatable_sequence(cls, attr: Attr) -> bool:
    """Determine if the given attr is repeatable.

    Repeatable means max_occurs > 1

    Args:
        attr: The attr instance

    Returns:
        The bool result
    """
    seq = attr.restrictions.sequence
    if seq:
        for path in attr.restrictions.path:
            if path[0] == "s" and path[1] == seq:
                return path[3] > 1 if path else False

            if path[3] > 1:
                return True

    return False