Beispiel #1
0
 def test_element_children(self):
     sequence_one = Sequence(elements=[Element(), Element()])
     sequence_two = Sequence(max_occurs=2, elements=[Element(), Element()])
     restriction = Restriction(
         enumerations=[Enumeration(value=x) for x in "abc"], sequence=sequence_two,
     )
     complex_type = ComplexType(
         attributes=[Attribute(), Attribute()],
         sequence=sequence_one,
         simple_content=SimpleContent(restriction=Restriction()),
         complex_content=ComplexContent(restriction=restriction,),
     )
     restrictions = Restrictions.from_element(complex_type)
     children = self.builder.element_children(complex_type, restrictions)
     expected = [
         (sequence_two.elements[0], Restrictions.from_element(sequence_two)),
         (sequence_two.elements[1], Restrictions.from_element(sequence_two)),
         (restriction.enumerations[0], Restrictions.from_element(restriction)),
         (restriction.enumerations[1], Restrictions.from_element(restriction)),
         (restriction.enumerations[2], Restrictions.from_element(restriction)),
         (sequence_one.elements[0], Restrictions.from_element(sequence_one)),
         (sequence_one.elements[1], Restrictions.from_element(sequence_one)),
         (complex_type.attributes[0], Restrictions.from_element(complex_type)),
         (complex_type.attributes[1], Restrictions.from_element(complex_type)),
     ]
     self.assertIsInstance(children, GeneratorType)
     self.assertEqual(expected, list(children))
Beispiel #2
0
    def build_class_attribute(
        cls, target: Class, obj: ElementBase, parent_restrictions: Restrictions
    ):
        """Generate and append an attribute field to the target class."""

        target.ns_map.update(obj.ns_map)
        types = cls.build_class_attribute_types(target, obj)
        restrictions = Restrictions.from_element(obj)

        if obj.class_name in (Tag.ELEMENT, Tag.ANY):
            restrictions.merge(parent_restrictions)

        if restrictions.prohibited:
            return

        name = obj.real_name
        target.attrs.append(
            Attr(
                index=obj.index,
                name=name,
                local_name=name,
                default=obj.default_value,
                fixed=obj.is_fixed,
                types=types,
                tag=obj.class_name,
                help=obj.display_help,
                namespace=cls.element_namespace(obj, target.qname.namespace),
                restrictions=restrictions,
            )
        )
Beispiel #3
0
    def build_class_attributes(cls, obj: ElementBase, target: Class):
        """Build the target class attributes from the given ElementBase
        children."""

        restrictions = Restrictions.from_element(obj)
        for child, restrictions in cls.element_children(obj, restrictions):
            cls.build_class_attribute(target, child, restrictions)

        target.attrs.sort(key=lambda x: x.index)
Beispiel #4
0
    def element_children(
        cls, obj: ElementBase, restrictions: Restrictions
    ) -> Iterator[Tuple[ElementBase, Restrictions]]:
        """Recursively find and return all child elements that are qualified to
        be class attributes."""

        for child in obj.children():
            if child.is_attribute:
                yield child, restrictions
            else:
                yield from cls.element_children(
                    child, restrictions=Restrictions.from_element(child)
                )
Beispiel #5
0
    def test_element_children_with_parents_restrictions(self):
        choice = Choice(elements=[Element(name="elem1")])
        complex_type = ComplexType(
            sequence=Sequence(choices=[choice], min_occurs=0, max_occurs=3))
        parent_restrictions = Restrictions.from_element(complex_type)
        children = SchemaMapper.element_children(complex_type,
                                                 parent_restrictions)

        child, restrictions = next(children)
        expected = Restrictions(min_occurs=0,
                                max_occurs=3,
                                sequential=True,
                                choice=str(id(choice)))
        self.assertEqual(expected, restrictions)