Merge "Move the container fetcher function to utils"

This commit is contained in:
Jenkins
2014-01-20 18:58:19 +00:00
committed by Gerrit Code Review
2 changed files with 20 additions and 19 deletions

View File

@@ -32,23 +32,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
@@ -250,7 +233,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)",
@@ -403,7 +386,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

@@ -75,6 +75,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)