summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorl-m-h@web.de <l-m-h@web.de>2017-12-07 12:40:48 +0100
committerDavid Bremner <david@tethera.net>2017-12-19 06:40:47 -0400
commite745857c4a40f081560bdbe2b75ec1eafddea441 (patch)
tree603046f43de6b04cd93f473182c73725256db1f0
parentc536de3afb365dbbb3f85755f149383285c53b9c (diff)
python: turn get_config_list into a generator
This mimics the behaviour of the underlying C function more closely as it also does not store all values in memory.
-rw-r--r--bindings/python/notmuch/database.py30
1 files changed, 20 insertions, 10 deletions
diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py
index 54966307..32566620 100644
--- a/bindings/python/notmuch/database.py
+++ b/bindings/python/notmuch/database.py
@@ -690,24 +690,22 @@ class Database(object):
_config_list_destroy.restype = None
def get_config_list(self, prefix=''):
- """Return a list of key, value pairs where the start of key matches the
- given prefix
+ """Return a generator of key, value pairs where the start of key
+ matches the given prefix
Note that only config values that are stored in the database are
searched and returned. The config file is not read. If no `prefix` is
given all config values are returned.
- This could be used to get all config values or all named queries into a
- dict for example::
+ This could be used to get all named queries into a dict for example::
- config = {k: v for k, v in db.get_config_list()}
queries = {k[6:]: v for k, v in db.get_config_list('query.')}
:param prefix: a string by which the keys should be selected
:type prefix: str
- :returns: all key-value pairs where `prefix` matches the beginning
+ :yields: all key-value pairs where `prefix` matches the beginning
of the key
- :rtype: a list of pairs of str
+ :ytype: pairs of str
:raises: :exc:`NotmuchError` in case of failure.
"""
@@ -717,13 +715,25 @@ class Database(object):
byref(config_list_p))
if status != STATUS.SUCCESS:
raise NotmuchError(status)
- config_list = []
while self._config_list_valid(config_list_p):
key = self._config_list_key(config_list_p).decode('utf-8')
value = self._config_list_value(config_list_p).decode('utf-8')
- config_list.append((key, value))
+ yield key, value
self._config_list_move_to_next(config_list_p)
- return config_list
+
+ def get_configs(self, prefix=''):
+ """Return a dict of key, value pairs where the start of key matches the
+ given prefix
+
+ :param prefix: a string by which the keys should be selected
+ :type prefix: str
+ :returns: all key-value pairs where `prefix` matches the beginning
+ of the key
+ :rtype: a dict of str: str
+ :raises: :exc:`NotmuchError` in case of failure.
+
+ """
+ return dict(self.get_config_list(prefix))
"""notmuch_database_set_config"""
_set_config = nmlib.notmuch_database_set_config