summaryrefslogtreecommitdiff
path: root/alot/addressbook/__init__.py
blob: 834142d626f930cb1764634e737f3579e5b57a01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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 abc
import re

class AddressbookError(Exception):
    pass


class AddressBook:
    """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:`ExternalAddressbook` for implementations.
    """

    __metaclass__ = abc.ABCMeta

    def __init__(self, ignorecase=True):
        self.reflags = re.IGNORECASE if ignorecase else 0

    @abc.abstractmethod
    def get_contacts(self):  # pragma no cover
        """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 = re.compile('.*%s.*' % re.escape(query), self.reflags)
        for name, email in self.get_contacts():
            if query.match(name) or query.match(email):
                res.append((name, email))
        return res