Project

General

Profile

1
# Map spreadsheet manipulation
2

    
3
import strings
4
import util
5

    
6
def col_label(col_name): return col_name.partition(':')[0]
7

    
8
def combinable(*headers):
9
    return strings.overlaps(*[col_label(header[0]) for header in headers])
10

    
11
def is_nonexplicit_empty_mapping(row):
12
    return reduce(util.and_, (v == '' for v in row[1:]))
13

    
14
def merge_values(*vals):
15
    new = []
16
    for val in vals:
17
        if val != '' and val not in new: new.append(val)
18
    return '; '.join(new)
19

    
20
def merge_rows(*rows):
21
    '''e.g. ['a','b'] + ['','y','z'] = ['a','b; y','z']'''
22
    def get(row, i):
23
        try: return row[i]
24
        except IndexError: return ''
25
    return [merge_values(*[get(row, i) for row in rows])
26
        for i in xrange(max(map(len, rows)))]
27

    
28
def merge_mappings(in_, out):
29
    '''e.g. ['in','join','in_comments'] + ['join','out','out_comments'] =
30
    ['in','out','in_comments; out_comments']'''
31
    new = [in_[0], out[1]] # mapping
32
    new[2:] = merge_rows(in_[2:], out[2:]) # comments
33
    return new
(9-9/20)