Fix recursive_keypairs output

recursive_keypairs has 'separator' argument. This method is recursive
and a problem is that 'separator' argument is applied only in first
iteration of the method.

Besides, this method is used in flatten_metadata in API implementation.
That's why it is needed not to change output from recursive_keypairs in flatten_metadata.

So the path has a goal to fix incorrect recursive_keypairs impementation but
not to change output of flatten_matadata in API

Fixes bug 1268628

Change-Id: Ic9829342c47325c1df7b81dc4148c08a13e0d067
This commit is contained in:
Nadya Privalova
2014-01-19 12:45:56 +04:00
parent a00917a935
commit fb32c3b19c
2 changed files with 8 additions and 2 deletions

View File

@@ -510,7 +510,13 @@ def _flatten_metadata(metadata):
to unicode strings.
"""
if metadata:
return dict((k, unicode(v))
# After changing recursive_keypairs` output we need to keep
# flattening output unchanged.
# Example: recursive_keypairs({'a': {'b':{'c':'d'}}}, '.')
# output before: a.b:c=d
# output now: a.b.c=d
# So to keep the first variant just replace all dots except the first
return dict((k.replace('.', ':').replace(':', '.', 1), unicode(v))
for k, v in utils.recursive_keypairs(metadata,
separator='.')
if type(v) is not set)

View File

@@ -30,7 +30,7 @@ def recursive_keypairs(d, separator=':'):
"""
for name, value in sorted(d.iteritems()):
if isinstance(value, dict):
for subname, subvalue in recursive_keypairs(value):
for subname, subvalue in recursive_keypairs(value, separator):
yield ('%s%s%s' % (name, separator, subname), subvalue)
elif isinstance(value, (tuple, list)):
# When doing a pair of JSON encode/decode operations to the tuple,