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)
37 self._backend.get_contacts,
42 _moduleLogger.exception("While updating the addressbook")
45 oldContacts = self._numbers
46 oldContactNumbers = set(self.get_numbers())
48 self._numbers = self._populate_contacts(contacts)
49 newContactNumbers = set(self.get_numbers())
51 addedContacts = newContactNumbers - oldContactNumbers
52 removedContacts = oldContactNumbers - newContactNumbers
53 changedContacts = set(
55 for contactNumber in newContactNumbers.intersection(oldContactNumbers)
56 if self._numbers[contactNumber] != oldContacts[contactNumber]
59 if addedContacts or removedContacts or changedContacts:
60 message = self, addedContacts, removedContacts, changedContacts
61 self.updateSignalHandler.stage.send(message)
63 def get_numbers(self):
64 return self._numbers.iterkeys()
66 def get_contact_name(self, strippedNumber):
68 @throws KeyError if contact not in list (so client can choose what to display)
70 return self._numbers[strippedNumber][0]
72 def get_phone_type(self, strippedNumber):
74 return self._numbers[strippedNumber][1]
78 def is_blocked(self, strippedNumber):
80 return self._numbers[strippedNumber][2]["response"] == self._RESPONSE_BLOCKED
84 def _populate_contacts(self, contacts):
86 for contactId, contactDetails in contacts:
87 contactName = contactDetails["name"]
90 misc_utils.normalize_number(numberDetails["phoneNumber"]),
91 numberDetails.get("phoneType", "Mobile"),
93 for numberDetails in contactDetails["numbers"]
96 (number, (contactName, phoneType, contactDetails))
97 for (number, phoneType) in contactNumbers