summaryrefslogtreecommitdiff
path: root/alot/completion/abooks.py
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2019-08-17 10:00:30 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2019-08-17 11:10:37 +0100
commitd8d1429ec3daf8f2a67ffccbc2aa1d54eb3639c6 (patch)
tree3f13fd449bacd97432e18ba4fb0fb4b19ac0b5ed /alot/completion/abooks.py
parentb5e612c69b625271424b626da24d941ddbe39391 (diff)
refactor prompt completion
This just splits the file completion.py into several files, one for each Completer subclass.
Diffstat (limited to 'alot/completion/abooks.py')
-rw-r--r--alot/completion/abooks.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/alot/completion/abooks.py b/alot/completion/abooks.py
new file mode 100644
index 00000000..bc2fa20b
--- /dev/null
+++ b/alot/completion/abooks.py
@@ -0,0 +1,42 @@
+# Copyright (C) 2011-2019 Patrick Totzke <patricktotzke@gmail.com>
+# This file is released under the GNU GPL, version 3 or a later revision.
+# For further details see the COPYING file
+
+from .completer import Completer
+from ..addressbook import AddressbookError
+from ..db.utils import formataddr
+from ..errors import CompletionError
+
+
+class AbooksCompleter(Completer):
+ """Complete a contact from given address books."""
+
+ def __init__(self, abooks, addressesonly=False):
+ """
+ :param abooks: used to look up email addresses
+ :type abooks: list of :class:`~alot.account.AddresBook`
+ :param addressesonly: only insert address, not the realname of the
+ contact
+ :type addressesonly: bool
+ """
+ self.abooks = abooks
+ self.addressesonly = addressesonly
+
+ def complete(self, original, pos):
+ if not self.abooks:
+ return []
+ prefix = original[:pos]
+ res = []
+ for abook in self.abooks:
+ try:
+ res = res + abook.lookup(prefix)
+ except AddressbookError as e:
+ raise CompletionError(e)
+ if self.addressesonly:
+ returnlist = [(addr, len(addr)) for (name, addr) in res]
+ else:
+ returnlist = []
+ for name, addr in res:
+ newtext = formataddr((name, addr))
+ returnlist.append((newtext, len(newtext)))
+ return returnlist