/
format.py
105 lines (89 loc) · 2.19 KB
/
format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import odml
"""
A module providing general format information
and mappings of xml-attributes to their python class equivalents
"""
class Format(object):
_map = {}
_rev_map = None
def map(self, name):
"""maps an odml name to a python name"""
return self._map.get(name, name)
def revmap(self, name):
"""maps a python name to an odml name"""
if self._rev_map is None:
# create the reverse map only if requested
self._rev_map = {}
for k, v in self._map.iteritems():
self._rev_map[v] = k
return self._rev_map.get(name, name)
def __iter__(self):
"""iterates each python property name"""
for k in self._args:
yield self.map(k)
def create(self, *args, **kargs):
return getattr(odml, self.__class__.__name__)(*args, **kargs)
class Value(Format):
_name = "value"
_args = {
'value': 0,
'uncertainty': 0,
'unit': 0,
'type': 0,
'definition': 0,
'reference': 0,
'filename': 0,
'checksum': 0,
'encoder': 0
}
_map = {'type': 'dtype'}
class Property(Format):
_name = "property"
_args = {
'name': 1,
'value': 1,
'definition': 0,
'mapping': 0,
'dependency': 0,
'dependencyvalue': 0
}
_map = {
'value': 'values',
'dependencyvalue': 'dependency_value',
'type': 'dtype'
}
class Section(Format):
_name = "section"
_args = {
'type': 1,
'name': 0,
'definition': 0,
'reference': 0,
'link': 0,
'repository': 0,
'mapping': 0,
'section': 0,
'include': 0,
'property': 0
}
_map = {
'section': 'sections',
'property': 'properties',
}
class Document(Format):
_name = "odML"
_args = {
'version': 0,
'author': 0,
'date': 0,
'section': 0,
'repository': 0,
}
_map = {
'section': 'sections'
}
Document = Document()
Section = Section()
Value = Value()
Property = Property()
__all__ = [Document, Section, Property, Value]