Project

General

Profile

1
#!/usr/bin/env python
2
# Combines 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
import opts
12

    
13
def col_label(col_name): return col_name.partition(':')[0]
14

    
15
def overlaps(str0, str1): return str0.find(str1) >= 0 or str1.find(str0) >= 0
16

    
17
def main():
18
    ignore = opts.env_flag('ignore')
19
    header_num = int(opts.get_env_var('header_num', 0))
20
        # selects which map's header to use as the output header
21
    try: _prog_name, map_1_path = sys.argv
22
    except ValueError:
23
        raise SystemExit('Usage: env [ignore=1] [header_num={0|1}] '+sys.argv[0]
24
            +' <map_0 map_1 [| '+sys.argv[0]+' map_2]... >union_map')
25
    
26
    headers = [None]*2
27
    
28
    # Open map 0
29
    map_0_reader = csv.reader(sys.stdin)
30
    headers[0] = map_0_reader.next()
31
    
32
    # Open map 1
33
    stream = open(map_1_path, 'rb')
34
    map_1_reader = csv.reader(stream)
35
    headers[1] = map_1_reader.next()
36
    
37
    # Check col labels
38
    combinable = overlaps(*[col_label(header[0]) for header in headers])
39
    if not combinable and not ignore: raise SystemExit('Map error: '
40
        'Map 0 column 0 label doesn\'t contain map 1 column 0 label')
41
    
42
    # Pass through map 0, storing which inputs it defines
43
    writer = csv.writer(sys.stdout)
44
    writer.writerow(headers[header_num])
45
    inputs = set()
46
    for row in map_0_reader:
47
        if row[0] != '': inputs.add(row[0])
48
        writer.writerow(row)
49
    
50
    if combinable:
51
        # Add entries in map 1 that weren't already defined
52
        for row in map_1_reader:
53
            if row[0] != '' and row[0] not in inputs: writer.writerow(row)
54
    
55
    stream.close()
56

    
57
main()
(26-26/28)