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 == []:
22
        compare_col_nums = None # list_subset() value for all columns
23
    
24
    def compare_on(row): return tuple(util.list_subset(row, compare_col_nums))
25
    
26
    # Get map 1
27
    compare_cols = set()
28
    stream = open(map_1_path, 'rb')
29
    reader = csv.reader(stream)
30
    map_1_cols = reader.next()
31
    for row in reader:
32
        if row[0] != '': compare_cols.add(compare_on(row))
33
    stream.close()
34
    
35
    # Open map 0
36
    reader = csv.reader(sys.stdin)
37
    map_0_cols = reader.next()
38
    if not ignore and map_0_cols[0] != map_1_cols[0]: raise SystemExit(
39
        'Map error: Map 1 column 0 name doesn\'t match map 0 column 0 name')
40
    
41
    # Subtract map 1 from map 0
42
    writer = csv.writer(sys.stdout)
43
    writer.writerow(map_0_cols)
44
    for row in reader:
45
        if compare_on(row) not in compare_cols: writer.writerow(row)
46

    
47
main()
(27-27/32)