Project

General

Profile

1
# Map spreadsheet manipulation
2

    
3
import re
4

    
5
import Parser
6
import strings
7
import util
8

    
9
##### Metadata
10

    
11
def col_info(col_name, require_root=False):
12
    '''@return tuple (label, root, prefixes)'''
13
    def syntax_err(): raise Parser.SyntaxError('Column name must have '
14
        'syntax "datasrc[format,...]:root" (formats optional): '+col_name)
15
    
16
    match = re.match(r'^([^\[:]*)(?:\[([^\]]*?)\])?(?::(.*))?$', col_name)
17
    if not match: syntax_err()
18
    label, prefixes, root = match.groups()
19
    if require_root and root == None: syntax_err()
20
    return label, root, strings.split(',', util.coalesce(prefixes, ''))
21

    
22
##### Combinability
23

    
24
def col_formats(col_name):
25
    label, root, prefixes = col_info(col_name)
26
    return [label]+prefixes
27

    
28
def cols_combinable(*col_names):
29
    return strings.overlaps(*[','.join(col_formats(c)) for c in col_names])
30

    
31
def combinable(*headers): return cols_combinable(*[h[0] for h in headers])
32

    
33
def strip_root(root): return re.sub(r':\[[^\]]*?\]', r'', root)
34

    
35
def stripped_root(col_name):
36
    '''@return NamedTuple (labels, root)'''
37
    return util.do_ignore_none(strip_root, col_info(col_name)[1])
38

    
39
def cols_root_combinable(*col_names):
40
    roots = map(stripped_root, col_names)
41
    return roots[0] == None or roots[1] == None or roots[0] == roots[1]
42

    
43
def cols_fully_combinable(*col_names):
44
    return cols_combinable(*col_names) and cols_root_combinable(*col_names)
45

    
46
def join_combinable(*headers):
47
    return cols_fully_combinable(headers[0][1], headers[1][0])
48

    
49
##### Merging
50

    
51
def is_nonexplicit_empty_mapping(row):
52
    return reduce(util.and_, (v == '' for v in row[1:]))
53

    
54
def merge_values(*vals):
55
    new = []
56
    for val in vals:
57
        if val != '' and val not in new: new.append(val)
58
    return '; '.join(new)
59

    
60
def merge_rows(*rows):
61
    '''e.g. ['a','b'] + ['','y','z'] = ['a','b; y','z']'''
62
    def get(row, i):
63
        try: return row[i]
64
        except IndexError: return ''
65
    return [merge_values(*[get(row, i) for row in rows])
66
        for i in xrange(max(map(len, rows)))]
67

    
68
def merge_mapping_cols(in_, out, prefer=None):
69
    '''@param prefer None = [in_[0], out[1]]; 0 = in_; 1 = out'''
70
    if prefer == None: return [in_[0], out[1]]
71
    elif prefer == 0: return in_
72
    elif prefer == 1: return out
73
    else: raise Parser.SyntaxError('Invalid prefer: '+repr(prefer))
74

    
75
def merge_mappings(in_, out, **kw_args):
76
    '''e.g. ['in','join','in_comments'] + ['join','out','out_comments'] =
77
    ['in','out','in_comments; out_comments']'''
78
    return (merge_mapping_cols(in_[:2], out[:2], **kw_args)
79
        + merge_rows(in_[2:], out[2:]))
80

    
81
def merge_headers(in_, out, **kw_args):
82
    out_cols = [in_[1], out[1]]
83
    if cols_fully_combinable(*out_cols): out[1] = util.longest(*out_cols)
84
    return merge_mappings(in_, out, **kw_args)
(13-13/28)