Allow passing 'many_handler' to fetch_all function

When many values are found for a given result a callback
is called to determine what to return. To match the fetch
routine allow for the fetch_all function to pass along a
handler (and default to one that is the existing behavior)
so that people can extract there own values (if they so
choose).

Change-Id: I8c105e1a810e8e0c2c210613506c9ce59021009d
This commit is contained in:
Joshua Harlow
2015-03-12 15:18:36 -07:00
parent cebdb3ef97
commit 526f287201

View File

@@ -644,11 +644,13 @@ class Storage(object):
@lock_utils.read_locked
def fetch(self, name, many_handler=None):
"""Fetch a named result."""
# By default we just return the first of many (unless provided
# a different callback that can translate many results into something
# more meaningful).
def _many_handler(values):
# By default we just return the first of many (unless provided
# a different callback that can translate many results into
# something more meaningful).
return values[0]
if many_handler is None:
many_handler = lambda values: values[0]
many_handler = _many_handler
try:
providers = self._reverse_mapping[name]
except KeyError:
@@ -758,12 +760,14 @@ class Storage(object):
return missing
@lock_utils.read_locked
def fetch_all(self):
def fetch_all(self, many_handler=None):
"""Fetch all named results known so far."""
def many_handler(values):
def _many_handler(values):
if len(values) > 1:
return values
return values[0]
if many_handler is None:
many_handler = _many_handler
results = {}
for name in six.iterkeys(self._reverse_mapping):
try: