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
# Multi-safe (supports an input appearing multiple times). Note that if there is
4
# *any* non-empty mapping for an input in map_0, all mappings for that input in
5
# map_1 will be excluded.
6

    
7
import csv
8
import os.path
9
import sys
10

    
11
sys.path.append(os.path.dirname(__file__)+"/../lib")
12

    
13
import maps
14
import opts
15
import util
16

    
17
def non_empty(row): return row[0] != '' and row[1] != ''
18

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

    
62
main()
(54-54/58)