Skip to content

Commit

Permalink
Fix bug in map_keys and add imporvements
Browse files Browse the repository at this point in the history
  • Loading branch information
emi80 committed Oct 14, 2014
1 parent aae13f1 commit 4aa64ad
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions indexfile/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,38 +510,41 @@ def parse_line(cls, line, **kwargs):
return tagsd

@classmethod
def map_keys(cls, obj, **kwargs):
def map_keys(cls, obj, map_only=True, **kwargs):
""" Maps ``obj`` keys using the mapping information contained
in the arguments. ``kwargs`` is used to specify the index format
information.
:param obj: the input dictionary
"""
# TODO: use map_keys as a general function for mapping
if not obj:
log.debug('No data to map')
return {}

dsid = kwargs.get('id')
idxmap = kwargs.get('map', {})

out = {}
# return original dict if no id and map definitions are found
if not idxmap and not dsid:
return obj

out = dict(obj.items())
if idxmap:
log.debug('Mappings present')
for k, val in idxmap.items():
if not val:
idxmap.pop(k)
continue
idxmap[val] = k

log.debug('Create output dictionary')
for k, val in obj.items():
key = k
if map:
log.debug('Map %s to %s', key, idxmap.get(key))
key = idxmap.get(k)
if key:
if key == dsid:
key = "id"
out[key] = val
log.debug('Mapping attribute names using map: %s', idxmap)
if map_only:
# use known attributes with non-empty mapping
log.debug("Using only known mappings from the map")
out = dict([(idxmap.get(k), v) for (k, v) in out.iteritems()
if idxmap.get(k)])
else:
log.debug("Using input attributes if no mapping found")
out = dict([(idxmap.get(k), v) if idxmap.get(k)
else (k, v) for (k, v) in out.iteritems()])
if dsid:
log.debug("Mapping 'id' to %r", dsid)
out = dict([('id', v) if k == dsid else (k, v)
for (k, v) in out.iteritems()])

return out

0 comments on commit 4aa64ad

Please sign in to comment.