Project

General

Profile

1
#!/usr/bin/env python
2
# Extracts subpaths from a map spreadsheet
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 subpath config from env vars
17
    config_names = ['subpath', 'new_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]+' <map >submap')
23
    
24
    # Transform map
25
    for config in configs:
26
        config['prefix'] = config['subpath']+config['new_root']
27
        config['prefix_len'] = len(config['prefix'])
28
    reader = csv.reader(sys.stdin)
29
    col_names = reader.next()[:2]
30
    # TODO: replace col_names with new_roots
31
    writer = csv.writer(sys.stdout)
32
    writer.writerow(col_names)
33
    for row in reader:
34
        if row[0].startswith(configs[0]['prefix']):
35
            assert row[1].startswith(configs[1]['prefix'])
36
            for idx in range(len(configs)):
37
                row[idx] = row[idx][configs[idx]['prefix_len']:]
38
            writer.writerow(row)
39

    
40
main()
(7-7/12)