Implementing caches for a session
[gc-dialer] / src / backends / gv_backend.py
index 16aef65..e820ffb 100644 (file)
@@ -27,12 +27,13 @@ Resources
 
 from __future__ import with_statement
 
+import itertools
 import logging
 
-import gvoice
+from gvoice import gvoice
 
 
-_moduleLogger = logging.getLogger("gv_backend")
+_moduleLogger = logging.getLogger(__name__)
 
 
 class GVDialer(object):
@@ -40,8 +41,6 @@ class GVDialer(object):
        def __init__(self, cookieFile = None):
                self._gvoice = gvoice.GVoiceBackend(cookieFile)
 
-               self._contacts = None
-
        def is_quick_login_possible(self):
                """
                @returns True then is_authed might be enough to login, else full login is required
@@ -66,6 +65,9 @@ class GVDialer(object):
        def logout(self):
                return self._gvoice.logout()
 
+       def persist(self):
+               return self._gvoice.persist()
+
        def is_dnd(self):
                return self._gvoice.is_dnd()
 
@@ -85,8 +87,8 @@ class GVDialer(object):
                """
                return self._gvoice.cancel(outgoingNumber)
 
-       def send_sms(self, phoneNumber, message):
-               self._gvoice.send_sms(phoneNumber, message)
+       def send_sms(self, phoneNumbers, message):
+               self._gvoice.send_sms(phoneNumbers, message)
 
        def search(self, query):
                """
@@ -144,44 +146,27 @@ class GVDialer(object):
                """
                @returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
                """
-               return self._gvoice.get_recent()
+               return list(self._gvoice.get_recent())
 
        def get_contacts(self):
                """
-               @returns Iterable of (contact id, contact name)
-               """
-               self._update_contacts_cache()
-               contactsToSort = [
-                       (contactDetails["name"], contactId)
-                       for contactId, contactDetails in self._contacts.iteritems()
-               ]
-               contactsToSort.sort()
-               return (
-                       (contactId, contactName)
-                       for (contactName, contactId) in contactsToSort
-               )
-
-       def get_contact_details(self, contactId):
-               """
-               @returns Iterable of (Phone Type, Phone Number)
+               @returns Fresh dictionary of items
                """
-               if self._contacts is None:
-                       self._update_contacts_cache()
-               contactDetails = self._contacts[contactId]
-               # Defaulting phoneTypes because those are just things like faxes
-               return (
-                       (number.get("phoneType", ""), number["phoneNumber"])
-                       for number in contactDetails["numbers"]
-               )
+               return dict(self._gvoice.get_contacts())
 
        def get_messages(self):
-               conversations = self._gvoice.get_conversations()
+               return list(self._get_messages())
+
+       def _get_messages(self):
+               voicemails = self._gvoice.get_voicemails()
+               smss = self._gvoice.get_texts()
+               conversations = itertools.chain(voicemails, smss)
                for conversation in conversations:
                        messages = conversation.messages
-                       messageParts = (
+                       messageParts = [
                                (message.whoFrom, self._format_message(message), message.when)
                                for message in messages
-                       )
+                       ]
 
                        messageDetails = {
                                "id": conversation.id,
@@ -221,9 +206,6 @@ class GVDialer(object):
        def factory_name():
                return "Google Voice"
 
-       def _update_contacts_cache(self):
-               self._contacts = dict(self._gvoice.get_contacts())
-
        def _format_message(self, message):
                messagePartFormat = {
                        "med1": "<i>%s</i>",