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
        row_written = False
46
        if row[1] != '':
47
            out_orig = row[1] # used in "No join mapping" error msg
48
            
49
            # Look for a match
50
            out_rows = []
51
            suffix = ''
52
            while True:
53
                try:
54
                    out_rows = map_1[row[1]]
55
                    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
            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
                    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
            else:
75
                msg = 'No'
76
                if is_empty: msg += ' non-empty'
77
                msg += ' join mapping for '+out_orig
78
                set_error(msg)
79
        elif row[2] == '': # also no comment explaining why no input mapping
80
            set_error('No input mapping for '+row[0])
81
        
82
        if not row_written: writer.writerow(row)
83

    
84
main()
(25-25/54)