2018-12-13 02:35:20 +03:00
|
|
|
#!/usr/bin/python2
|
|
|
|
|
|
|
|
import os
|
2018-12-20 21:07:27 +03:00
|
|
|
import sys
|
2018-12-13 02:35:20 +03:00
|
|
|
|
|
|
|
def run():
|
|
|
|
mapping = {}
|
|
|
|
|
2018-12-13 03:58:46 +03:00
|
|
|
for path, _, files in os.walk('src'):
|
2018-12-13 02:35:20 +03:00
|
|
|
for f in files:
|
2018-12-13 03:20:20 +03:00
|
|
|
if f.endswith('.rs') and f != 'colors.rs':
|
2018-12-13 02:35:20 +03:00
|
|
|
for k, v in read_file(os.path.join(path, f)):
|
2018-12-13 04:23:27 +03:00
|
|
|
if k in mapping:
|
|
|
|
raise ValueError('Color {} defined twice'.format(k))
|
2018-12-13 02:35:20 +03:00
|
|
|
mapping[k] = v
|
|
|
|
|
2018-12-20 21:07:27 +03:00
|
|
|
with open(sys.argv[1], 'w') as f:
|
|
|
|
f.write('fn default_colors() -> HashMap<String, Color> {\n')
|
2018-12-13 03:53:34 +03:00
|
|
|
f.write(' let mut m = HashMap::new();\n')
|
|
|
|
for k in sorted(mapping.iterkeys()):
|
|
|
|
f.write(' m.insert("{}".to_string(), {});\n'.format(k, mapping[k]))
|
|
|
|
f.write(' m\n')
|
|
|
|
f.write('}\n')
|
2018-12-13 02:35:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
def read_file(filename):
|
|
|
|
entries = []
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
src = ''.join(f.readlines())
|
|
|
|
while src:
|
2018-12-13 03:20:20 +03:00
|
|
|
if src.startswith('get_def('):
|
|
|
|
src = src[len('get_def('):]
|
2018-12-13 02:35:20 +03:00
|
|
|
|
|
|
|
# Look for the opening "
|
|
|
|
while src[0] != '"':
|
|
|
|
src = src[1:]
|
|
|
|
src = src[1:]
|
|
|
|
|
|
|
|
# Read the key until the closing "
|
|
|
|
key = ''
|
|
|
|
while src[0] != '"':
|
|
|
|
key += src[0]
|
|
|
|
src = src[1:]
|
|
|
|
src = src[1:]
|
|
|
|
|
|
|
|
# Look for the ,
|
|
|
|
while src[0] != ',':
|
|
|
|
src = src[1:]
|
|
|
|
src = src[1:]
|
|
|
|
|
|
|
|
# Look for the Color
|
|
|
|
while not src.startswith('Color'):
|
|
|
|
src = src[1:]
|
|
|
|
|
2018-12-13 03:20:20 +03:00
|
|
|
# Wait for the ()'s to be mismatched, meaning we found the ) of the get_def()
|
2018-12-13 02:35:20 +03:00
|
|
|
counter = 0
|
|
|
|
value = ''
|
|
|
|
while True:
|
|
|
|
value += src[0]
|
|
|
|
if src[0] == '(':
|
|
|
|
counter += 1
|
|
|
|
elif src[0] == ')':
|
|
|
|
counter -= 1
|
|
|
|
if counter == -1:
|
|
|
|
value = value[:-1]
|
|
|
|
entries.append((key, value))
|
|
|
|
src = src[1:]
|
|
|
|
break
|
|
|
|
elif src[0] == ',' and counter == 0:
|
|
|
|
value = value[:-1]
|
|
|
|
entries.append((key, value))
|
|
|
|
src = src[1:]
|
|
|
|
break
|
|
|
|
src = src[1:]
|
|
|
|
else:
|
|
|
|
src = src[1:]
|
|
|
|
return entries
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|