Project

General

Profile

« Previous | Next » 

Revision 3822

join: Support discarding multiple outputs if they should be considered ambiguous

View differences:

join
1 1
#!/usr/bin/env python
2 2
# Inner-joins two map spreadsheets A->B and B->C to A->C
3
# Multi-safe (supports an input appearing multiple times).
3
# Multi-safe (supports an input appearing multiple times). If multiple outputs
4
# should be considered ambiguous, they can be discarded by setting only_one.
4 5

  
5 6
import csv
6 7
import operator
......
11 12
sys.path.append(os.path.dirname(__file__)+"/../lib")
12 13

  
13 14
import maps
15
import opts
14 16
import util
15 17

  
16 18
def main():
19
    only_one = opts.env_flag('only_one')
17 20
    try: _prog_name, map_1_path = sys.argv
18 21
    except ValueError:
19
        raise SystemExit('Usage: '+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]
20
            +' map_2]... >joined_map')
22
        raise SystemExit('Usage: env [only_one=1] '+sys.argv[0]
23
            +' <map_0 map_1 [| '+sys.argv[0]+' map_2]... >joined_map')
21 24
    
22 25
    # Get map 1
23 26
    map_1 = {}
......
64 67
            
65 68
            # Write new mapping
66 69
            is_empty = len(out_rows) == 1 and out_rows[0][1] == ''
67
            if out_rows and not is_empty: # found non-empty mapping(s)
70
            if only_one and len(out_rows) > 1: # multiple outputs are ambiguous
71
                set_error('Ambiguous mapping for '+row[1]) # discards mapping
72
            elif out_rows and not is_empty: # found non-empty mapping(s)
68 73
                for out_row in out_rows:
69 74
                    row_ = row[:] # don't modify row, since it will be reused
70 75
                    row_ = maps.merge_mappings(row_, out_row)

Also available in: Unified diff