Parses an element in the schema.
(element_xml)
| 85 | |
| 86 | |
| 87 | def _parse_element(element_xml): |
| 88 | """Parses an <element> element in the schema.""" |
| 89 | name = element_xml.get('name') |
| 90 | if not name: |
| 91 | raise ValueError('Element must always have a name') |
| 92 | repeated = _str2bool(element_xml.get('repeated')) |
| 93 | on_demand = _str2bool(element_xml.get('on_demand')) |
| 94 | |
| 95 | attributes = collections.OrderedDict() |
| 96 | attributes_xml = element_xml.find('attributes') |
| 97 | if attributes_xml is not None: |
| 98 | for attribute_xml in attributes_xml.findall('attribute'): |
| 99 | attributes[attribute_xml.get('name')] = _parse_attribute(attribute_xml) |
| 100 | |
| 101 | identifier = None |
| 102 | namespace = None |
| 103 | for attribute_spec in attributes.values(): |
| 104 | if attribute_spec.type == attribute.Identifier: |
| 105 | identifier = attribute_spec.name |
| 106 | namespace = element_xml.get('namespace') or name |
| 107 | |
| 108 | children = collections.OrderedDict() |
| 109 | children_xml = element_xml.find('children') |
| 110 | if children_xml is not None: |
| 111 | for child_xml in children_xml.findall('element'): |
| 112 | children[child_xml.get('name')] = _parse_element(child_xml) |
| 113 | |
| 114 | element_spec = ElementSpec( |
| 115 | name, repeated, on_demand, identifier, namespace, attributes, children) |
| 116 | |
| 117 | recursive = _str2bool(element_xml.get('recursive')) |
| 118 | if recursive: |
| 119 | element_spec.children[name] = element_spec |
| 120 | |
| 121 | common_keys = set(element_spec.attributes).intersection(element_spec.children) |
| 122 | if common_keys: |
| 123 | raise RuntimeError( |
| 124 | 'Element \'{}\' contains the following attributes and children with ' |
| 125 | 'the same name: \'{}\'. This violates the design assumptions of ' |
| 126 | 'this library. Please file a bug report. Thank you.' |
| 127 | .format(name, sorted(common_keys))) |
| 128 | |
| 129 | return element_spec |
| 130 | |
| 131 | |
| 132 | def _parse_attribute(attribute_xml): |
no test coverage detected
searching dependent graphs…