From e40b9cd06e955bd63c248c8922c6bf0c1073f885 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Mon, 7 Apr 2014 16:34:58 +0300 Subject: [PATCH] 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 --- keystone/common/authorization.py | 19 ------------------- keystone/common/controller.py | 8 ++++---- keystone/common/utils.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/keystone/common/authorization.py b/keystone/common/authorization.py index 6dc74356df..1aae1b0301 100644 --- a/keystone/common/authorization.py +++ b/keystone/common/authorization.py @@ -16,8 +16,6 @@ # License for the specific language governing permissions and limitations # under the License. -import collections - from keystone.contrib import federation from keystone import exception from keystone.openstack.common.gettextutils import _ @@ -43,23 +41,6 @@ It is a dictionary with the following attributes: LOG = log.getLogger(__name__) -def flatten(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(v, new_key).items()) - else: - items.append((new_key, v)) - return dict(items) - - def is_v3_token(token): # V3 token data are encapsulated into "token" key while # V2 token data are encapsulated into "access" key. diff --git a/keystone/common/controller.py b/keystone/common/controller.py index 993263a0a6..9f0917c341 100644 --- a/keystone/common/controller.py +++ b/keystone/common/controller.py @@ -147,7 +147,7 @@ def protected(callback=None): policy_dict.update(kwargs) self.policy_api.enforce(creds, action, - authorization.flatten(policy_dict)) + utils.flatten_dict(policy_dict)) LOG.debug(_('RBAC: Authorization granted')) return f(self, context, *args, **kwargs) return inner @@ -188,7 +188,7 @@ def filterprotected(*filters): self.policy_api.enforce(creds, action, - authorization.flatten(target)) + utils.flatten_dict(target)) LOG.debug(_('RBAC: Authorization granted')) else: @@ -452,7 +452,7 @@ class V3Controller(wsgi.Application): attr = filter['name'] value = filter['value'] refs = [r for r in refs if _attr_match( - authorization.flatten(r).get(attr), value)] + utils.flatten_dict(r).get(attr), value)] else: # It might be an inexact filter refs = [r for r in refs if _inexact_attr_match( @@ -612,7 +612,7 @@ class V3Controller(wsgi.Application): policy_dict.update(prep_info['input_attr']) self.policy_api.enforce(creds, action, - authorization.flatten(policy_dict)) + utils.flatten_dict(policy_dict)) LOG.debug(_('RBAC: Authorization granted')) @classmethod diff --git a/keystone/common/utils.py b/keystone/common/utils.py index c720d21343..51e995752c 100644 --- a/keystone/common/utils.py +++ b/keystone/common/utils.py @@ -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.