- for element_element in tree.getiterator('element'):
- raw_attrs = element_element.attrib
-
- element_name = '%sElement' % raw_attrs.get('cppname', raw_attrs.get('name'))
- element_level = int(raw_attrs['level'])
- element_attrs = {
- '__module__': None,
- 'class_id': int(raw_attrs['id'], 0),
- 'class_name': raw_attrs['name'],
- 'data_type': SPECDATA_TYPES[raw_attrs['type']]
- }
-
- while parent_elements and element_level <= parent_elements[-1][0]:
- parent_elements.pop()
-
- if element_level == -1:
- element_attrs['class_global'] = True
- parent_elements = []
- elif element_level == 0:
- element_attrs['class_root'] = True
- parent_elements = []
- else:
- if raw_attrs.get('recursive', '0') == '1':
- element_attrs['class_parents'] = (parent_elements[-1][1], 'self')
+ def child_elements(parent_level, element_list, upper_recursive=None):
+ children = []
+ while element_list:
+ raw_element = element_list[0]
+ raw_attrs = raw_element.attrib
+
+ element_level = int(raw_attrs['level'])
+
+ is_global = False
+ if element_level == -1:
+ is_global = True
+ elif parent_level is not None and not element_level > parent_level:
+ break
+ element_list = element_list[1:]
+
+ element_name = '%sElement' % raw_attrs.get('cppname', raw_attrs.get('name')).translate(None, '-')
+ element_attrs = {
+ '__module__': None,
+ 'id': int(raw_attrs['id'], 0),
+ 'name': raw_attrs['name'],
+ 'type': SPECDATA_TYPES[raw_attrs['type']],
+ 'mandatory': True if raw_attrs.get('mandatory', False) == '1' else False,
+ 'multiple': True if raw_attrs.get('multiple', False) == '1' else False
+ }
+ try:
+ element_attrs['default'] = {
+ INT: lambda default: int(default),
+ UINT: lambda default: int(default),
+ FLOAT: lambda default: float(default),
+ STRING: lambda default: str(default),
+ UNICODE: lambda default: unicode(default)
+ }.get(element_attrs['type'], lambda default: default)(raw_attrs['default'])
+ except (KeyError, ValueError):
+ element_attrs['default'] = None
+
+ element = type(element_name, (Element,), element_attrs)
+ elements[element_name] = element
+
+ recursive = []
+ if upper_recursive:
+ recursive.extend(upper_recursive)
+ if raw_attrs.get('recursive', False) == '1':
+ recursive.append(element)
+
+ element_children, element_list = child_elements(element_level if not is_global else 0, element_list, recursive)
+ element_children += tuple(recursive)
+ element.children = element_children
+
+ if is_global:
+ globals.append(element)