Project

General

Profile

1
#!/usr/bin/env python
2
# Subtracts map spreadsheet A1->C from A0->B to produce A->B.
3
# Other A0 entries are subtracted as well:
4
# - Empty entries without a comment documenting why they are empty, if there is
5
#   a matching entry in A1.
6
# - Entries whose input and output maps to the same non-empty value in A1
7
# Multi-safe (supports an input appearing multiple times). Note that if there is
8
# *any* mapping for an input in subtract_map, all mappings for that input in
9
# from_map will be excluded.
10
# Case- and punctuation-insensitive.
11

    
12
import csv
13
import operator
14
import os.path
15
import sys
16

    
17
sys.path.append(os.path.dirname(__file__)+"/../lib")
18

    
19
import maps
20
import opts
21
import util
22

    
23
def main():
24
    ignore = opts.env_flag('ignore')
25
    try: _prog_name, map_1_path = sys.argv[:2]
26
    except ValueError:
27
        raise SystemExit('Usage: env [ignore=1] '+sys.argv[0]+' <from_map '
28
            'subtract_map [compare_col_num...] [| '+sys.argv[0]
29
            +' subtract_map_2]... >difference_map')
30
    compare_col_nums = map(int, sys.argv[2:]) # 0-based
31
    if compare_col_nums == []:
32
        compare_col_nums = None # list_subset() value for all columns
33
    
34
    def compare_on(row):
35
        return tuple(map(maps.simplify, util.list_subset(row, compare_col_nums,
36
            default=None)))
37
    
38
    headers = [None]*2
39
    
40
    # Get map 1
41
    input_cols = set()
42
    compare_cols = set()
43
    map_ = dict()
44
    stream = open(map_1_path, 'rb')
45
    reader = csv.reader(stream)
46
    headers[1] = reader.next()
47
    for row in reader:
48
        if row[0] != '':
49
            input_cols.add(maps.simplify(row[0]))
50
            compare_cols.add(compare_on(row))
51
            if reduce(operator.and_, (v == '' for v in row[1:])): # all empty
52
                map_[row[0]] = row[1]
53
    stream.close()
54
    
55
    # Open map 0
56
    reader = csv.reader(sys.stdin)
57
    headers[0] = reader.next()
58
    
59
    # Check col labels
60
    combinable = maps.combinable(*headers)
61
    if not combinable and not ignore:
62
        raise SystemExit('Map error: '
63
        'Map 0 column 0 label doesn\'t contain map 1 column 0 label')
64
    
65
    # Subtract map 1 from map 0
66
    writer = csv.writer(sys.stdout)
67
    writer.writerow(headers[0])
68
    for row in reader:
69
        if not combinable or not (
70
            (maps.is_nonexplicit_empty_mapping(row)
71
                and maps.simplify(row[0]) in input_cols)
72
            or compare_on(row) in compare_cols
73
            or util.have_same_value(map_, row[0], row[1]) # map to same place
74
            ):
75
            # not combinable or not in map 1
76
            writer.writerow(row)
77

    
78
main()
(54-54/61)