Merge "Move the container fetcher function to utils"
This commit is contained in:
@@ -32,23 +32,6 @@ LOG = logging.getLogger(__name__)
|
|||||||
STATES_WITH_RESULTS = (states.SUCCESS, states.REVERTING, states.FAILURE)
|
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):
|
class Storage(object):
|
||||||
"""Interface between engines and logbook
|
"""Interface between engines and logbook
|
||||||
|
|
||||||
@@ -250,7 +233,7 @@ class Storage(object):
|
|||||||
return
|
return
|
||||||
for name, index in six.iteritems(result_mapping):
|
for name, index in six.iteritems(result_mapping):
|
||||||
try:
|
try:
|
||||||
_item_from_result(data, index, name)
|
misc.item_from(data, index, name=name)
|
||||||
except exceptions.NotFound:
|
except exceptions.NotFound:
|
||||||
LOG.warning("Task %s did not supply result "
|
LOG.warning("Task %s did not supply result "
|
||||||
"with index %r (name %s)",
|
"with index %r (name %s)",
|
||||||
@@ -403,7 +386,7 @@ class Storage(object):
|
|||||||
for task_name, index in reversed(indexes):
|
for task_name, index in reversed(indexes):
|
||||||
try:
|
try:
|
||||||
result = self.get(task_name)
|
result = self.get(task_name)
|
||||||
return _item_from_result(result, index, name)
|
return misc.item_from(result, index, name=name)
|
||||||
except exceptions.NotFound:
|
except exceptions.NotFound:
|
||||||
pass
|
pass
|
||||||
raise exceptions.NotFound("Unable to find result %r" % name)
|
raise exceptions.NotFound("Unable to find result %r" % name)
|
||||||
|
|||||||
@@ -75,6 +75,24 @@ def get_version_string(obj):
|
|||||||
return obj_version
|
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):
|
def get_duplicate_keys(iterable, key=None):
|
||||||
if key is not None:
|
if key is not None:
|
||||||
iterable = six.moves.map(key, iterable)
|
iterable = six.moves.map(key, iterable)
|
||||||
|
|||||||
Reference in New Issue
Block a user