From dd1e1e8312fffd242e9b8c260acd88903b51baae Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Mon, 30 Dec 2013 11:29:08 -0800 Subject: [PATCH] Move the container fetcher function to utils Change-Id: If4d340df7b4627a5290daaed9fa650d704dc6d82 --- taskflow/storage.py | 21 ++------------------- taskflow/utils/misc.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/taskflow/storage.py b/taskflow/storage.py index b4b9f3f5..1a723119 100644 --- a/taskflow/storage.py +++ b/taskflow/storage.py @@ -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) diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py index 3ec136c7..942d638e 100644 --- a/taskflow/utils/misc.py +++ b/taskflow/utils/misc.py @@ -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)