-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.py
68 lines (54 loc) · 1.76 KB
/
parser.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
class GenesisParserError(KeyError):
pass
class GenesisInputParser(dict):
comment_chars = ('!', '%', '#',)
def __init__(self, file_, debug=False):
self.file_ = file_
with open(self.file_, 'r') as f:
lines = f.readlines()
inside, outside = 0, 1
status = outside
blockname, blocklines = None, None
for line in lines:
line = line.strip()
if debug:
print(line)
if any(line.startswith(c) for c in self.comment_chars):
pass
elif status == outside:
if line.startswith('&'):
status = inside
blockname = line[1:]
blocklines = []
elif status == inside:
if line == '&end':
status = outside
self.addblock(blockname, blocklines)
else:
blocklines.append(line)
def addblock(self, blockname, blocklines):
dd = {}
for line in blocklines:
try:
key, value = line.split('=')
except:
print(line)
raise
key = key.strip()
value = value.strip()
try:
value = float(value)
except ValueError:
pass
if key in dd:
print(blocklines)
raise GenesisParserError('Key %s specified more than once!' % key)
else:
dd[key] = value
if 'label' in dd:
key = dd['label']
else:
key = blockname
if key in self:
raise GenesisParserError('Block %s occurs more than once!' % key)
self[key] = dd