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