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