Project

General

Profile

1 929 aaronmk
#!/usr/bin/env python
2
# Subtracts map spreadsheet A1->C from A0->B to A->B
3
4
import csv
5 1137 aaronmk
import os.path
6 929 aaronmk
import sys
7
8 1137 aaronmk
sys.path.append(os.path.dirname(__file__)+"/../lib")
9
10
import opts
11
import util
12
13 929 aaronmk
def main():
14 1137 aaronmk
    ignore = opts.env_flag('ignore')
15
    try: _prog_name, map_1_path = sys.argv[:2]
16 929 aaronmk
    except ValueError:
17 1137 aaronmk
        raise SystemExit('Usage: env [ignore=1] '+sys.argv[0]+' <from_map '
18
            'subtract_map [compare_col_num...] [| '+sys.argv[0]
19
            +' subtract_map_2]... >difference_map')
20
    compare_col_nums = map(int, sys.argv[2:]) # 0-based
21
    if compare_col_nums == []: compare_col_nums = [0]
22 929 aaronmk
23 1137 aaronmk
    def compare_on(row): return tuple(util.list_subset(row, compare_col_nums))
24
25 929 aaronmk
    # Get map 1
26 1137 aaronmk
    compare_cols = set()
27 929 aaronmk
    stream = open(map_1_path, 'rb')
28
    reader = csv.reader(stream)
29
    map_1_cols = reader.next()
30
    for row in reader:
31 1137 aaronmk
        if row[0] != '': compare_cols.add(compare_on(row))
32 929 aaronmk
    stream.close()
33
34
    # Open map 0
35
    reader = csv.reader(sys.stdin)
36
    map_0_cols = reader.next()
37 1137 aaronmk
    if not ignore and map_0_cols[0] != map_1_cols[0]: raise SystemExit(
38
        'Map error: Map 1 column 0 name doesn\'t match map 0 column 0 name')
39 929 aaronmk
40
    # Subtract map 1 from map 0
41
    writer = csv.writer(sys.stdout)
42
    writer.writerow(map_0_cols)
43
    for row in reader:
44 1137 aaronmk
        if compare_on(row) not in compare_cols: writer.writerow(row)
45 929 aaronmk
46
main()