-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '0.3.6-feature-abooks' into testing
- Loading branch information
Showing
10 changed files
with
178 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright (C) 2011-2015 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 | ||
import re | ||
|
||
|
||
class AddressbookError(Exception): | ||
pass | ||
|
||
|
||
class AddressBook(object): | ||
"""can look up email addresses and realnames for contacts. | ||
.. note:: | ||
This is an abstract class that leaves :meth:`get_contacts` | ||
unspecified. See :class:`AbookAddressBook` and | ||
:class:`MatchSdtoutAddressbook` for implementations. | ||
""" | ||
def __init__(self, ignorecase=True): | ||
self.reflags = re.IGNORECASE if ignorecase else 0 | ||
|
||
def get_contacts(self): | ||
"""list all contacts tuples in this abook as (name, email) tuples""" | ||
return [] | ||
|
||
def lookup(self, query=''): | ||
"""looks up all contacts where name or address match query""" | ||
res = [] | ||
query = '.*%s.*' % query | ||
for name, email in self.get_contacts(): | ||
try: | ||
if re.match(query, name, self.reflags) or \ | ||
re.match(query, email, self.reflags): | ||
res.append((name, email)) | ||
except: | ||
pass | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright (C) 2011-2015 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 | ||
|
||
import os | ||
from . import AddressBook | ||
from ..settings.utils import read_config | ||
|
||
|
||
class AbookAddressBook(AddressBook): | ||
""":class:`AddressBook` that parses abook's config/database files""" | ||
def __init__(self, path='~/.abook/addressbook', **kwargs): | ||
""" | ||
:param path: path to theme file | ||
:type path: str | ||
""" | ||
AddressBook.__init__(self, **kwargs) | ||
DEFAULTSPATH = os.path.join(os.path.dirname(__file__), 'defaults') | ||
self._spec = os.path.join(DEFAULTSPATH, 'abook_contacts.spec') | ||
path = os.path.expanduser(path) | ||
self._config = read_config(path, self._spec) | ||
del(self._config['format']) | ||
|
||
def get_contacts(self): | ||
c = self._config | ||
res = [] | ||
for id in c.sections: | ||
for email in c[id]['email']: | ||
if email: | ||
res.append((c[id]['name'], email)) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright (C) 2011-2015 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 | ||
import re | ||
|
||
from ..helper import call_cmd | ||
from ..helper import split_commandstring | ||
from . import AddressBook, AddressbookError | ||
|
||
|
||
class ExternalAddressbook(AddressBook): | ||
""":class:`AddressBook` that parses a shell command's output""" | ||
|
||
def __init__(self, commandline, regex, reflags=None, | ||
external_filtering=True, | ||
**kwargs): | ||
""" | ||
: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". | ||
: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.commandline = commandline | ||
self.regex = regex | ||
self.reflags = reflags | ||
self.external_filtering = external_filtering | ||
|
||
def get_contacts(self): | ||
return self._call_and_parse(self.commandline) | ||
|
||
def lookup(self, 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 ' % commandline | ||
msg += 'return code %d' % retval | ||
if errmsg: | ||
msg += ':\n%s' % errmsg | ||
raise AddressbookError(msg) | ||
|
||
if not resultstring: | ||
return [] | ||
lines = resultstring.splitlines() | ||
res = [] | ||
for l in lines: | ||
m = re.match(self.regex, l, self.reflags) | ||
if m: | ||
info = m.groupdict() | ||
if 'email' and 'name' in info: | ||
email = info['email'].strip() | ||
name = info['name'] | ||
res.append((name, email)) | ||
return res |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters