1
|
#!/usr/bin/env python
|
2
|
# Combines two map spreadsheets A0->B and A1->C to A->B, with B overwriting C
|
3
|
# Multi-safe (supports an input appearing multiple times). Note that if there is
|
4
|
# *any* non-empty mapping for an input in map_0, all mappings for that input in
|
5
|
# map_1 will be excluded.
|
6
|
|
7
|
import csv
|
8
|
import os.path
|
9
|
import sys
|
10
|
|
11
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
12
|
|
13
|
import maps
|
14
|
import opts
|
15
|
import util
|
16
|
|
17
|
def main():
|
18
|
ignore = opts.env_flag('ignore')
|
19
|
header_num = util.cast(int, opts.get_env_var('header_num'))
|
20
|
# selects which map's header to use as the output header
|
21
|
all_ = opts.env_flag('all') # doesn't merge mappings that are in both maps
|
22
|
try: _prog_name, map_1_path = sys.argv
|
23
|
except ValueError:
|
24
|
raise SystemExit('Usage: env [ignore=1] [header_num={0|1}] [all=1] '
|
25
|
+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]+' map_2]... >out_map')
|
26
|
|
27
|
headers = [None]*2
|
28
|
|
29
|
# Open map 0
|
30
|
map_0_reader = csv.reader(sys.stdin)
|
31
|
headers[0] = map_0_reader.next()
|
32
|
|
33
|
# Open map 1
|
34
|
stream = open(map_1_path, 'rb')
|
35
|
map_1_reader = csv.reader(stream)
|
36
|
headers[1] = map_1_reader.next()
|
37
|
|
38
|
# Check col labels
|
39
|
combinable = maps.combinable(*headers)
|
40
|
if not combinable and not ignore:
|
41
|
raise SystemExit('Map error: '
|
42
|
'Map 0 column 0 label doesn\'t contain map 1 column 0 label')
|
43
|
|
44
|
# Pass through map 0, storing which mappings it defines
|
45
|
writer = csv.writer(sys.stdout)
|
46
|
writer.writerow(maps.merge_headers(*headers, **dict(prefer=header_num)))
|
47
|
mappings = set()
|
48
|
for row in map_0_reader:
|
49
|
if not all_ and row[0] != '' and row[1] != '':
|
50
|
mappings.add(row[0])
|
51
|
mappings.add(row[1])
|
52
|
writer.writerow(row)
|
53
|
|
54
|
if combinable:
|
55
|
# Add mappings in map 1 that weren't already defined or mapped to
|
56
|
for row in map_1_reader:
|
57
|
if row[0] != '' and row[0] not in mappings: writer.writerow(row)
|
58
|
|
59
|
stream.close()
|
60
|
|
61
|
main()
|