-
Notifications
You must be signed in to change notification settings - Fork 10
/
parse.py
executable file
·67 lines (61 loc) · 2.31 KB
/
parse.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
#!/usr/bin/env python
import os
import sys
import re
# regex for numbers
number_regex = r'([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)'
# regex for array of numbers in format '[1;2;3]'
array_regex = r'(\[' + number_regex + r'(;' + number_regex + r')*\])'
# value can be either a number or an array
value_regex = '(' + number_regex + '|' + array_regex + ')'
# basic dimension is word, possibly with power, e.g. cm, cm^{-3}, cm^2.
simple_dimension_regex = r'\w+(\^({[+-]?\d+}|\d+))?'
# dimension can contain multiple simple dimensions divided by / and *, or percent sign
dimension_regex = '({0}([/*]{0})*|%)'.format(simple_dimension_regex)
# value with the dimension are separated by space
value_dimension_regex = value_regex + '\s+' + dimension_regex
# text only values can contain word symbols, digits, dots, slashes, ampersands and hyphens
text_only_regex = r'([\w\d&-./]+)'
# Python expressions that are to be evaluated during preprocessing
eval_regex = r'eval\{(.+)\}'
# Python statements that are to be executed during preprocessing
exec_regex = r'exec\{(.+)\}'
conf = sys.argv[1]
if not os.path.isfile(conf):
raise Exception('Config file [%s] not found' % conf)
with open(conf, "r") as f:
buf = ''
for l in f:
if '#' in l:
l = l[:l.find('#')]
l = l.strip()
if l == '':
continue
l = re.sub(eval_regex, lambda m: str(eval(m.group(1))), l)
if re.match('^' + exec_regex + '$', l):
command = re.findall(exec_regex, l)[0]
exec(command)
else:
buf += l + '\n'
for l in buf.splitlines():
key_value = l.split('=')
if len(key_value) != 2:
raise Exception('Line [%s] is invalid' % l)
key = key_value[0].strip()
value = key_value[1].strip()
print(key)
if re.match('^' + value_regex + '$', value):
print(value)
print('#')
elif re.match('^' + text_only_regex + '$', value):
print('#')
print(value)
elif re.match('^' + value_dimension_regex + '$', value):
v = re.findall(value_regex, value)[0][0]
print(v)
d = value[len(v):].strip()
print(d)
else:
raise Exception('Syntax of value [%s] of key [%s] is incorrect' % (value, key))
print('$')
print('$')