mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-29 12:43:38 +03:00
85 lines
2.6 KiB
Python
Executable File
85 lines
2.6 KiB
Python
Executable File
#!/usr/bin/python2
|
|
|
|
import os
|
|
|
|
def run():
|
|
mapping = {}
|
|
|
|
for path, _, files in os.walk('src'):
|
|
for f in files:
|
|
if f.endswith('.rs') and f != 'colors.rs':
|
|
for k, v in read_file(os.path.join(path, f)):
|
|
# TODO Check for double-definitions
|
|
mapping[k] = v
|
|
|
|
with open('src/init_colors.rs', 'w') as f:
|
|
f.write('// AUTOGENERATED! Do not modify.\n')
|
|
f.write('\n')
|
|
f.write('use ezgui::Color;\n')
|
|
f.write('use std::collections::HashMap;\n')
|
|
f.write('\n')
|
|
f.write('pub fn default_colors() -> HashMap<String, Color> {\n')
|
|
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')
|
|
|
|
|
|
def read_file(filename):
|
|
entries = []
|
|
with open(filename, 'r') as f:
|
|
src = ''.join(f.readlines())
|
|
while src:
|
|
if src.startswith('get_def('):
|
|
src = src[len('get_def('):]
|
|
|
|
# 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:]
|
|
|
|
# Wait for the ()'s to be mismatched, meaning we found the ) of the get_def()
|
|
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()
|