Project

General

Profile

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 3777 aaronmk
        row_written = False
46 730 aaronmk
        if row[1] != '':
47 1283 aaronmk
            out_orig = row[1] # used in "No join mapping" error msg
48
49 1170 aaronmk
            # Look for a match
50 3766 aaronmk
            out_rows = []
51 1170 aaronmk
            suffix = ''
52
            while True:
53
                try:
54 3766 aaronmk
                    out_rows = map_1[row[1]]
55 1170 aaronmk
                    break
56
                except KeyError:
57
                    # Heuristically look for a match on a parent path.
58
                    # If this produces a syntactically invalid parent path (e.g.
59
                    # there is a / within []), it will be ignored since there
60
                    # wouldn't be a an entry in map_1.
61
                    row[1], sep, new_suffix = row[1].rpartition('/')
62
                    if sep == '': break
63
                    suffix = sep+new_suffix+suffix # prepend new suffix
64
65
            # Write new mapping
66 3766 aaronmk
            is_empty = len(out_rows) == 1 and out_rows[0][1] == ''
67
            if out_rows and not is_empty: # found non-empty mapping(s)
68
                for out_row in out_rows:
69 3777 aaronmk
                    row_ = row[:] # don't modify row, since it will be reused
70
                    row_ = maps.merge_mappings(row_, out_row)
71
                    if row_[1] != '': row_[1] += suffix # don't modify out_row!
72
                    writer.writerow(row_)
73
                row_written = True
74 1800 aaronmk
            else:
75
                msg = 'No'
76 3766 aaronmk
                if is_empty: msg += ' non-empty'
77 1800 aaronmk
                msg += ' join mapping for '+out_orig
78
                set_error(msg)
79 1739 aaronmk
        elif row[2] == '': # also no comment explaining why no input mapping
80
            set_error('No input mapping for '+row[0])
81
82 3777 aaronmk
        if not row_written: writer.writerow(row)
83 51 aaronmk
84
main()