Revision 2040
Added by Aaron Marcuse-Kubitza over 12 years ago
map | ||
---|---|---|
13 | 13 |
sys.path.append(os.path.dirname(__file__)+"/../lib") |
14 | 14 |
|
15 | 15 |
import csvs |
16 |
import db_xml |
|
16 | 17 |
import exc |
17 | 18 |
import iters |
18 | 19 |
import maps |
... | ... | |
31 | 32 |
import xml_parse |
32 | 33 |
|
33 | 34 |
def get_with_prefix(map_, prefixes, key): |
34 |
'''Gets all entries for the given key with any of the given prefixes''' |
|
35 |
'''Gets all entries for the given key with any of the given prefixes |
|
36 |
@return tuple(found_key, found_value) |
|
37 |
''' |
|
35 | 38 |
values = [] |
36 | 39 |
for key_ in strings.with_prefixes(['']+prefixes, key): # also with no prefix |
37 | 40 |
try: value = map_[key_] |
38 | 41 |
except KeyError, e: continue # keep going |
39 |
values.append(value)
|
|
42 |
values.append((key_, value))
|
|
40 | 43 |
|
41 | 44 |
if values != []: return values |
42 | 45 |
else: raise e # re-raise last KeyError |
... | ... | |
259 | 262 |
mappings[:] = [] # empty existing elements |
260 | 263 |
for in_, out in mappings_orig: |
261 | 264 |
if metadata_value(in_) == None: |
262 |
try: names = get_with_prefix(col_idxs, prefixes, in_)
|
|
265 |
try: cols = get_with_prefix(col_idxs, prefixes, in_)
|
|
263 | 266 |
except KeyError: pass |
264 |
else: mappings[len(mappings):] = [[v, out] for v in names]
|
|
265 |
# can't use += because that uses assignment
|
|
267 |
else: mappings[len(mappings):] = [[db_xml.ColRef(*col), out]
|
|
268 |
for col in cols] # can't use += because that uses =
|
|
266 | 269 |
|
267 |
def get_value(in_, row): return row.list[in_] |
|
270 |
def get_value(in_, row): return row.list[in_.idx]
|
|
268 | 271 |
def wrap_row(row): |
269 | 272 |
return util.ListDict(util.list_as_length(row, col_names_ct), |
270 | 273 |
col_names, col_idxs) # handle CSV rows of different lengths |
Also available in: Unified diff
bin/map: map_table(): Make each column a db_xml.ColRef instead of a bare index, so that it will appear as the column name when converted to a string. This will provide better debugging info in the template tree and also avoid needing to create a separate sample row in by_col.