Project

General

Profile

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