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