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 maps
11
import opts
12
import util
13

    
14
def main():
15
    ignore = opts.env_flag('ignore')
16
    try: _prog_name, map_1_path = sys.argv[:2]
17
    except ValueError:
18
        raise SystemExit('Usage: env [ignore=1] '+sys.argv[0]+' <from_map '
19
            'subtract_map [compare_col_num...] [| '+sys.argv[0]
20
            +' subtract_map_2]... >difference_map')
21
    compare_col_nums = map(int, sys.argv[2:]) # 0-based
22
    if compare_col_nums == []:
23
        compare_col_nums = None # list_subset() value for all columns
24
    
25
    def compare_on(row): return tuple(util.list_subset(row, compare_col_nums))
26
    
27
    headers = [None]*2
28
    
29
    # Get map 1
30
    compare_cols = set()
31
    stream = open(map_1_path, 'rb')
32
    reader = csv.reader(stream)
33
    headers[1] = reader.next()
34
    for row in reader:
35
        if row[0] != '': compare_cols.add(compare_on(row))
36
    stream.close()
37
    
38
    # Open map 0
39
    reader = csv.reader(sys.stdin)
40
    headers[0] = reader.next()
41
    
42
    # Check col labels
43
    if not maps.combinable(*headers) and not ignore:
44
        raise SystemExit('Map error: '
45
        'Map 0 column 0 label doesn\'t contain map 1 column 0 label')
46
    
47
    # Subtract map 1 from map 0
48
    writer = csv.writer(sys.stdout)
49
    writer.writerow(headers[0])
50
    for row in reader:
51
        if compare_on(row) not in compare_cols: writer.writerow(row)
52

    
53
main()
(27-27/32)