1
|
#!/usr/bin/env python
|
2
|
# Combines two map spreadsheets A0->B and A1->C to A->B, with B overwriting C
|
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
|
|
13
|
def main():
|
14
|
ignore = opts.env_flag('ignore')
|
15
|
header_num = int(opts.get_env_var('header_num', 0))
|
16
|
# selects which map's header to use as the output header
|
17
|
try: _prog_name, map_1_path = sys.argv
|
18
|
except ValueError:
|
19
|
raise SystemExit('Usage: env [ignore=1] [header_num={0|1}] '+sys.argv[0]
|
20
|
+' <map_0 map_1 [| '+sys.argv[0]+' map_2]... >union_map')
|
21
|
|
22
|
headers = [None]*2
|
23
|
|
24
|
# Open map 0
|
25
|
map_0_reader = csv.reader(sys.stdin)
|
26
|
headers[0] = map_0_reader.next()
|
27
|
|
28
|
# Open map 1
|
29
|
stream = open(map_1_path, 'rb')
|
30
|
map_1_reader = csv.reader(stream)
|
31
|
headers[1] = map_1_reader.next()
|
32
|
|
33
|
# Check col labels
|
34
|
if not maps.combinable(*headers) and not ignore:
|
35
|
raise SystemExit('Map error: '
|
36
|
'Map 0 column 0 label doesn\'t contain map 1 column 0 label')
|
37
|
|
38
|
# Pass through map 0, storing which inputs it defines
|
39
|
writer = csv.writer(sys.stdout)
|
40
|
writer.writerow(headers[header_num])
|
41
|
inputs = set()
|
42
|
for row in map_0_reader:
|
43
|
if row[0] != '': inputs.add(row[0])
|
44
|
writer.writerow(row)
|
45
|
|
46
|
if combinable:
|
47
|
# Add entries in map 1 that weren't already defined
|
48
|
for row in map_1_reader:
|
49
|
if row[0] != '' and row[0] not in inputs: writer.writerow(row)
|
50
|
|
51
|
stream.close()
|
52
|
|
53
|
main()
|