Move the container fetcher function to utils

Change-Id: If4d340df7b4627a5290daaed9fa650d704dc6d82
This commit is contained in:
Joshua Harlow
2013-12-30 11:29:08 -08:00
parent 9632fe6392
commit dd1e1e8312
2 changed files with 20 additions and 19 deletions

View File

@@ -31,23 +31,6 @@ LOG = logging.getLogger(__name__)
STATES_WITH_RESULTS = (states.SUCCESS, states.REVERTING, states.FAILURE)
def _item_from_result(result, index, name):
"""Attempts to fetch a index/key from a given result."""
if index is None:
return result
try:
return result[index]
except (IndexError, KeyError, ValueError, TypeError):
# NOTE(harlowja): The result that the uuid returned can not be
# accessed in the manner that the index is requesting. Perhaps
# the result is a dictionary-like object and that key does
# not exist (key error), or the result is a tuple/list and a
# non-numeric key is being requested (index error), or there
# was no result and an attempt to index into None is being
# requested (type error).
raise exceptions.NotFound("Unable to find result %r" % name)
class Storage(object):
"""Interface between engines and logbook
@@ -249,7 +232,7 @@ class Storage(object):
return
for name, index in six.iteritems(result_mapping):
try:
_item_from_result(data, index, name)
misc.item_from(data, index, name=name)
except exceptions.NotFound:
LOG.warning("Task %s did not supply result "
"with index %r (name %s)",
@@ -402,7 +385,7 @@ class Storage(object):
for task_name, index in reversed(indexes):
try:
result = self.get(task_name)
return _item_from_result(result, index, name)
return misc.item_from(result, index, name=name)
except exceptions.NotFound:
pass
raise exceptions.NotFound("Unable to find result %r" % name)

View File

@@ -74,6 +74,24 @@ def get_version_string(obj):
return obj_version
def item_from(container, index, name=None):
"""Attempts to fetch a index/key from a given container."""
if index is None:
return container
try:
return container[index]
except (IndexError, KeyError, ValueError, TypeError):
# NOTE(harlowja): Perhaps the container is a dictionary-like object
# and that key does not exist (key error), or the container is a
# tuple/list and a non-numeric key is being requested (index error),
# or there was no container and an attempt to index into none/other
# unsubscriptable type is being requested (type error).
if name is None:
name = index
raise exceptions.NotFound("Unable to find %r in container %s"
% (name, container))
def get_duplicate_keys(iterable, key=None):
if key is not None:
iterable = six.moves.map(key, iterable)