Trying to improve debug output due to an exception from some of this code
[theonering] / src / gvoice / addressbook.py
index 99cd721..2a5e389 100644 (file)
@@ -4,28 +4,44 @@
 import logging
 
 import util.coroutines as coroutines
-import util.misc as util_misc
+import util.misc as misc_utils
+import util.go_utils as gobject_utils
 
 
-_moduleLogger = logging.getLogger("gvoice.addressbook")
+_moduleLogger = logging.getLogger(__name__)
 
 
 class Addressbook(object):
 
-       def __init__(self, backend):
+       _RESPONSE_GOOD = 0
+       _RESPONSE_BLOCKED = 3
+
+       def __init__(self, backend, asyncPool):
                self._backend = backend
                self._numbers = {}
+               self._asyncPool = asyncPool
 
                self.updateSignalHandler = coroutines.CoTee()
 
        def update(self, force=False):
                if not force and self._numbers:
                        return
+
+               le = gobject_utils.AsyncLinearExecution(self._asyncPool, self._update)
+               le.start()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _update(self):
+               contacts = yield (
+                       self._backend.get_contacts,
+                       (),
+                       {},
+               )
+
                oldContacts = self._numbers
                oldContactNumbers = set(self.get_numbers())
 
-               self._numbers = {}
-               self._populate_contacts()
+               self._numbers = self._populate_contacts(contacts)
                newContactNumbers = set(self.get_numbers())
 
                addedContacts = newContactNumbers - oldContactNumbers
@@ -44,25 +60,36 @@ class Addressbook(object):
                return self._numbers.iterkeys()
 
        def get_contact_name(self, strippedNumber):
+               """
+               @throws KeyError if contact not in list (so client can choose what to display)
+               """
                return self._numbers[strippedNumber][0]
 
        def get_phone_type(self, strippedNumber):
-               return self._numbers[strippedNumber][1]
-
-       def _populate_contacts(self):
-               if self._numbers:
-                       return
-               contacts = self._backend.get_contacts()
+               try:
+                       return self._numbers[strippedNumber][1]
+               except KeyError:
+                       return "unknown"
+
+       def is_blocked(self, strippedNumber):
+               try:
+                       return self._numbers[strippedNumber][2]["response"] == self._RESPONSE_BLOCKED
+               except KeyError:
+                       return False
+
+       def _populate_contacts(self, contacts):
+               numbers = {}
                for contactId, contactDetails in contacts:
                        contactName = contactDetails["name"]
                        contactNumbers = (
                                (
+                                       misc_utils.normalize_number(numberDetails["phoneNumber"]),
                                        numberDetails.get("phoneType", "Mobile"),
-                                       util_misc.normalize_number(numberDetails["phoneNumber"]),
                                )
                                for numberDetails in contactDetails["numbers"]
                        )
-                       self._numbers.update(
-                               (number, (contactName, phoneType))
-                               for (phoneType, number) in contactNumbers
+                       numbers.update(
+                               (number, (contactName, phoneType, contactDetails))
+                               for (number, phoneType) in contactNumbers
                        )
+               return numbers