1 |
929
|
aaronmk
|
#!/usr/bin/env python
|
2 |
|
|
# Subtracts map spreadsheet A1->C from A0->B to A->B
|
3 |
|
|
|
4 |
|
|
import csv
|
5 |
1137
|
aaronmk
|
import os.path
|
6 |
929
|
aaronmk
|
import sys
|
7 |
|
|
|
8 |
1137
|
aaronmk
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
9 |
|
|
|
10 |
|
|
import opts
|
11 |
|
|
import util
|
12 |
|
|
|
13 |
929
|
aaronmk
|
def main():
|
14 |
1137
|
aaronmk
|
ignore = opts.env_flag('ignore')
|
15 |
|
|
try: _prog_name, map_1_path = sys.argv[:2]
|
16 |
929
|
aaronmk
|
except ValueError:
|
17 |
1137
|
aaronmk
|
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 |
1495
|
aaronmk
|
if compare_col_nums == []:
|
22 |
|
|
compare_col_nums = None # list_subset() value for all columns
|
23 |
929
|
aaronmk
|
|
24 |
1137
|
aaronmk
|
def compare_on(row): return tuple(util.list_subset(row, compare_col_nums))
|
25 |
|
|
|
26 |
929
|
aaronmk
|
# Get map 1
|
27 |
1137
|
aaronmk
|
compare_cols = set()
|
28 |
929
|
aaronmk
|
stream = open(map_1_path, 'rb')
|
29 |
|
|
reader = csv.reader(stream)
|
30 |
|
|
map_1_cols = reader.next()
|
31 |
|
|
for row in reader:
|
32 |
1137
|
aaronmk
|
if row[0] != '': compare_cols.add(compare_on(row))
|
33 |
929
|
aaronmk
|
stream.close()
|
34 |
|
|
|
35 |
|
|
# Open map 0
|
36 |
|
|
reader = csv.reader(sys.stdin)
|
37 |
|
|
map_0_cols = reader.next()
|
38 |
1137
|
aaronmk
|
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 |
929
|
aaronmk
|
|
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 |
1137
|
aaronmk
|
if compare_on(row) not in compare_cols: writer.writerow(row)
|
46 |
929
|
aaronmk
|
|
47 |
|
|
main()
|