6 import util.coroutines as coroutines
7 import util.misc as misc_utils
8 import util.go_utils as gobject_utils
11 _moduleLogger = logging.getLogger(__name__)
14 class Addressbook(object):
19 def __init__(self, backend, asyncPool):
20 self._backend = backend
22 self._asyncPool = asyncPool
24 self.updateSignalHandler = coroutines.CoTee()
26 def update(self, force=False):
27 if not force and self._numbers:
30 le = gobject_utils.AsyncLinearExecution(self._asyncPool, self._update)
33 @misc_utils.log_exception(_moduleLogger)
36 self._backend.get_contacts,
41 oldContacts = self._numbers
42 oldContactNumbers = set(self.get_numbers())
44 self._numbers = self._populate_contacts(contacts)
45 newContactNumbers = set(self.get_numbers())
47 addedContacts = newContactNumbers - oldContactNumbers
48 removedContacts = oldContactNumbers - newContactNumbers
49 changedContacts = set(
51 for contactNumber in newContactNumbers.intersection(oldContactNumbers)
52 if self._numbers[contactNumber] != oldContacts[contactNumber]
55 if addedContacts or removedContacts or changedContacts:
56 message = self, addedContacts, removedContacts, changedContacts
57 self.updateSignalHandler.stage.send(message)
59 def get_numbers(self):
60 return self._numbers.iterkeys()
62 def get_contact_name(self, strippedNumber):
64 @throws KeyError if contact not in list (so client can choose what to display)
66 return self._numbers[strippedNumber][0]
68 def get_phone_type(self, strippedNumber):
70 return self._numbers[strippedNumber][1]
74 def is_blocked(self, strippedNumber):
76 return self._numbers[strippedNumber][2]["response"] == self._RESPONSE_BLOCKED
80 def _populate_contacts(self, contacts):
82 for contactId, contactDetails in contacts:
83 contactName = contactDetails["name"]
86 misc_utils.normalize_number(numberDetails["phoneNumber"]),
87 numberDetails.get("phoneType", "Mobile"),
89 for numberDetails in contactDetails["numbers"]
92 (number, (contactName, phoneType, contactDetails))
93 for (number, phoneType) in contactNumbers