Revision 1704
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/maps.py | ||
---|---|---|
1 | 1 |
# Map spreadsheet manipulation |
2 | 2 |
|
3 |
import re |
|
4 |
|
|
5 |
import Parser |
|
3 | 6 |
import strings |
4 | 7 |
import util |
5 | 8 |
|
6 |
def col_label(col_name): return col_name.partition(':')[0] |
|
9 |
def col_info(col_name, require_root=False): |
|
10 |
'''@return tuple (label, root, prefixes)''' |
|
11 |
def syntax_err(): raise Parser.SyntaxException('Column name must have ' |
|
12 |
'syntax "datasrc[format,...]:root" (formats optional): '+col_name) |
|
13 |
|
|
14 |
match = re.match(r'^([^\[:]*)(?:\[([^\]]*?)\])?(?::(.*))?$', col_name) |
|
15 |
if not match: syntax_err() |
|
16 |
label, prefixes, root = match.groups() |
|
17 |
if require_root and root == None: syntax_err() |
|
18 |
return label, root, strings.split(',', util.coalesce(prefixes, '')) |
|
7 | 19 |
|
20 |
def col_formats(col_name): |
|
21 |
label, root, prefixes = col_info(col_name) |
|
22 |
return [label]+prefixes |
|
23 |
|
|
8 | 24 |
def combinable(*headers): |
9 |
return strings.overlaps(*[col_label(header[0]) for header in headers])
|
|
25 |
return strings.overlaps(*[','.join(col_formats(h[0])) for h in headers])
|
|
10 | 26 |
|
11 | 27 |
def is_nonexplicit_empty_mapping(row): |
12 | 28 |
return reduce(util.and_, (v == '' for v in row[1:])) |
Also available in: Unified diff
maps.py: Added col_info() to get label, root, prefixes from col_name. Added col_formats() for use by combinable(). Use new col_formats() in combinable(). Removed no longer needed col_label().