File: reset_attribute_sequences.py

package info (click to toggle)
python-xsdata 24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,936 kB
  • sloc: python: 29,257; xml: 404; makefile: 27; sh: 6
file content (36 lines) | stat: -rw-r--r-- 1,190 bytes parent folder | download
1
2
3
4
5
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
from xsdata.codegen.mixins import HandlerInterface
from xsdata.codegen.models import Attr, Class, get_restriction_sequence
from xsdata.utils import collections


class ResetAttributeSequences(HandlerInterface):
    """Validate if fields are part of a repeatable sequence otherwise reset the
    sequence flag."""

    __slots__ = ()

    def process(self, target: Class):
        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:
        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