source: framspy/framsfiles/_context.py @ 1254

Last change on this file since 1254 was 1104, checked in by Maciej Komosinski, 4 years ago

Added the "framsfiles" module for reading and writing Framsticks-format files (genotypes, settings, scrips, neurons, etc.)

File size: 1.7 KB
Line 
1import os.path
2from xml.etree import ElementTree
3
4_FRAMSCRIPT_XML_PATH = os.path.join((os.path.dirname(__file__)), '', 'framscript.xml')
5
6
7def _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
36def _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
48def _get_root():
49    return ElementTree.parse(_FRAMSCRIPT_XML_PATH).getroot()
50
51
52def _get_context(node):
53    return node.attrib['context']
54
55
56def _get_name(node):
57    return node.attrib['name']
58
59
60def _get_min(node, dtype):
61    return dtype(node.attrib['min'])
62
63
64def _get_max(node, dtype):
65    return dtype(node.attrib['max'])
66
67
68def _is_field(node):
69    return node.tag == 'element' and 'type' in node.attrib
70
71
72_specs, _contexts = _create_specs_from_xml()
Note: See TracBrowser for help on using the repository browser.