1 |
927
|
aaronmk
|
#!/usr/bin/env python
|
2 |
|
|
# Intersects two map spreadsheets A0->B and A1->C to A->B, with B overwriting C
|
3 |
3760
|
aaronmk
|
# Multi-safe (supports an input appearing multiple times).
|
4 |
4504
|
aaronmk
|
# Case- and punctuation-insensitive.
|
5 |
927
|
aaronmk
|
|
6 |
|
|
import csv
|
7 |
|
|
import os.path
|
8 |
|
|
import sys
|
9 |
|
|
|
10 |
|
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
11 |
|
|
|
12 |
|
|
import maps
|
13 |
1525
|
aaronmk
|
import util
|
14 |
927
|
aaronmk
|
|
15 |
|
|
def main():
|
16 |
1525
|
aaronmk
|
try: _prog_name, map_1_path = sys.argv[:2]
|
17 |
927
|
aaronmk
|
except ValueError:
|
18 |
1525
|
aaronmk
|
raise SystemExit('Usage: '+sys.argv[0]+' <map_0 map_1 '
|
19 |
|
|
'[compare_col_num...] [| '+sys.argv[0]
|
20 |
927
|
aaronmk
|
+' map_2]... >intersect_map')
|
21 |
1525
|
aaronmk
|
compare_col_nums = map(int, sys.argv[2:]) # 0-based
|
22 |
|
|
if compare_col_nums == []:
|
23 |
|
|
compare_col_nums = None # list_subset() value for all columns
|
24 |
927
|
aaronmk
|
|
25 |
4504
|
aaronmk
|
def compare_on(row): return tuple(map(maps.simplify,
|
26 |
|
|
util.list_subset(row, compare_col_nums)))
|
27 |
1525
|
aaronmk
|
|
28 |
|
|
headers = [None]*2
|
29 |
|
|
|
30 |
927
|
aaronmk
|
# Get map 1
|
31 |
1525
|
aaronmk
|
compare_cols = set()
|
32 |
927
|
aaronmk
|
stream = open(map_1_path, 'rb')
|
33 |
|
|
reader = csv.reader(stream)
|
34 |
1525
|
aaronmk
|
headers[1] = reader.next()
|
35 |
927
|
aaronmk
|
for row in reader:
|
36 |
1525
|
aaronmk
|
if row[0] != '':
|
37 |
|
|
compare_cols.add(compare_on(row))
|
38 |
927
|
aaronmk
|
stream.close()
|
39 |
|
|
|
40 |
|
|
# Open map 0
|
41 |
|
|
reader = csv.reader(sys.stdin)
|
42 |
1525
|
aaronmk
|
headers[0] = reader.next()
|
43 |
927
|
aaronmk
|
|
44 |
1525
|
aaronmk
|
# Check col labels
|
45 |
|
|
combinable = maps.combinable(*headers)
|
46 |
4084
|
aaronmk
|
if not combinable:
|
47 |
1525
|
aaronmk
|
raise SystemExit('Map error: '
|
48 |
|
|
'Map 0 column 0 label doesn\'t contain map 1 column 0 label')
|
49 |
|
|
|
50 |
927
|
aaronmk
|
# Add map 0 to map 1, overwriting existing entries
|
51 |
|
|
writer = csv.writer(sys.stdout)
|
52 |
1525
|
aaronmk
|
writer.writerow(headers[0])
|
53 |
927
|
aaronmk
|
for row in reader:
|
54 |
1525
|
aaronmk
|
if not combinable or compare_on(row) in compare_cols:
|
55 |
|
|
# not combinable, or in map 1
|
56 |
|
|
writer.writerow(row)
|
57 |
927
|
aaronmk
|
|
58 |
|
|
main()
|