def test_element_children(self): sequence_one = Sequence.create(elements=[Element.create(), Element.create()]) sequence_two = Sequence.create( max_occurs=2, elements=[Element.create(), Element.create()] ) restriction = Restriction.create( enumerations=[Enumeration.create(value=x) for x in "abc"], sequence=sequence_two, ) complex_type = ComplexType.create( attributes=[Attribute.create(), Attribute.create()], sequence=sequence_one, simple_content=SimpleContent.create(restriction=Restriction.create()), complex_content=ComplexContent.create(restriction=restriction,), ) children = self.builder.element_children(complex_type) 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))
def build_class_attribute(self, target: Class, obj: ElementBase, parent_restrictions: Restrictions): """Generate and append an attribute target to the target class.""" types = self.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.ns_map.update(obj.ns_map) 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=self.element_namespace(obj), restrictions=restrictions, ))
def element_children( self, obj: ElementBase, restrictions: Optional[Restrictions] = None ) -> Iterator[Tuple[AttributeElement, 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 or Restrictions() else: yield from self.element_children( child, restrictions=Restrictions.from_element(child) )