Protection against some kind of weird error I hit
[theonering] / src / gvoice / conversations.py
1 #!/usr/bin/python
2
3 from __future__ import with_statement
4
5 import datetime
6 import logging
7
8 try:
9         import cPickle
10         pickle = cPickle
11 except ImportError:
12         import pickle
13
14 import constants
15 import util.coroutines as coroutines
16 import util.misc as misc_utils
17
18
19 _moduleLogger = logging.getLogger(__name__)
20
21
22 class Conversations(object):
23
24         def __init__(self, getter):
25                 self._get_raw_conversations = getter
26                 self._conversations = {}
27
28                 self.updateSignalHandler = coroutines.CoTee()
29
30         @property
31         def _name(self):
32                 return repr(self._get_raw_conversations.__name__)
33
34         def load(self, path):
35                 assert not self._conversations
36                 try:
37                         with open(path, "rb") as f:
38                                 fileVersion, fileBuild, convs = pickle.load(f)
39                 except (pickle.PickleError, IOError, EOFError, ValueError):
40                         _moduleLogger.exception("While loading for %s" % self._name)
41                         return
42
43                 if fileVersion == constants.__version__ and fileBuild == constants.__build__:
44                         self._conversations = convs
45                 else:
46                         _moduleLogger.debug(
47                                 "%s Skipping cache due to version mismatch (%s-%s)" % (self._name, fileVersion, fileBuild)
48                         )
49
50         def save(self, path):
51                 try:
52                         dataToDump = (constants.__version__, constants.__build__, self._conversations)
53                         with open(path, "wb") as f:
54                                 pickle.dump(dataToDump, f, pickle.HIGHEST_PROTOCOL)
55                 except (pickle.PickleError, IOError):
56                         _moduleLogger.exception("While saving for %s" % self._name)
57
58         def update(self, force=False):
59                 if not force and self._conversations:
60                         return
61
62                 oldConversationIds = set(self._conversations.iterkeys())
63
64                 updateConversationIds = set()
65                 conversations = list(self._get_raw_conversations())
66                 conversations.sort()
67                 for conversation in conversations:
68                         key = misc_utils.normalize_number(conversation.number)
69                         try:
70                                 mergedConversations = self._conversations[key]
71                         except KeyError:
72                                 mergedConversations = MergedConversations()
73                                 self._conversations[key] = mergedConversations
74
75                         try:
76                                 mergedConversations.append_conversation(conversation)
77                                 isConversationUpdated = True
78                         except RuntimeError, e:
79                                 if False:
80                                         _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e))
81                                 isConversationUpdated = False
82
83                         if isConversationUpdated:
84                                 updateConversationIds.add(key)
85
86                 if updateConversationIds:
87                         message = (self, updateConversationIds, )
88                         self.updateSignalHandler.stage.send(message)
89
90         def get_conversations(self):
91                 return self._conversations.iterkeys()
92
93         def get_conversation(self, key):
94                 return self._conversations[key]
95
96         def clear_conversation(self, key):
97                 try:
98                         del self._conversations[key]
99                 except KeyError:
100                         _moduleLogger.info("%s Conversation never existed for %r" % (self._name, key, ))
101
102         def clear_all(self):
103                 self._conversations.clear()
104
105
106 class MergedConversations(object):
107
108         def __init__(self):
109                 self._conversations = []
110
111         def append_conversation(self, newConversation):
112                 self._validate(newConversation)
113                 similarExist = False
114                 for similarConversation in self._find_related_conversation(newConversation.id):
115                         self._update_previous_related_conversation(similarConversation, newConversation)
116                         self._remove_repeats(similarConversation, newConversation)
117                         similarExist = True
118                 if similarExist:
119                         # Hack to reduce a race window with GV marking messages as read
120                         # because it thinks we replied when really we replied to the
121                         # previous message.  Clients of this code are expected to handle
122                         # this gracefully.  Other race conditions may exist but clients are
123                         # responsible for them
124                         if newConversation.messages:
125                                 newConversation.isRead = False
126                         else:
127                                 newConversation.isRead = True
128                 self._conversations.append(newConversation)
129
130         def to_dict(self):
131                 selfDict = {}
132                 selfDict["conversations"] = [conv.to_dict() for conv in self._conversations]
133                 return selfDict
134
135         @property
136         def conversations(self):
137                 return self._conversations
138
139         def _validate(self, newConversation):
140                 if not self._conversations:
141                         return
142
143                 for constantField in ("number", ):
144                         assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % (
145                                 getattr(self._conversations[0], constantField),
146                                 getattr(newConversation, constantField),
147                         )
148
149                 if newConversation.time <= self._conversations[-1].time:
150                         raise RuntimeError("Conversations got out of order")
151
152         def _find_related_conversation(self, convId):
153                 similarConversations = (
154                         conversation
155                         for conversation in self._conversations
156                         if conversation.id == convId
157                 )
158                 return similarConversations
159
160         def _update_previous_related_conversation(self, relatedConversation, newConversation):
161                 for commonField in ("isSpam", "isTrash", "isArchived"):
162                         newValue = getattr(newConversation, commonField)
163                         setattr(relatedConversation, commonField, newValue)
164
165         def _remove_repeats(self, relatedConversation, newConversation):
166                 newConversationMessages = newConversation.messages
167                 newConversation.messages = [
168                         newMessage
169                         for newMessage in newConversationMessages
170                         if newMessage not in relatedConversation.messages
171                 ]
172                 _moduleLogger.debug("Found %d new messages in conversation %s (%d/%d)" % (
173                         len(newConversationMessages) - len(newConversation.messages),
174                         newConversation.id,
175                         len(newConversation.messages),
176                         len(newConversationMessages),
177                 ))
178                 assert 0 < len(newConversation.messages), "Everything shouldn't have been removed"
179
180
181 def filter_out_read(conversations):
182         return (
183                 conversation
184                 for conversation in conversations
185                 if not conversation.isRead and not conversation.isArchived
186         )
187
188
189 def is_message_from_self(message):
190         return message.whoFrom == "Me:"
191
192
193 def filter_out_self(conversations):
194         return (
195                 newConversation
196                 for newConversation in conversations
197                 if len(newConversation.messages) and any(
198                         not is_message_from_self(message)
199                         for message in newConversation.messages
200                 )
201         )
202
203
204 class FilterOutReported(object):
205
206         NULL_TIMESTAMP = datetime.datetime(1, 1, 1)
207
208         def __init__(self):
209                 self._lastMessageTimestamp = self.NULL_TIMESTAMP
210
211         def get_last_timestamp(self):
212                 return self._lastMessageTimestamp
213
214         def __call__(self, conversations):
215                 filteredConversations = [
216                         conversation
217                         for conversation in conversations
218                         if self._lastMessageTimestamp < conversation.time
219                 ]
220                 if filteredConversations and self._lastMessageTimestamp < filteredConversations[0].time:
221                         self._lastMessageTimestamp = filteredConversations[0].time
222                 return filteredConversations