1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2008-2009 Warp Networks, S.L.
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.
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.
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"""
20
from zope.interface import implements
22
from wader.common.encoding import to_u
23
from wader.common.interfaces import IContact
26
class Contact(object):
27
"""I am a Contact on Wader"""
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)
39
return '<Contact name=%s number=%s>' % (self.name, self.number)
41
args = (self.name, self.number, self.index)
42
return '<Contact name=%s number=%s index=%d>' % args
47
if self.index and c.index:
48
return self.index == c.index
50
return self.name == c.name and self.number == c.number
53
return not self.__eq__(c)
56
"""See :meth:`wader.common.interfaces.IContact.to_csv`"""
57
name = '"%s"' % self.name
58
number = '"%s"' % self.number
62
class ContactStore(object):
66
A central point to perform operations on the different contact
67
backends (see :class:`~wader.common.interfaces.IContactProvider`)
71
super(ContactStore, self).__init__()
74
def add_provider(self, provider):
75
"""Adds ``provider`` to the list of registered providers"""
76
self._providers.append(provider)
78
def remove_provider(self, provider):
79
"""Removes ``provider`` to the list of registered providers"""
80
self._providers.remove(provider)
84
while self._providers:
85
provider = self._providers.pop()
88
def _call_method(self, name, *args):
90
Executes method ``name`` using ``args`` in all the registered providers
93
for prov in self._providers:
94
result = getattr(prov, name)(*args)
95
if isinstance(result, list):
96
ret.extend(getattr(prov, name)(*args))
101
def add_contact(self, data):
102
""":meth:`~wader.common.interfaces.IContactProvider.add_contact`"""
103
return self._call_method('add_contact', data)[0]
105
def edit_contact(self, data):
106
""":meth:`~wader.common.interfaces.IContactProvider.edit_contact`"""
107
return self._call_method('edit_contact', data)[0]
109
def find_contacts_by_name(self, name):
111
:meth:`~wader.common.interfaces.IContactProvider.find_contacts_by_name`
113
return self._call_method('find_contacts_by_name', name)
115
def find_contacts_by_number(self, number):
117
see `IContactProvider.find_contacts_by_number`
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
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:]))
134
def _find_contacts_by_number(self, number):
135
return self._call_method('find_contacts_by_number', number)
137
def list_contacts(self):
138
""":meth:`~wader.common.interfaces.IContactProvider.list_contacts`"""
139
return self._call_method('list_contacts')
141
def remove_contact(self, contact):
142
""":meth:`~wader.common.interfaces.IContactProvider.remove_contact`"""
143
self._call_method('remove_contact', contact)