summaryrefslogtreecommitdiff
path: root/alot/addressbook
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2015-04-10 10:57:38 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2015-04-15 13:05:13 +0100
commit637b679003b353d2630fed8ca942f3ab522e68b5 (patch)
tree9f481ec939022c16b09b33b5c235b11893c49da8 /alot/addressbook
parent2f064af42d4735649d361cbda640353ca08289fd (diff)
refactor MatchSdtoutAddressbook
The class is now called ExternalAddressbook and makes it optional to call the external command with the given search string as parameter when looking up addresses
Diffstat (limited to 'alot/addressbook')
-rw-r--r--alot/addressbook/external.py52
1 files changed, 33 insertions, 19 deletions
diff --git a/alot/addressbook/external.py b/alot/addressbook/external.py
index 21096d23..da9aab59 100644
--- a/alot/addressbook/external.py
+++ b/alot/addressbook/external.py
@@ -8,34 +8,48 @@ from ..helper import split_commandstring
from . import AddressBook, AddressbookError
-class MatchSdtoutAddressbook(AddressBook):
- """:class:`AddressBook` that parses a shell command's output for lookups"""
+class ExternalAddressbook(AddressBook):
+ """:class:`AddressBook` that parses a shell command's output"""
- def __init__(self, command, match=None, **kwargs):
+ def __init__(self, commandline, regex, reflags=None,
+ external_filtering=True,
+ **kwargs):
"""
- :param command: lookup command
- :type command: str
- :param match: regular expression used to match contacts in `commands`
+ :param commandline: commandline
+ :type commandline: str
+ :param regex: regular expression used to match contacts in `commands`
output to stdout. Must define subparts named "email" and
- "name". Defaults to
- :regexp:`^(?P<email>[^@]+@[^\t]+)\t+(?P<name>[^\t]+)`.
- :type match: str
+ "name".
+ :type regex: str
+ :param reflags: flags to use with regular expression
+ :param external_filtering: if True the command is fired
+ with the given search string as parameter
+ and the result is not filtered further.
+ If set to False, the command is fired without
+ additional parameters and the result list is filtered
+ according to the search string.
+ :type external_filtering: bool
"""
AddressBook.__init__(self, **kwargs)
- self.command = command
- if not match:
- self.match = '^(?P<email>[^@]+@[^\t]+)\t+(?P<name>[^\t]+)'
- else:
- self.match = match
+ self.commandline = commandline
+ self.regex = regex
+ self.reflags = reflags
+ self.external_filtering = external_filtering
def get_contacts(self):
- return self.lookup('\'\'')
+ return self._call_and_parse(self.commandline)
def lookup(self, prefix):
- cmdlist = split_commandstring(self.command)
- resultstring, errmsg, retval = call_cmd(cmdlist + [prefix])
+ if self.external_filtering:
+ return self._call_and_parse(self.commandline + " " + prefix)
+ else:
+ return AddressBook.lookup(self, prefix)
+
+ def _call_and_parse(self, commandline):
+ cmdlist = split_commandstring(commandline)
+ resultstring, errmsg, retval = call_cmd(cmdlist)
if retval != 0:
- msg = 'abook command "%s" returned with ' % self.command
+ msg = 'abook command "%s" returned with ' % commandline
msg += 'return code %d' % retval
if errmsg:
msg += ':\n%s' % errmsg
@@ -46,7 +60,7 @@ class MatchSdtoutAddressbook(AddressBook):
lines = resultstring.splitlines()
res = []
for l in lines:
- m = re.match(self.match, l, self.reflags)
+ m = re.match(self.regex, l, self.reflags)
if m:
info = m.groupdict()
if 'email' and 'name' in info: