-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseml.py
executable file
·59 lines (52 loc) · 1.79 KB
/
seml.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
#!/usr/bin/env python3
import sys
import re
p = re.compile('(?P<indent> *)(' +
'(?P<obj_key>[\w_-]+):(?P<obj_value>.+)?' +
'|(?P<array_entry>-(?P<array_value>.+)?)' +
'|(?P<comment>#.*)' +
'|(?P<empty_line>)' +
')')
lines = [line.rstrip('\n') for line in sys.stdin]
def process(min_indent = -1):
global lines
expected_indent = None
result = None
while lines:
line = lines[0]
m = p.fullmatch(line)
assert m, "line failed to parse: '" + line + "'"
if m.group('comment') is not None or m.group('empty_line') is not None:
lines = lines[1:]
continue
indent = len(m.group('indent'))
if m.group('array_entry'):
indent += 1
if expected_indent:
if indent < expected_indent:
break
assert indent == expected_indent, "wrong indent in '" + line + "'"
else:
assert indent > min_indent, "unexpected dedent in '" + line + "'"
expected_indent = indent
lines = lines[1:]
if m.group('array_entry'):
if result:
assert isinstance(result, list)
else:
result = []
if m.group('array_value'):
result.append(m.group('array_value').strip())
else:
result.append(process(expected_indent))
elif m.group('obj_key'):
if result:
assert not isinstance(result, list)
else:
result = {}
if m.group('obj_value'):
result[m.group('obj_key')] = m.group('obj_value').strip()
else:
result[m.group('obj_key')] = process(expected_indent)
return result
print(process())