Parses an element in the schema.
(attribute_xml)
| 130 | |
| 131 | |
| 132 | def _parse_attribute(attribute_xml): |
| 133 | """Parses an <attribute> element in the schema.""" |
| 134 | name = attribute_xml.get('name') |
| 135 | required = _str2bool(attribute_xml.get('required')) |
| 136 | conflict_allowed = _str2bool(attribute_xml.get('conflict_allowed')) |
| 137 | conflict_behavior = attribute_xml.get('conflict_behavior', 'replace') |
| 138 | attribute_type = attribute_xml.get('type') |
| 139 | other_kwargs = {} |
| 140 | if attribute_type == 'keyword': |
| 141 | attribute_callable = attribute.Keyword |
| 142 | other_kwargs['valid_values'] = attribute_xml.get('valid_values').split(' ') |
| 143 | elif attribute_type == 'array': |
| 144 | array_size_str = attribute_xml.get('array_size') |
| 145 | attribute_callable = attribute.Array |
| 146 | other_kwargs['length'] = int(array_size_str) if array_size_str else None |
| 147 | other_kwargs['dtype'] = _ARRAY_DTYPE_MAP[attribute_xml.get('array_type')] |
| 148 | elif attribute_type == 'identifier': |
| 149 | attribute_callable = attribute.Identifier |
| 150 | elif attribute_type == 'reference': |
| 151 | attribute_callable = attribute.Reference |
| 152 | other_kwargs['reference_namespace'] = ( |
| 153 | attribute_xml.get('reference_namespace') or name) |
| 154 | elif attribute_type == 'basepath': |
| 155 | attribute_callable = attribute.BasePath |
| 156 | other_kwargs['path_namespace'] = attribute_xml.get('path_namespace') |
| 157 | elif attribute_type == 'file': |
| 158 | attribute_callable = attribute.File |
| 159 | other_kwargs['path_namespace'] = attribute_xml.get('path_namespace') |
| 160 | else: |
| 161 | try: |
| 162 | attribute_callable = _SCALAR_TYPE_MAP[attribute_type] |
| 163 | except KeyError as exc: |
| 164 | raise ValueError( |
| 165 | 'Invalid attribute type: {}'.format(attribute_type) |
| 166 | ) from exc |
| 167 | |
| 168 | return AttributeSpec( |
| 169 | name=name, type=attribute_callable, required=required, |
| 170 | conflict_allowed=conflict_allowed, conflict_behavior=conflict_behavior, |
| 171 | other_kwargs=other_kwargs) |
| 172 | |
| 173 | |
| 174 | def collect_namespaces(root_spec): |
no test coverage detected
searching dependent graphs…