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 |
503
|
aaronmk
|
config['in_root_re'] = r'^'+re.escape(config['in_root'])+r'(?=\b|\W|$)'
|
34 |
69
|
aaronmk
|
writer.writerow(cols)
|
35 |
68
|
aaronmk
|
for row in reader:
|
36 |
72
|
aaronmk
|
def sub(i):
|
37 |
928
|
aaronmk
|
row[i], n = re.subn(configs[i]['in_root_re'], r'',
|
38 |
|
|
configs[i]['root']+row[i])
|
39 |
72
|
aaronmk
|
return n > 0
|
40 |
928
|
aaronmk
|
if row[0] != '' and sub(0):
|
41 |
|
|
if row[1] != '' and not sub(1): raise SystemExit('Map error: Root "'
|
42 |
72
|
aaronmk
|
+configs[1]['in_root']+'" is not contained in output mapping: '
|
43 |
|
|
+row[1])
|
44 |
68
|
aaronmk
|
writer.writerow(row)
|
45 |
|
|
|
46 |
|
|
main()
|