Project

General

Profile

1 187 aaronmk
#!/usr/bin/env python
2 926 aaronmk
# Combines two map spreadsheets A0->B and A1->C to A->B, with B overwriting C
3 3759 aaronmk
# Multi-safe (supports an input appearing multiple times). Note that if there is
4 4158 aaronmk
# *any* non-empty mapping for an input in map_0, all mappings for that input in
5
# map_1 will be excluded.
6 187 aaronmk
7
import csv
8 738 aaronmk
import os.path
9 187 aaronmk
import sys
10
11 738 aaronmk
sys.path.append(os.path.dirname(__file__)+"/../lib")
12
13
import maps
14 1123 aaronmk
import opts
15 1769 aaronmk
import util
16 738 aaronmk
17 4164 aaronmk
def non_empty(row): return row[0] != '' and row[1] != ''
18
19 187 aaronmk
def main():
20 1123 aaronmk
    ignore = opts.env_flag('ignore')
21 1769 aaronmk
    header_num = util.cast(int, opts.get_env_var('header_num'))
22 1127 aaronmk
        # selects which map's header to use as the output header
23 3902 aaronmk
    all_ = opts.env_flag('all') # doesn't merge mappings that are in both maps
24 187 aaronmk
    try: _prog_name, map_1_path = sys.argv
25
    except ValueError:
26 3902 aaronmk
        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 187 aaronmk
29 1125 aaronmk
    headers = [None]*2
30 187 aaronmk
31 1125 aaronmk
    # Open map 0
32
    map_0_reader = csv.reader(sys.stdin)
33
    headers[0] = map_0_reader.next()
34
35
    # Open map 1
36 187 aaronmk
    stream = open(map_1_path, 'rb')
37 1125 aaronmk
    map_1_reader = csv.reader(stream)
38
    headers[1] = map_1_reader.next()
39 187 aaronmk
40 1125 aaronmk
    # Check col labels
41 1507 aaronmk
    combinable = maps.combinable(*headers)
42
    if not combinable and not ignore:
43 1502 aaronmk
        raise SystemExit('Map error: '
44 1125 aaronmk
        'Map 0 column 0 label doesn\'t contain map 1 column 0 label')
45 187 aaronmk
46 2023 aaronmk
    # Pass through map 0, storing which mappings it defines
47 187 aaronmk
    writer = csv.writer(sys.stdout)
48 1796 aaronmk
    writer.writerow(maps.merge_headers(*headers, **dict(prefer=header_num)))
49 2023 aaronmk
    mappings = set()
50 1125 aaronmk
    for row in map_0_reader:
51 4164 aaronmk
        if all_ or non_empty(row):
52
            if not all_:
53
                mappings.add(row[0])
54
                mappings.add(row[1])
55
            writer.writerow(row)
56 1125 aaronmk
57
    if combinable:
58 2023 aaronmk
        # Add mappings in map 1 that weren't already defined or mapped to
59 1125 aaronmk
        for row in map_1_reader:
60 4164 aaronmk
            if non_empty(row) and row[0] not in mappings: writer.writerow(row)
61 1125 aaronmk
62
    stream.close()
63 187 aaronmk
64
main()