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