Fixing a random logging bug which causes an exception
[theonering] / src / gvoice / conversations.py
index 7435da8..d0f80e9 100644 (file)
@@ -1,9 +1,17 @@
 #!/usr/bin/python
 
+from __future__ import with_statement
 
 import logging
 
+try:
+       import cPickle
+       pickle = cPickle
+except ImportError:
+       import pickle
+
 import util.coroutines as coroutines
+import util.misc as util_misc
 
 
 _moduleLogger = logging.getLogger("gvoice.conversations")
@@ -11,12 +19,31 @@ _moduleLogger = logging.getLogger("gvoice.conversations")
 
 class Conversations(object):
 
-       def __init__(self, backend):
-               self._backend = backend
+       def __init__(self, getter):
+               self._get_raw_conversations = getter
                self._conversations = {}
 
                self.updateSignalHandler = coroutines.CoTee()
 
+       @property
+       def _name(self):
+               return repr(self._get_raw_conversations.__name__)
+
+       def load(self, path):
+               assert not self._conversations
+               try:
+                       with open(path, "rb") as f:
+                               self._conversations = pickle.load(f)
+               except (pickle.PickleError, IOError):
+                       _moduleLogger.exception("While loading for %s" % self._name)
+
+       def save(self, path):
+               try:
+                       with open(path, "wb") as f:
+                               pickle.dump(self._conversations, f, pickle.HIGHEST_PROTOCOL)
+               except (pickle.PickleError, IOError):
+                       _moduleLogger.exception("While saving for %s" % self._name)
+
        def update(self, force=False):
                if not force and self._conversations:
                        return
@@ -24,10 +51,10 @@ class Conversations(object):
                oldConversationIds = set(self._conversations.iterkeys())
 
                updateConversationIds = set()
-               conversations = list(self._backend.get_conversations())
+               conversations = list(self._get_raw_conversations())
                conversations.sort()
                for conversation in conversations:
-                       key = conversation.contactId, conversation.number
+                       key = util_misc.normalize_number(conversation.number)
                        try:
                                mergedConversations = self._conversations[key]
                        except KeyError:
@@ -39,7 +66,7 @@ class Conversations(object):
                                isConversationUpdated = True
                        except RuntimeError, e:
                                if False:
-                                       _moduleLogger.info("Skipping conversation for %r because '%s'" % (key, e))
+                                       _moduleLogger.info("%s Skipping conversation for %r because '%s'" % (self._name, key, e))
                                isConversationUpdated = False
 
                        if isConversationUpdated:
@@ -59,7 +86,7 @@ class Conversations(object):
                try:
                        del self._conversations[key]
                except KeyError:
-                       _moduleLogger.info("Conversation never existed for %r" % (key,))
+                       _moduleLogger.info("%s Conversation never existed for %r" % (self._name, key, ))
 
        def clear_all(self):
                self._conversations.clear()
@@ -85,7 +112,7 @@ class MergedConversations(object):
                if not self._conversations:
                        return
 
-               for constantField in ("contactId", "number"):
+               for constantField in ("number", ):
                        assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % (
                                getattr(self._conversations[0], constantField),
                                getattr(newConversation, constantField),
@@ -114,7 +141,7 @@ class MergedConversations(object):
                        for newMessage in newConversationMessages
                        if newMessage not in relatedConversation.messages
                ]
-               _moduleLogger.info("Found %d new messages in conversation %s (%d/%d)" % (
+               _moduleLogger.debug("Found %d new messages in conversation %s (%d/%d)" % (
                        len(newConversationMessages) - len(newConversation.messages),
                        newConversation.id,
                        len(newConversation.messages),