~nherriot/bcm/tags

« back to all changes in this revision

Viewing changes to bcm-2.99.03-alpha1/src/core/wader/common/contact.py

  • Committer: andrewbird
  • Date: 2010-04-29 07:52:44 UTC
  • Revision ID: svn-v4:302e0824-f0b9-4af8-b993-bc22a3d40462:tags:766
Create tag bcm-2.99.03-alpha1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (C) 2008-2009  Warp Networks, S.L.
 
3
# Author:  Pablo Martí
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along
 
16
# with this program; if not, write to the Free Software Foundation, Inc.,
 
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
"""Contact related classes and utilities"""
 
19
 
 
20
from zope.interface import implements
 
21
 
 
22
from wader.common.encoding import to_u
 
23
from wader.common.interfaces import IContact
 
24
 
 
25
 
 
26
class Contact(object):
 
27
    """I am a Contact on Wader"""
 
28
 
 
29
    implements(IContact)
 
30
 
 
31
    def __init__(self, name, number, index=None):
 
32
        super(Contact, self).__init__()
 
33
        self.name = to_u(name)
 
34
        self.number = to_u(number)
 
35
        self.index = index
 
36
 
 
37
    def __repr__(self):
 
38
        if not self.index:
 
39
            return '<Contact name=%s number=%s>' % (self.name, self.number)
 
40
 
 
41
        args = (self.name, self.number, self.index)
 
42
        return '<Contact name=%s number=%s index=%d>' % args
 
43
 
 
44
    __str__ = __repr__
 
45
 
 
46
    def __eq__(self, c):
 
47
        if self.index and c.index:
 
48
            return self.index == c.index
 
49
 
 
50
        return self.name == c.name and self.number == c.number
 
51
 
 
52
    def __ne__(self, c):
 
53
        return not self.__eq__(c)
 
54
 
 
55
    def to_csv(self):
 
56
        """See :meth:`wader.common.interfaces.IContact.to_csv`"""
 
57
        name = '"%s"' % self.name
 
58
        number = '"%s"' % self.number
 
59
        return [name, number]
 
60
 
 
61
 
 
62
class ContactStore(object):
 
63
    """
 
64
    I am a contact store
 
65
 
 
66
    A central point to perform operations on the different contact
 
67
    backends (see :class:`~wader.common.interfaces.IContactProvider`)
 
68
    """
 
69
 
 
70
    def __init__(self):
 
71
        super(ContactStore, self).__init__()
 
72
        self._providers = []
 
73
 
 
74
    def add_provider(self, provider):
 
75
        """Adds ``provider`` to the list of registered providers"""
 
76
        self._providers.append(provider)
 
77
 
 
78
    def remove_provider(self, provider):
 
79
        """Removes ``provider`` to the list of registered providers"""
 
80
        self._providers.remove(provider)
 
81
 
 
82
    def close(self):
 
83
        """Frees resources"""
 
84
        while self._providers:
 
85
            provider = self._providers.pop()
 
86
            provider.close()
 
87
 
 
88
    def _call_method(self, name, *args):
 
89
        """
 
90
        Executes method ``name`` using ``args`` in all the registered providers
 
91
        """
 
92
        ret = []
 
93
        for prov in self._providers:
 
94
            result = getattr(prov, name)(*args)
 
95
            if isinstance(result, list):
 
96
                ret.extend(getattr(prov, name)(*args))
 
97
            else:
 
98
                ret.append(result)
 
99
        return ret
 
100
 
 
101
    def add_contact(self, data):
 
102
        """:meth:`~wader.common.interfaces.IContactProvider.add_contact`"""
 
103
        return self._call_method('add_contact', data)[0]
 
104
 
 
105
    def edit_contact(self, data):
 
106
        """:meth:`~wader.common.interfaces.IContactProvider.edit_contact`"""
 
107
        return self._call_method('edit_contact', data)[0]
 
108
 
 
109
    def find_contacts_by_name(self, name):
 
110
        """
 
111
        :meth:`~wader.common.interfaces.IContactProvider.find_contacts_by_name`
 
112
        """
 
113
        return self._call_method('find_contacts_by_name', name)
 
114
 
 
115
    def find_contacts_by_number(self, number):
 
116
        """
 
117
        see `IContactProvider.find_contacts_by_number`
 
118
        """
 
119
        # first try a full match, if succeeds return result
 
120
        # otherwise try to remove 3 chars and if succeeds return result
 
121
        # i.e. match '723123112' instead of '+44723123112' (UK, ES)
 
122
        # otherwise try to remove 4 chars and if succeeds return result
 
123
        # i.e. match '821372121' instead of '+353821372121' (IE)
 
124
        # otherwise we failed
 
125
        for n in [0, 3, 4]:
 
126
            # finding out if a generator returns None is a bit cumbersome
 
127
            # so we just consume the generator and create a list
 
128
            match = list(self._find_contacts_by_number(number[n:]))
 
129
            if match:
 
130
                return match
 
131
 
 
132
        return []
 
133
 
 
134
    def _find_contacts_by_number(self, number):
 
135
        return self._call_method('find_contacts_by_number', number)
 
136
 
 
137
    def list_contacts(self):
 
138
        """:meth:`~wader.common.interfaces.IContactProvider.list_contacts`"""
 
139
        return self._call_method('list_contacts')
 
140
 
 
141
    def remove_contact(self, contact):
 
142
        """:meth:`~wader.common.interfaces.IContactProvider.remove_contact`"""
 
143
        self._call_method('remove_contact', contact)