6.1.36
by Curtis Hovey
Separate gracle MemoryStore to its own module. |
1 |
__all__ = [ |
2 |
'MemoryStore', |
|
3 |
]
|
|
4 |
||
5 |
import simplejson |
|
6 |
from urlparse import parse_qs |
|
7 |
||
6.1.38
by Curtis Hovey
Move errors to their own module. |
8 |
from grackle.error import ( |
6.1.36
by Curtis Hovey
Separate gracle MemoryStore to its own module. |
9 |
MessageIdNotFound, |
10 |
UnparsableDateRange, |
|
11 |
UnsupportedDisplayType, |
|
12 |
UnsupportedOrder, |
|
13 |
)
|
|
14 |
||
15 |
||
16 |
SUPPORTED_DISPLAY_TYPES = set(['all', 'text-only', 'headers-only']) |
|
17 |
||
18 |
||
19 |
SUPPORTED_ORDERS = set( |
|
20 |
['date', 'author', 'subject', 'thread_newest', 'thread_oldest', |
|
21 |
'thread_subject']) |
|
22 |
||
23 |
||
24 |
def threaded_messages(messages): |
|
25 |
threads = {} |
|
26 |
count = 0 |
|
27 |
pending = [] |
|
28 |
for message in messages: |
|
29 |
if message.get('replies') is None: |
|
30 |
threads[message['message_id']] = [message] |
|
31 |
count += 1 |
|
32 |
else: |
|
33 |
pending.append(message) |
|
34 |
for message in pending: |
|
35 |
threads[message['replies']].append(message) |
|
36 |
return threads.values() |
|
37 |
||
38 |
||
39 |
class MemoryStore: |
|
40 |
"""A memory-backed message store."""
|
|
41 |
||
42 |
def __init__(self, messages): |
|
43 |
"""Constructor."""
|
|
44 |
self.messages = messages |
|
45 |
||
46 |
@staticmethod
|
|
47 |
def is_multipart(message): |
|
48 |
return isinstance(message['body'], list) |
|
49 |
||
50 |
def get_messages(self, archive_id, query_string): |
|
51 |
"""Return matching messages.
|
|
52 |
||
53 |
:param archive_id: The archive to retrieve from.
|
|
54 |
:param query_string: Contains 'parameters', which is a JSON-format
|
|
55 |
string describing parameters.
|
|
56 |
"""
|
|
57 |
query = parse_qs(query_string) |
|
58 |
parameters = simplejson.loads(query['parameters'][0]) |
|
59 |
order = parameters.get('order') |
|
60 |
messages = self.messages[archive_id] |
|
61 |
if order is not None: |
|
62 |
if order not in SUPPORTED_ORDERS: |
|
63 |
raise UnsupportedOrder |
|
64 |
elif order.startswith('thread_'): |
|
65 |
threaded = threaded_messages(messages) |
|
66 |
messages = [] |
|
67 |
if order == 'thread_subject': |
|
68 |
threaded.sort(key=lambda t: t[0]['subject']) |
|
69 |
if order == 'thread_oldest': |
|
70 |
threaded.sort(key=lambda t: min(m['date'] for m in t)) |
|
71 |
if order == 'thread_newest': |
|
72 |
threaded.sort(key=lambda t: max(m['date'] for m in t)) |
|
73 |
for thread in threaded: |
|
74 |
messages.extend(thread) |
|
75 |
else: |
|
76 |
messages.sort(key=lambda m: m[order]) |
|
77 |
display_type = parameters.get('display_type', 'all') |
|
78 |
if display_type not in SUPPORTED_DISPLAY_TYPES: |
|
79 |
raise UnsupportedDisplayType |
|
80 |
if 'date_range' in parameters: |
|
81 |
try: |
|
82 |
start_date, end_date = parameters['date_range'].split('..') |
|
83 |
if not start_date or not end_date: |
|
84 |
raise UnparsableDateRange |
|
85 |
except ValueError: |
|
86 |
raise UnparsableDateRange |
|
87 |
new_messages = [] |
|
88 |
for message in messages: |
|
89 |
if (not parameters['include_hidden'] and message['hidden']): |
|
90 |
continue
|
|
91 |
if ('message_ids' in parameters |
|
92 |
and message['message_id'] not in parameters['message_ids']): |
|
93 |
continue
|
|
94 |
if ('date_range' in parameters |
|
95 |
and (message['date'] < start_date |
|
96 |
or message['date'] > end_date)): |
|
97 |
continue
|
|
98 |
message = dict(message) |
|
99 |
if 'headers' in parameters: |
|
100 |
headers = dict( |
|
101 |
(k, v) for k, v in message['headers'].iteritems() |
|
102 |
if k in parameters['headers']) |
|
103 |
message['headers'] = headers |
|
104 |
if display_type == 'headers-only': |
|
105 |
del message['body'] |
|
106 |
elif display_type == 'text-only' and self.is_multipart(message): |
|
107 |
text_parts = [ |
|
108 |
part.get_payload() for part in message['body'] |
|
109 |
if part.get_content_type() == 'text/plain'] |
|
110 |
message['body'] = '\n\n'.join(text_parts) |
|
111 |
elif display_type == 'all' and self.is_multipart(message): |
|
112 |
parts = [str(part.get_payload()) for part in message['body']] |
|
113 |
message['body'] = '\n\n'.join(parts) |
|
114 |
max_body = parameters.get('max_body_length') |
|
115 |
if max_body is not None and display_type != 'headers-only': |
|
116 |
message['body'] = message['body'][:max_body] |
|
117 |
new_messages.append(message) |
|
118 |
messages = new_messages |
|
119 |
limit = parameters.get('limit', 100) |
|
120 |
memo = parameters.get('memo') |
|
121 |
message_id_indices = dict( |
|
122 |
(m['message_id'], idx) for idx, m in enumerate(messages)) |
|
123 |
if memo is None: |
|
124 |
start = 0 |
|
125 |
else: |
|
126 |
start = message_id_indices[memo.encode('rot13')] |
|
127 |
if start > 0: |
|
128 |
previous_memo = messages[start - 1]['message_id'].encode('rot13') |
|
129 |
else: |
|
130 |
previous_memo = None |
|
131 |
end = min(start + limit, len(messages)) |
|
132 |
if end < len(messages): |
|
133 |
next_memo = messages[end]['message_id'].encode('rot13') |
|
134 |
else: |
|
135 |
next_memo = None |
|
136 |
messages = messages[start:end] |
|
137 |
||
138 |
response = { |
|
139 |
'messages': messages, |
|
140 |
'next_memo': next_memo, |
|
141 |
'previous_memo': previous_memo |
|
142 |
}
|
|
143 |
return response |
|
144 |
||
145 |
def hide_message(self, archive_id, query_string): |
|
146 |
"""Return matching messages.
|
|
147 |
||
148 |
:param archive_id: The archive to retrieve from.
|
|
149 |
:param query_string: Contains 'parameters', which is a JSON-format
|
|
150 |
string describing parameters.
|
|
151 |
"""
|
|
152 |
query = parse_qs(query_string) |
|
153 |
parameters = simplejson.loads(query['parameters'][0]) |
|
154 |
message_id = parameters['message_id'] |
|
155 |
hidden = parameters['hidden'] |
|
156 |
messages = self.messages[archive_id] |
|
157 |
for message in messages: |
|
158 |
if message['message_id'] == message_id: |
|
159 |
message['hidden'] = hidden |
|
160 |
response = { |
|
161 |
'message_id': message_id, |
|
162 |
'hidden': hidden, |
|
163 |
}
|
|
164 |
return response |
|
165 |
raise MessageIdNotFound |