Project

General

Profile

1
#!/usr/bin/env python
2
# Intersects two map spreadsheets A0->B and A1->C to A->B, with B overwriting C
3
# Multi-safe (supports an input appearing multiple times).
4

    
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
import util
13

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

    
56
main()
(23-23/58)