X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fgvoice%2Fconversations.py;h=7cad027581ee438c65edaa1ba396ecea322c4dfc;hp=2b7df61a46a8fcddd34c937324cd755252a006e8;hb=76d24c8d4a16fe0e620e8d26b676876a19c50162;hpb=4f32f7a5b10392a945a30f48891a5828af8b5395 diff --git a/src/gvoice/conversations.py b/src/gvoice/conversations.py index 2b7df61..7cad027 100644 --- a/src/gvoice/conversations.py +++ b/src/gvoice/conversations.py @@ -20,6 +20,11 @@ import util.go_utils as gobject_utils _moduleLogger = logging.getLogger(__name__) +class ConversationError(RuntimeError): + + pass + + class Conversations(object): OLDEST_COMPATIBLE_FORMAT_VERSION = misc_utils.parse_version("0.8.0") @@ -47,20 +52,13 @@ class Conversations(object): _moduleLogger.exception("While loading for %s" % self._name) return - if misc_utils.compare_versions( + if convs and misc_utils.compare_versions( self.OLDEST_COMPATIBLE_FORMAT_VERSION, misc_utils.parse_version(fileVersion), ) <= 0: _moduleLogger.info("%s Loaded cache" % (self._name, )) self._conversations = convs self._loadedFromCache = True - for key, mergedConv in self._conversations.iteritems(): - _moduleLogger.debug("%s \tLoaded %s" % (self._name, key)) - for conv in mergedConv.conversations: - message = "%s \t\tLoaded %s (%r) %r %r %r" % ( - self._name, conv.id, conv.time, conv.isRead, conv.isArchived, len(conv.messages) - ) - _moduleLogger.debug(message) else: _moduleLogger.debug( "%s Skipping cache due to version mismatch (%s-%s)" % ( @@ -70,21 +68,16 @@ class Conversations(object): def save(self, path): _moduleLogger.info("%s Saving cache" % (self._name, )) + if not self._conversations: + _moduleLogger.info("%s Odd, no conversations to cache. Did we never load the cache?" % (self._name, )) + return + try: dataToDump = (constants.__version__, constants.__build__, self._conversations) with open(path, "wb") as f: pickle.dump(dataToDump, f, pickle.HIGHEST_PROTOCOL) except (pickle.PickleError, IOError): _moduleLogger.exception("While saving for %s" % self._name) - - for key, mergedConv in self._conversations.iteritems(): - _moduleLogger.debug("%s \tSaving %s" % (self._name, key)) - for conv in mergedConv.conversations: - message = "%s \t\tSaving %s (%r) %r %r %r" % ( - self._name, conv.id, conv.time, conv.isRead, conv.isArchived, len(conv.messages) - ) - _moduleLogger.debug(message) - _moduleLogger.info("%s Cache saved" % (self._name, )) def update(self, force=False): @@ -96,11 +89,15 @@ class Conversations(object): @misc_utils.log_exception(_moduleLogger) def _update(self): - conversationResult = yield ( - self._get_raw_conversations, - (), - {}, - ) + try: + conversationResult = yield ( + self._get_raw_conversations, + (), + {}, + ) + except Exception: + _moduleLogger.exception("%s While updating conversations" % (self._name, )) + return oldConversationIds = set(self._conversations.iterkeys()) @@ -119,12 +116,17 @@ class Conversations(object): markAllAsRead = False else: markAllAsRead = True + try: mergedConversations.append_conversation(conversation, markAllAsRead) isConversationUpdated = True + except ConversationError, e: + isConversationUpdated = False + except AssertionError, e: + _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e)) + isConversationUpdated = False except RuntimeError, e: - if False: - _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e)) + _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e)) isConversationUpdated = False if isConversationUpdated: @@ -182,10 +184,11 @@ class MergedConversations(object): # * We cache to disk the history of messages sent/received # * On first run we mark all server messages as read due to no cache # * If not first load or from cache (disk or in-memory) then it must be unread - if markAllAsRead: - newConversation.isRead = True - else: - newConversation.isRead = False + if newConversation.type != newConversation.TYPE_VOICEMAIL: + if markAllAsRead: + newConversation.isRead = True + else: + newConversation.isRead = False if newConversation.messages: # must not have had all items removed due to duplicates @@ -211,7 +214,7 @@ class MergedConversations(object): ) if newConversation.time <= self._conversations[-1].time: - raise RuntimeError("Conversations got out of order") + raise ConversationError("Conversations got out of order") def _find_related_conversation(self, convId): similarConversations = ( @@ -284,3 +287,18 @@ class FilterOutReported(object): if filteredConversations and self._lastMessageTimestamp < filteredConversations[0].time: self._lastMessageTimestamp = filteredConversations[0].time return filteredConversations + + +def print_conversations(path): + import pprint + + try: + with open(path, "rb") as f: + fileVersion, fileBuild, convs = pickle.load(f) + except (pickle.PickleError, IOError, EOFError, ValueError): + _moduleLogger.exception("") + else: + for key, value in convs.iteritems(): + convs[key] = value.to_dict() + pprint.pprint((fileVersion, fileBuild)) + pprint.pprint(convs)