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