Add helper shim for getting items

Within the wsgi module, the wsgi request object has a cache_db_items()
function that does a dictionary __getitem__ on the item passed in.
Because the objects are being changed over to remove dictionary syntax,
__getitem__ on these objects now fails. But the item being passed in is
not always a versioned object, so we can't just totally drop the
__getitem__ to use getattr(). To keep compatibility between them, a
function is added to check if the item has __getitem__. If it does,
we'll use it as a dictionary, otherwise, we'll use getattr().

Change-Id: I84b76d2fd344e0f53ffaada3ed74d3b97de4f962
Partially-Implements: bp rm-object-dict-compat
This commit is contained in:
Ryan Rossiter 2016-01-11 19:08:03 +00:00
parent 5d6e0086b5
commit e11f788ca7

View File

@ -86,6 +86,17 @@ def get_media_map():
return dict(_MEDIA_TYPE_MAP.items())
# NOTE(rlrossit): This function allows a get on both a dict-like and an
# object-like object. cache_db_items() is used on both versioned objects and
# dicts, so the function can't be totally changed over to [] syntax, nor
# can it be changed over to use getattr().
def item_get(item, item_key):
if hasattr(item, '__getitem__'):
return item[item_key]
else:
return getattr(item, item_key)
class Request(wsgi.Request):
"""Add some OpenStack API-specific logic to the base webob.Request."""
@ -105,7 +116,7 @@ class Request(wsgi.Request):
"""
db_items = self._extension_data['db_items'].setdefault(key, {})
for item in items:
db_items[item[item_key]] = item
db_items[item_get(item, item_key)] = item
def get_db_items(self, key):
"""Allow an API extension to get previously stored objects within