Project

General

Profile

1
#!/usr/bin/env python
2
# Inner-joins two map spreadsheets A->B and B->C to A->C
3
# Multi-safe (supports an input appearing multiple times).
4

    
5
import csv
6
import operator
7
import os.path
8
import sys
9
import warnings
10

    
11
sys.path.append(os.path.dirname(__file__)+"/../lib")
12

    
13
import maps
14
import util
15

    
16
def main():
17
    try: _prog_name, map_1_path = sys.argv
18
    except ValueError:
19
        raise SystemExit('Usage: '+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]
20
            +' map_2]... >joined_map')
21
    
22
    # Get map 1
23
    map_1 = {}
24
    stream = open(map_1_path, 'rb')
25
    reader = csv.reader(stream)
26
    map_1_cols = reader.next()
27
    for row in reader:
28
        if row[0] != '': map_1.setdefault(row[0], []).append(row)
29
    stream.close()
30
    
31
    # Join map 1 to map 0
32
    reader = csv.reader(sys.stdin)
33
    writer = csv.writer(sys.stdout)
34
    map_0_cols = reader.next()
35
    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
        +'" doesn\'t match map 1 input column name "'+map_1_cols[0]+'"')
38
    writer.writerow(maps.merge_mappings(map_0_cols, map_1_cols))
39
    for row in reader:
40
        def set_error(msg):
41
            warnings.warn(UserWarning(msg))
42
            row[2] = '** '+msg+' ** '+util.list_setdefault(row, 2, '')
43
            row[1] = ''
44
        
45
        if row[1] != '':
46
            out_orig = row[1] # used in "No join mapping" error msg
47
            
48
            # Look for a match
49
            out_rows = []
50
            suffix = ''
51
            while True:
52
                try:
53
                    out_rows = map_1[row[1]]
54
                    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
            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
            else:
71
                msg = 'No'
72
                if is_empty: msg += ' non-empty'
73
                msg += ' join mapping for '+out_orig
74
                set_error(msg)
75
        elif row[2] == '': # also no comment explaining why no input mapping
76
            set_error('No input mapping for '+row[0])
77
        
78
        writer.writerow(row)
79

    
80
main()
(23-23/52)