Project

General

Profile

1
#!/usr/bin/env python
2
# Combines two map spreadsheets A0->B and A1->C to A->B, with B overwriting C
3

    
4
import csv
5
import os.path
6
import sys
7

    
8
sys.path.append(os.path.dirname(__file__)+"/../lib")
9

    
10
import maps
11
import opts
12

    
13
def col_label(col_name): return col_name.partition(':')[0]
14

    
15
def main():
16
    ignore = opts.env_flag('ignore')
17
    try: _prog_name, map_1_path = sys.argv
18
    except ValueError:
19
        raise SystemExit('Usage: env [ignore=1] '+sys.argv[0]
20
            +' <map_0 map_1 [| '+sys.argv[0]+' map_2]... >union_map')
21
    
22
    map_ = {}
23
    def add_map(reader):
24
        for row in reader:
25
            if row[0] != '':
26
                try: new = map_[row[0]]
27
                except KeyError: new = row
28
                else: new = maps.merge_mappings(new, row)
29
                map_[row[0]] = new
30
    
31
    # Get map 1
32
    stream = open(map_1_path, 'rb')
33
    reader = csv.reader(stream)
34
    map_1_cols = reader.next()
35
    add_map(reader)
36
    stream.close()
37
    
38
    # Add map 0 to map 1, overwriting existing entries
39
    reader = csv.reader(sys.stdin)
40
    map_0_cols = reader.next()
41
    if not col_label(map_0_cols[0]).find(col_label(map_1_cols[0])) >= 0:
42
        if ignore: sys.exit()
43
        else: raise SystemExit('Map error: Map 0 column 0 label doesn\'t '
44
            'contain map 1 column 0 label')
45
    add_map(reader)
46
    
47
    # Write combined map
48
    writer = csv.writer(sys.stdout)
49
    writer.writerow(maps.merge_mappings(map_1_cols, map_0_cols))
50
    for row in map_.itervalues(): writer.writerow(row)
51

    
52
main()
(24-24/25)