1 from xml.etree.ElementTree import parse as parse_xml
2 from .base import INT, UINT, FLOAT, STRING, UNICODE, DATE, BINARY, CONTAINER, Element, Schema
17 def parse_specdata(source, schema_name):
20 Reads a schema specification from a file (e.g., specdata.xml) or file-like object, and returns a tuple containing:
22 * a mapping of class names to Element subclasses
25 :arg source: the file or file-like object
26 :type source: str or file-like object
27 :arg schema_name: the name of the schema
28 :type schema_name: str
33 tree = parse_xml(source)
37 for element_element in tree.getiterator('element'):
38 raw_attrs = element_element.attrib
40 element_name = '%sElement' % raw_attrs.get('cppname', raw_attrs.get('name'))
41 element_level = int(raw_attrs['level'])
44 'class_id': int(raw_attrs['id'], 0),
45 'class_name': raw_attrs['name'],
46 'data_type': SPECDATA_TYPES[raw_attrs['type']]
49 while parent_elements and element_level <= parent_elements[-1][0]:
52 if element_level == -1:
53 element_attrs['class_global'] = True
55 elif element_level == 0:
56 element_attrs['class_root'] = True
59 if raw_attrs.get('recursive', '0') == '1':
60 element_attrs['class_parents'] = (parent_elements[-1][1], 'self')
62 element_attrs['class_parents'] = (parent_elements[-1][1],)
64 element = type(element_name, (Element,), element_attrs)
65 elements[element_name] = element
66 parent_elements.append((element_level, element))
70 'elements': tuple(elements.values())
72 schema = type(schema_name, (Schema,), schema_attrs)
74 return elements, schema