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
            try: out_row = map_1[row[1]]
38
            except KeyError:
39
                msg = 'No join mapping for '+row[1]
40
                warnings.warn(UserWarning(msg))
41
                row[2] = '** '+msg+' ** '+row[2]
42
                row[1] = ''
43
            else: row = maps.merge_mappings(row, out_row)
44
        writer.writerow(row)
45

    
46
main()
(10-10/23)