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
|
|
12
|
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
|
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
|
|
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
|
if map_0_cols[0] != map_1_cols[0]: raise SystemExit('Map error: '
|
38
|
'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
|
writer.writerow(maps.merge_mappings(map_1_cols, map_0_cols))
|
44
|
for row in map_.itervalues(): writer.writerow(row)
|
45
|
|
46
|
main()
|