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