Project

General

Profile

1 187 aaronmk
#!/usr/bin/env python
2 926 aaronmk
# Combines two map spreadsheets A0->B and A1->C to A->B, with B overwriting C
3 187 aaronmk
4
import csv
5 738 aaronmk
import os.path
6 187 aaronmk
import sys
7
8 738 aaronmk
sys.path.append(os.path.dirname(__file__)+"/../lib")
9
10
import maps
11
12 187 aaronmk
def main():
13
    try: _prog_name, map_1_path = sys.argv
14
    except ValueError:
15
        raise SystemExit('Usage: '+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]
16
            +' map_2]... >union_map')
17
18
    map_ = {}
19
    def add_map(reader):
20
        for row in reader:
21 738 aaronmk
            if row[0] != '':
22
                try: new = map_[row[0]]
23
                except KeyError: new = row
24
                else: new = maps.merge_mappings(new, row)
25
                map_[row[0]] = new
26 187 aaronmk
27
    # Get map 1
28
    stream = open(map_1_path, 'rb')
29
    reader = csv.reader(stream)
30
    map_1_cols = reader.next()
31
    add_map(reader)
32
    stream.close()
33
34
    # Add map 0 to map 1, overwriting existing entries
35
    reader = csv.reader(sys.stdin)
36
    map_0_cols = reader.next()
37 738 aaronmk
    if map_0_cols[0] != map_1_cols[0]: raise SystemExit('Map error: '
38 187 aaronmk
        'Map 1 column 0 name doesn\'t match map 0 column 0 name')
39
    add_map(reader)
40
41
    # Write combined map
42
    writer = csv.writer(sys.stdout)
43 738 aaronmk
    writer.writerow(maps.merge_mappings(map_1_cols, map_0_cols))
44
    for row in map_.itervalues(): writer.writerow(row)
45 187 aaronmk
46
main()