1
|
#!/usr/bin/env python
|
2
|
# Intersects 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]... >intersect_map')
|
17
|
|
18
|
# Get map 1
|
19
|
map_1 = {}
|
20
|
stream = open(map_1_path, 'rb')
|
21
|
reader = csv.reader(stream)
|
22
|
map_1_cols = reader.next()
|
23
|
for row in reader:
|
24
|
if row[0] != '': map_1[row[0]] = row
|
25
|
stream.close()
|
26
|
|
27
|
# Open map 0
|
28
|
reader = csv.reader(sys.stdin)
|
29
|
map_0_cols = reader.next()
|
30
|
if map_0_cols[0] != map_1_cols[0]: raise SystemExit('Map error: '
|
31
|
'Map 1 column 0 name doesn\'t match map 0 column 0 name')
|
32
|
|
33
|
# Add map 0 to map 1, overwriting existing entries
|
34
|
writer = csv.writer(sys.stdout)
|
35
|
writer.writerow(maps.merge_mappings(map_1_cols, map_0_cols))
|
36
|
for row in reader:
|
37
|
if row[0] != '':
|
38
|
try: prev = map_1[row[0]]
|
39
|
except KeyError: pass
|
40
|
else: writer.writerow(maps.merge_mappings(prev, row))
|
41
|
|
42
|
main()
|