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 |
1123
|
aaronmk
|
import opts
|
12 |
738
|
aaronmk
|
|
13 |
1123
|
aaronmk
|
def col_label(col_name): return col_name.partition(':')[0]
|
14 |
|
|
|
15 |
187
|
aaronmk
|
def main():
|
16 |
1123
|
aaronmk
|
ignore = opts.env_flag('ignore')
|
17 |
187
|
aaronmk
|
try: _prog_name, map_1_path = sys.argv
|
18 |
|
|
except ValueError:
|
19 |
1123
|
aaronmk
|
raise SystemExit('Usage: env [ignore=1] '+sys.argv[0]
|
20 |
|
|
+' <map_0 map_1 [| '+sys.argv[0]+' map_2]... >union_map')
|
21 |
187
|
aaronmk
|
|
22 |
|
|
map_ = {}
|
23 |
|
|
def add_map(reader):
|
24 |
|
|
for row in reader:
|
25 |
738
|
aaronmk
|
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 |
187
|
aaronmk
|
|
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 |
1123
|
aaronmk
|
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 |
187
|
aaronmk
|
add_map(reader)
|
46 |
|
|
|
47 |
|
|
# Write combined map
|
48 |
|
|
writer = csv.writer(sys.stdout)
|
49 |
738
|
aaronmk
|
writer.writerow(maps.merge_mappings(map_1_cols, map_0_cols))
|
50 |
|
|
for row in map_.itervalues(): writer.writerow(row)
|
51 |
187
|
aaronmk
|
|
52 |
|
|
main()
|