Refactor: moved flatten function to utils

Previously, the keystone.common.authorization module contained the function
"flatten", which would transform a dictionary (possibly nested) into a
single level dictionary. Given that the "flattening" of a dictionary is
a more general concept, not related to authorization, I thought it would
be more intuitive to have it in the keystone.common.utils module.

Change-Id: Ic1f6fa70634ade41e392ee7c5cd8adac913fd0b8
This commit is contained in:
Juan Antonio Osorio
2014-04-07 16:34:58 +03:00
committed by Gerrit Code Review
parent 9b580d286a
commit e40b9cd06e
3 changed files with 22 additions and 23 deletions

View File

@@ -17,6 +17,7 @@
# under the License.
import calendar
import collections
import grp
import hashlib
import json
@@ -40,6 +41,23 @@ CONF = config.CONF
LOG = log.getLogger(__name__)
def flatten_dict(d, parent_key=''):
"""Flatten a nested dictionary
Converts a dictionary with nested values to a single level flat
dictionary, with dotted notation for each key.
"""
items = []
for k, v in d.items():
new_key = parent_key + '.' + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten_dict(v, new_key).items())
else:
items.append((new_key, v))
return dict(items)
def read_cached_file(filename, cache_info, reload_func=None):
"""Read from a file if it has been modified.