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