1 |
68
|
aaronmk
|
#!/usr/bin/env python
|
2 |
69
|
aaronmk
|
# Transforms a map spreadsheet to use a different root
|
3 |
68
|
aaronmk
|
|
4 |
|
|
import csv
|
5 |
|
|
import os.path
|
6 |
72
|
aaronmk
|
import re
|
7 |
68
|
aaronmk
|
import sys
|
8 |
|
|
|
9 |
|
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
10 |
|
|
|
11 |
|
|
import opts
|
12 |
|
|
|
13 |
|
|
def all_not_none(list_):
|
14 |
|
|
return reduce(lambda a, b: a and b, map(lambda e: e != None, list_))
|
15 |
|
|
|
16 |
|
|
def main():
|
17 |
69
|
aaronmk
|
# Get config from env vars
|
18 |
|
|
config_names = ['in_root', 'out_root']
|
19 |
68
|
aaronmk
|
env_names = []
|
20 |
|
|
configs = map(lambda col: opts.get_env_vars(config_names, col, env_names),
|
21 |
|
|
['left', 'right'])
|
22 |
|
|
if not all_not_none(configs): raise SystemExit(
|
23 |
69
|
aaronmk
|
'Usage: '+opts.env_usage(env_names)+' '+sys.argv[0]+' <in_map >out_map')
|
24 |
68
|
aaronmk
|
|
25 |
|
|
# Transform map
|
26 |
|
|
reader = csv.reader(sys.stdin)
|
27 |
|
|
writer = csv.writer(sys.stdout)
|
28 |
69
|
aaronmk
|
cols = reader.next()
|
29 |
70
|
aaronmk
|
for i in xrange(len(configs)):
|
30 |
69
|
aaronmk
|
config = configs[i]
|
31 |
72
|
aaronmk
|
label, sep, config['root'] = cols[i].partition(':')
|
32 |
|
|
cols[i] = label+sep+config['out_root']
|
33 |
|
|
config['in_root_re'] = r'^'+re.escape(config['in_root'])+r'\b'
|
34 |
69
|
aaronmk
|
writer.writerow(cols)
|
35 |
68
|
aaronmk
|
for row in reader:
|
36 |
70
|
aaronmk
|
for i in xrange(len(configs)): row[i] = configs[i]['root']+row[i]
|
37 |
72
|
aaronmk
|
def sub(i):
|
38 |
|
|
row[i], n = re.subn(configs[i]['in_root_re'], r'', row[i])
|
39 |
|
|
return n > 0
|
40 |
|
|
if sub(0):
|
41 |
|
|
if not sub(1): raise SystemExit('Map error: Root "'
|
42 |
|
|
+configs[1]['in_root']+'" is not contained in output mapping: '
|
43 |
|
|
+row[1])
|
44 |
68
|
aaronmk
|
writer.writerow(row)
|
45 |
|
|
|
46 |
|
|
main()
|