Revision 1125
Added by Aaron Marcuse-Kubitza almost 13 years ago
union | ||
---|---|---|
12 | 12 |
|
13 | 13 |
def col_label(col_name): return col_name.partition(':')[0] |
14 | 14 |
|
15 |
def overlaps(str0, str1): return str0.find(str1) >= 0 or str1.find(str0) >= 0 |
|
16 |
|
|
15 | 17 |
def main(): |
16 | 18 |
ignore = opts.env_flag('ignore') |
17 | 19 |
try: _prog_name, map_1_path = sys.argv |
... | ... | |
19 | 21 |
raise SystemExit('Usage: env [ignore=1] '+sys.argv[0] |
20 | 22 |
+' <map_0 map_1 [| '+sys.argv[0]+' map_2]... >union_map') |
21 | 23 |
|
22 |
map_ = {} |
|
23 |
def add_map(reader): |
|
24 |
for row in reader: |
|
25 |
if row[0] != '': |
|
26 |
try: new = map_[row[0]] |
|
27 |
except KeyError: new = row |
|
28 |
else: new = maps.merge_mappings(new, row) |
|
29 |
map_[row[0]] = new |
|
24 |
headers = [None]*2 |
|
30 | 25 |
|
31 |
# Get map 1 |
|
26 |
# Open map 0 |
|
27 |
map_0_reader = csv.reader(sys.stdin) |
|
28 |
headers[0] = map_0_reader.next() |
|
29 |
|
|
30 |
# Open map 1 |
|
32 | 31 |
stream = open(map_1_path, 'rb') |
33 |
reader = csv.reader(stream) |
|
34 |
map_1_cols = reader.next() |
|
35 |
add_map(reader) |
|
36 |
stream.close() |
|
32 |
map_1_reader = csv.reader(stream) |
|
33 |
headers[1] = map_1_reader.next() |
|
37 | 34 |
|
38 |
# Add map 0 to map 1, overwriting existing entries |
|
39 |
reader = csv.reader(sys.stdin) |
|
40 |
map_0_cols = reader.next() |
|
41 |
if not col_label(map_0_cols[0]).find(col_label(map_1_cols[0])) >= 0: |
|
42 |
if ignore: sys.exit() |
|
43 |
else: raise SystemExit('Map error: Map 0 column 0 label doesn\'t ' |
|
44 |
'contain map 1 column 0 label') |
|
45 |
add_map(reader) |
|
35 |
# Check col labels |
|
36 |
combinable = overlaps(*[col_label(header[0]) for header in headers]) |
|
37 |
if not combinable and not ignore: raise SystemExit('Map error: ' |
|
38 |
'Map 0 column 0 label doesn\'t contain map 1 column 0 label') |
|
46 | 39 |
|
47 |
# Write combined map
|
|
40 |
# Pass through map 0, storing which inputs it defines
|
|
48 | 41 |
writer = csv.writer(sys.stdout) |
49 |
writer.writerow(maps.merge_mappings(map_1_cols, map_0_cols)) |
|
50 |
for row in map_.itervalues(): writer.writerow(row) |
|
42 |
writer.writerow(headers[0]) |
|
43 |
inputs = set() |
|
44 |
for row in map_0_reader: |
|
45 |
if row[0] != '': inputs.add(row[0]) |
|
46 |
writer.writerow(row) |
|
47 |
|
|
48 |
if combinable: |
|
49 |
# Add entries in map 1 that weren't already defined |
|
50 |
for row in map_1_reader: |
|
51 |
if row[0] != '' and row[0] not in inputs: writer.writerow(row) |
|
52 |
|
|
53 |
stream.close() |
|
51 | 54 |
|
52 | 55 |
main() |
Also available in: Unified diff
union: Pass through map 0, so that if ignore is set, the input map will still be output. Allow either map's input label to contain the other's input label to enable e.g. appending mappings for an older input version to those for a newer input version.