From df2c671bc53b0fa85d4f01ad42d749bd051e991f Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Sat, 25 Feb 2012 21:01:50 +0000 Subject: introduce AbookAddressbook a direct parser for abook's contact files. --- alot/account.py | 28 +++++++++++----------------- alot/defaults/abook_contacts.spec | 7 +++++++ alot/defaults/alot.rc.spec | 8 ++++++-- alot/settings.py | 6 ++++-- 4 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 alot/defaults/abook_contacts.spec (limited to 'alot') diff --git a/alot/account.py b/alot/account.py index c41266ad..12cb3c23 100644 --- a/alot/account.py +++ b/alot/account.py @@ -6,7 +6,6 @@ import email import os import glob import shlex -from ConfigParser import SafeConfigParser from urlparse import urlparse import helper @@ -213,31 +212,26 @@ class AddressBook(object): res = [] for name, email in self.get_contacts(): if name.startswith(prefix) or email.startswith(prefix): - res.append("%s <%s>" % (name, email)) + res.append((name, email)) return res class AbookAddressBook(AddressBook): """:class:`AddressBook` that parses abook's config/database files""" - def __init__(self, config=None): + def __init__(self, path='~/.abook/addressbook'): """ - :param config: path to an `abook` contacts file - (defaults to '/.abook/addressbook') - :type config: str + :param path: path to theme file + :type path: str """ - self.abook = SafeConfigParser() - if not config: - config = os.environ["HOME"] + "/.abook/addressbook" - self.abook.read(config) + 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 = helper.read_config(path, self._spec) + del(self._config['format']) def get_contacts(self): - res = [] - for s in self.abook.sections(): - if s.isdigit(): - name = self.abook.get(s, 'name') - email = self.abook.get(s, 'email') - res.append((name, email)) - return res + c = self._config + return [(c[id]['name'], c[id]['email']) for id in c.sections] class MatchSdtoutAddressbook(AddressBook): diff --git a/alot/defaults/abook_contacts.spec b/alot/defaults/abook_contacts.spec new file mode 100644 index 00000000..d8855fce --- /dev/null +++ b/alot/defaults/abook_contacts.spec @@ -0,0 +1,7 @@ +[format] + program = string + version = string + +[__many__] + name = string + email = string diff --git a/alot/defaults/alot.rc.spec b/alot/defaults/alot.rc.spec index fedc5f9e..7f5255ae 100644 --- a/alot/defaults/alot.rc.spec +++ b/alot/defaults/alot.rc.spec @@ -159,10 +159,14 @@ user_agent = string(default='alot/$VERSION') # address book for this account [[[abook]]] # type identifier for addressbook - type = option('shellcommand', default=None) - # command to lookup contacts. + type = option('shellcommand', 'abook', default=None) + # command to lookup contacts in shellcommand abooks # it will be called with the lookup prefix as only argument command = string(default=None) # regular expression used to match name/address pairs in the output of `command` + # for shellcommand abooks regexp = string(default=None) + + # contacts file used for type 'abook' addressbook + abook_contacts_file = string(default='~/.abook/addressbook') diff --git a/alot/settings.py b/alot/settings.py index e971703a..d50cfebd 100644 --- a/alot/settings.py +++ b/alot/settings.py @@ -11,7 +11,7 @@ from urwid import AttrSpec, AttrSpecError from configobj import ConfigObj, ConfigObjError, flatten_errors, Section from validate import Validator -from account import SendmailAccount, MatchSdtoutAddressbook +from account import SendmailAccount, MatchSdtoutAddressbook, AbookAddressBook from collections import OrderedDict from ConfigParser import SafeConfigParser, ParsingError, NoOptionError @@ -168,7 +168,6 @@ class SettingsManager(object): if abook['type'] == 'shellcommand': cmd = abook['command'] regexp = abook['regexp'] - logging.debug('abook: %s: %s' % (cmd, regexp)) if cmd is not None and regexp is not None: args['abook'] = MatchSdtoutAddressbook(cmd, match=regexp) @@ -176,6 +175,9 @@ class SettingsManager(object): msg = 'underspecified abook of type \'shellcommand\':' msg += '\ncommand: %s\nregexp:%s' % (cmd, regexp) raise ConfigError(msg) + elif abook['type'] == 'abook': + contacts_path = abook['abook_contacts_file'] + args['abook'] = AbookAddressBook(contacts_path) else: del(args['abook']) -- cgit v1.2.3