File: reset_attribute_sequence_numbers.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 (37 lines) | stat: -rw-r--r-- 1,100 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
37
from collections import defaultdict

from xsdata.codegen.mixins import RelativeHandlerInterface
from xsdata.codegen.models import Class


class ResetAttributeSequenceNumbers(RelativeHandlerInterface):
    """
    Reset attributes sequence numbers.

    Until now all sequence numbers point to the id of sequence class!!!
    """

    __slots__ = ()

    def process(self, target: Class):
        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:
        return (
            max(
                (attr.restrictions.sequence or 0 for attr in self.base_attrs(target)),
                default=0,
            )
            + 1
        )