1
|
#!/usr/bin/env python
|
2
|
# Subtracts map spreadsheet A1->C from A0->B to produce A->B.
|
3
|
# A0 entries that are empty and have no comment documenting why they are empty
|
4
|
# are subtracted as well if there is a matching entry in A1.
|
5
|
|
6
|
import csv
|
7
|
import os.path
|
8
|
import sys
|
9
|
|
10
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
11
|
|
12
|
import maps
|
13
|
import opts
|
14
|
import util
|
15
|
|
16
|
def main():
|
17
|
ignore = opts.env_flag('ignore')
|
18
|
try: _prog_name, map_1_path = sys.argv[:2]
|
19
|
except ValueError:
|
20
|
raise SystemExit('Usage: env [ignore=1] '+sys.argv[0]+' <from_map '
|
21
|
'subtract_map [compare_col_num...] [| '+sys.argv[0]
|
22
|
+' subtract_map_2]... >difference_map')
|
23
|
compare_col_nums = map(int, sys.argv[2:]) # 0-based
|
24
|
if compare_col_nums == []:
|
25
|
compare_col_nums = None # list_subset() value for all columns
|
26
|
|
27
|
def compare_on(row): return tuple(util.list_subset(row, compare_col_nums))
|
28
|
|
29
|
headers = [None]*2
|
30
|
|
31
|
# Get map 1
|
32
|
input_cols = set()
|
33
|
compare_cols = set()
|
34
|
stream = open(map_1_path, 'rb')
|
35
|
reader = csv.reader(stream)
|
36
|
headers[1] = reader.next()
|
37
|
for row in reader:
|
38
|
if row[0] != '':
|
39
|
input_cols.add(row[0])
|
40
|
compare_cols.add(compare_on(row))
|
41
|
stream.close()
|
42
|
|
43
|
# Open map 0
|
44
|
reader = csv.reader(sys.stdin)
|
45
|
headers[0] = reader.next()
|
46
|
|
47
|
# Check col labels
|
48
|
if not maps.combinable(*headers) and not ignore:
|
49
|
raise SystemExit('Map error: '
|
50
|
'Map 0 column 0 label doesn\'t contain map 1 column 0 label')
|
51
|
|
52
|
# Subtract map 1 from map 0
|
53
|
writer = csv.writer(sys.stdout)
|
54
|
writer.writerow(headers[0])
|
55
|
for row in reader:
|
56
|
if not (maps.is_nonexplicit_empty_mapping(row) and row[0] in input_cols
|
57
|
or compare_on(row) in compare_cols): # not in map 1
|
58
|
writer.writerow(row)
|
59
|
|
60
|
main()
|