1
|
#!/usr/bin/env python
|
2
|
# Inner-joins two map spreadsheets A->B and B->C to A->C
|
3
|
# Multi-safe (supports an input appearing multiple times). If multiple outputs
|
4
|
# should be considered ambiguous, they can be discarded by setting only_one.
|
5
|
|
6
|
import csv
|
7
|
import operator
|
8
|
import os.path
|
9
|
import sys
|
10
|
import warnings
|
11
|
|
12
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
13
|
|
14
|
import maps
|
15
|
import opts
|
16
|
import util
|
17
|
|
18
|
def main():
|
19
|
env_names = []
|
20
|
def usage_err():
|
21
|
raise SystemExit('Usage: '+opts.env_usage(env_names, True)+' '
|
22
|
+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]+' map_2]... >out_map')
|
23
|
|
24
|
only_one = opts.env_flag('only_one', False, env_names)
|
25
|
# Turns off "No input mapping" errors
|
26
|
quiet = opts.env_flag('quiet', False, env_names)
|
27
|
try: _prog_name, map_1_path = sys.argv
|
28
|
except ValueError: usage_err()
|
29
|
|
30
|
# Get map 1
|
31
|
map_1 = {}
|
32
|
stream = open(map_1_path, 'rb')
|
33
|
reader = csv.reader(stream)
|
34
|
map_1_cols = reader.next()
|
35
|
for row in reader:
|
36
|
if row[0] != '': map_1.setdefault(row[0], []).append(row)
|
37
|
stream.close()
|
38
|
|
39
|
# Join map 1 to map 0
|
40
|
reader = csv.reader(sys.stdin)
|
41
|
writer = csv.writer(sys.stdout)
|
42
|
map_0_cols = reader.next()
|
43
|
if not maps.join_combinable(map_0_cols, map_1_cols):
|
44
|
raise SystemExit('Map error: Map 0 output column name "'+map_0_cols[1]
|
45
|
+'" doesn\'t match map 1 input column name "'+map_1_cols[0]+'"')
|
46
|
writer.writerow(maps.merge_mappings(map_0_cols, map_1_cols))
|
47
|
for row in reader:
|
48
|
def set_error(msg):
|
49
|
warnings.warn(UserWarning(msg))
|
50
|
row[2] = '** '+msg+' ** '+util.list_setdefault(row, 2, '')
|
51
|
row[1] = ''
|
52
|
|
53
|
row_written = False
|
54
|
if row[1] != '':
|
55
|
out_orig = row[1] # used in "No join mapping" error msg
|
56
|
|
57
|
# Look for a match
|
58
|
out_rows = []
|
59
|
suffix = ''
|
60
|
while True:
|
61
|
try:
|
62
|
out_rows = map_1[row[1]]
|
63
|
break
|
64
|
except KeyError:
|
65
|
# Heuristically look for a match on a parent path.
|
66
|
# If this produces a syntactically invalid parent path (e.g.
|
67
|
# there is a / within []), it will be ignored since there
|
68
|
# wouldn't be a an entry in map_1.
|
69
|
row[1], sep, new_suffix = row[1].rpartition('/')
|
70
|
if sep == '': break
|
71
|
suffix = sep+new_suffix+suffix # prepend new suffix
|
72
|
|
73
|
# Write new mapping
|
74
|
is_empty = len(out_rows) == 1 and out_rows[0][1] == ''
|
75
|
if only_one and len(out_rows) > 1: # multiple outputs are ambiguous
|
76
|
set_error('Ambiguous mapping for '+row[1]) # discards mapping
|
77
|
elif out_rows and not is_empty: # found non-empty mapping(s)
|
78
|
for out_row in out_rows:
|
79
|
row_ = row[:] # don't modify row, since it will be reused
|
80
|
row_ = maps.merge_mappings(row_, out_row)
|
81
|
if row_[1] != '': row_[1] += suffix # don't modify out_row!
|
82
|
writer.writerow(row_)
|
83
|
row_written = True
|
84
|
else:
|
85
|
msg = 'No'
|
86
|
if is_empty: msg += ' non-empty'
|
87
|
msg += ' join mapping for '+out_orig
|
88
|
set_error(msg)
|
89
|
elif row[2] == '': # also no comment explaining why no input mapping
|
90
|
if not quiet: set_error('No input mapping for '+row[0])
|
91
|
|
92
|
if not row_written: writer.writerow(row)
|
93
|
|
94
|
main()
|