Project

General

Profile

1
#!/usr/bin/env python
2
# Subtracts map spreadsheet A1->C from A0->B to A->B
3

    
4
import csv
5
import os.path
6
import sys
7

    
8
sys.path.append(os.path.dirname(__file__)+"/../lib")
9

    
10
import opts
11
import util
12

    
13
def main():
14
    ignore = opts.env_flag('ignore')
15
    try: _prog_name, map_1_path = sys.argv[:2]
16
    except ValueError:
17
        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
    
23
    def compare_on(row): return tuple(util.list_subset(row, compare_col_nums))
24
    
25
    # Get map 1
26
    compare_cols = set()
27
    stream = open(map_1_path, 'rb')
28
    reader = csv.reader(stream)
29
    map_1_cols = reader.next()
30
    for row in reader:
31
        if row[0] != '': compare_cols.add(compare_on(row))
32
    stream.close()
33
    
34
    # Open map 0
35
    reader = csv.reader(sys.stdin)
36
    map_0_cols = reader.next()
37
    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
    
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
        if compare_on(row) not in compare_cols: writer.writerow(row)
45

    
46
main()
(24-24/29)