Project

General

Profile

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
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 69 aaronmk
    # Get config from env vars
17
    config_names = ['in_root', 'out_root']
18 68 aaronmk
    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 69 aaronmk
        'Usage: '+opts.env_usage(env_names)+' '+sys.argv[0]+' <in_map >out_map')
23 68 aaronmk
24
    # Transform map
25
    reader = csv.reader(sys.stdin)
26
    writer = csv.writer(sys.stdout)
27 69 aaronmk
    cols = reader.next()
28 70 aaronmk
    for i in xrange(len(configs)):
29 69 aaronmk
        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 68 aaronmk
    for row in reader:
37 70 aaronmk
        for i in xrange(len(configs)): row[i] = configs[i]['root']+row[i]
38 68 aaronmk
        if row[0].startswith(configs[0]['prefix']):
39 70 aaronmk
            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 69 aaronmk
                row[i] = row[i][configs[i]['prefix_len']:]
44 68 aaronmk
            writer.writerow(row)
45
46
main()