1 | import os.path |
---|
2 | from xml.etree import ElementTree |
---|
3 | |
---|
4 | _FRAMSCRIPT_XML_PATH = os.path.join((os.path.dirname(__file__)), '', 'framscript.xml') |
---|
5 | |
---|
6 | |
---|
7 | def _create_specs_from_xml(): |
---|
8 | contexts = {'sim file', 'gen file'} |
---|
9 | specs = dict() |
---|
10 | for child in _get_root(): |
---|
11 | context = _get_context(child) |
---|
12 | classname = _get_name(child) |
---|
13 | context_key = (context, classname) |
---|
14 | specs[context_key] = dict() |
---|
15 | contexts.add(context) |
---|
16 | |
---|
17 | for element in child: |
---|
18 | if _is_field(element): |
---|
19 | spec = dict() |
---|
20 | key = _get_name(element) |
---|
21 | dtype = _get_type(element) |
---|
22 | if dtype is None: |
---|
23 | continue |
---|
24 | |
---|
25 | if 'min' in element.attrib: |
---|
26 | spec['min'] = _get_min(element, dtype) |
---|
27 | if 'max' in element.attrib: |
---|
28 | spec['max'] = _get_max(element, dtype) |
---|
29 | |
---|
30 | spec['dtype'] = dtype |
---|
31 | specs[context_key][key] = spec |
---|
32 | |
---|
33 | return specs, contexts |
---|
34 | |
---|
35 | |
---|
36 | def _get_type(node): |
---|
37 | # possible types as of 1.03.2021: {'float', 'untyped', 'integer', 'string', 'text', 'void'} |
---|
38 | type_att = node.attrib['type'] |
---|
39 | if type_att == 'string': |
---|
40 | return str |
---|
41 | if type_att == 'integer': |
---|
42 | return int |
---|
43 | if type_att == 'float': |
---|
44 | return float |
---|
45 | return None |
---|
46 | |
---|
47 | |
---|
48 | def _get_root(): |
---|
49 | return ElementTree.parse(_FRAMSCRIPT_XML_PATH).getroot() |
---|
50 | |
---|
51 | |
---|
52 | def _get_context(node): |
---|
53 | return node.attrib['context'] |
---|
54 | |
---|
55 | |
---|
56 | def _get_name(node): |
---|
57 | return node.attrib['name'] |
---|
58 | |
---|
59 | |
---|
60 | def _get_min(node, dtype): |
---|
61 | return dtype(node.attrib['min']) |
---|
62 | |
---|
63 | |
---|
64 | def _get_max(node, dtype): |
---|
65 | return dtype(node.attrib['max']) |
---|
66 | |
---|
67 | |
---|
68 | def _is_field(node): |
---|
69 | return node.tag == 'element' and 'type' in node.attrib |
---|
70 | |
---|
71 | |
---|
72 | _specs, _contexts = _create_specs_from_xml() |
---|