Project

General

Profile

1
#!/usr/bin/env python
2
# Inner-joins two map spreadsheets A->B and B->C to A->C
3

    
4
import csv
5
import os.path
6
import sys
7
import warnings
8

    
9
sys.path.append(os.path.dirname(__file__)+"/../lib")
10

    
11
import maps
12

    
13
def main():
14
    try: _prog_name, map_1_path = sys.argv
15
    except ValueError:
16
        raise SystemExit('Usage: '+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]
17
            +' map_2]... >joined_map')
18
    
19
    # Get map 1
20
    map_1 = {}
21
    stream = open(map_1_path, 'rb')
22
    reader = csv.reader(stream)
23
    map_1_cols = reader.next()
24
    for row in reader:
25
        if row[0] != '': map_1[row[0]] = row
26
    stream.close()
27
    
28
    # Join map 1 to map 0
29
    reader = csv.reader(sys.stdin)
30
    writer = csv.writer(sys.stdout)
31
    map_0_cols = reader.next()
32
    if not map_0_cols[1] == map_1_cols[0]: raise SystemExit('Map error: '
33
        'Map 0 output column name doesn\'t match map 1 input column name')
34
    writer.writerow(maps.merge_mappings(map_0_cols, map_1_cols))
35
    for row in reader:
36
        if row[1] != '':
37
            # Look for a match
38
            out_row = None
39
            suffix = ''
40
            while True:
41
                try:
42
                    out_row = map_1[row[1]]
43
                    break
44
                except KeyError:
45
                    # Heuristically look for a match on a parent path.
46
                    # If this produces a syntactically invalid parent path (e.g.
47
                    # there is a / within []), it will be ignored since there
48
                    # wouldn't be a an entry in map_1.
49
                    row[1], sep, new_suffix = row[1].rpartition('/')
50
                    if sep == '': break
51
                    suffix = sep+new_suffix+suffix # prepend new suffix
52
            
53
            # Write new mapping
54
            if out_row != None:
55
                row = maps.merge_mappings(row, out_row)
56
                row[1] += suffix # don't modify out_row!
57
            else:
58
                msg = 'No join mapping for '+row[1]
59
                warnings.warn(UserWarning(msg))
60
                row[2] = '** '+msg+' ** '+row[2]
61
                row[1] = ''
62
        writer.writerow(row)
63

    
64
main()
(12-12/25)