Project

General

Profile

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 re
7
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
    # Get config from env vars
18
    config_names = ['in_root', 'out_root']
19
    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
        'Usage: '+opts.env_usage(env_names)+' '+sys.argv[0]+' <in_map >out_map')
24
    
25
    # Transform map
26
    reader = csv.reader(sys.stdin)
27
    writer = csv.writer(sys.stdout)
28
    cols = reader.next()
29
    for i in xrange(len(configs)):
30
        config = configs[i]
31
        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
    writer.writerow(cols)
35
    for row in reader:
36
        for i in xrange(len(configs)): row[i] = configs[i]['root']+row[i]
37
        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
            writer.writerow(row)
45

    
46
main()
(1-1/13)