-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatalisp2python.py
More file actions
executable file
·68 lines (49 loc) · 1.57 KB
/
datalisp2python.py
File metadata and controls
executable file
·68 lines (49 loc) · 1.57 KB
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
import pydatalisp as dl
class Exporter:
def __init__(self, container, asDict=False):
self.container = container
self.asDict = asDict
def _fromData(self, data):
if data.type == dl.Type.Group:
return self._fromGroup(data.group)
elif data.type == dl.Type.String:
return data.string
elif data.type == dl.Type.Integer:
return data.int
elif data.type == dl.Type.Float:
return data.float
elif data.type == dl.Type.Bool:
return data.bool
else:
return None
def _fromGroup(self, grp):
d = dict()
for data in grp.namedEntries:
d[data.key] = self._fromData(data)
for i in range(grp.anonymousCount):
d[i] = self._fromData(grp.at(i))
if grp.isArray:
return d
elif self.asDict:
d['__id__'] = grp.id
return d
else:
return type(grp.id, (), d)()
def run(self):
d = dict()
for grp in self.container.topGroups:
d[grp.id] = self._fromGroup(grp)
return d
def run(container, asDict=False):
exp = Exporter(container, asDict)
return exp.run()
def runFile(path):
sourcelogger = dl.SourceLogger()
datalisp = dl.DataLisp(sourcelogger)
with open(input, 'r') as f:
datalisp.parse(f.read())
if sourcelogger.errorCount > 0:
raise Exception("Error while parsing", input)
container = dl.DataContainer()
datalisp.build(container)
return run(container)