~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/store.py

  • Committer: Aaron Bentley
  • Date: 2012-01-11 08:06:03 UTC
  • Revision ID: aaron@canonical.com-20120111080603-fxgo006hthzc89kq
check is PHONY

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
__all__ = [
2
 
    'MemoryStore',
3
 
    ]
4
 
 
5
 
import email
6
 
import simplejson
7
 
from urlparse import parse_qs
8
 
 
9
 
from grackle.error import (
10
 
    ArchiveIdNotFound,
11
 
    MessageIdNotFound,
12
 
    UnparsableDateRange,
13
 
    UnsupportedDisplayType,
14
 
    UnsupportedOrder,
15
 
    )
16
 
 
17
 
 
18
 
SUPPORTED_DISPLAY_TYPES = set(['all', 'text-only', 'headers-only'])
19
 
 
20
 
 
21
 
SUPPORTED_ORDERS = set(
22
 
    ['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
23
 
     'thread_subject'])
24
 
 
25
 
 
26
 
def threaded_messages(messages):
27
 
    threads = {}
28
 
    count = 0
29
 
    pending = []
30
 
    for message in messages:
31
 
        if message.get('replies') is None:
32
 
            threads[message['message_id']] = [message]
33
 
            count += 1
34
 
        else:
35
 
            pending.append(message)
36
 
    for message in pending:
37
 
        threads[message['replies']].append(message)
38
 
    return threads.values()
39
 
 
40
 
 
41
 
def get_body_text(message):
42
 
    """Return the first plain/text messaage part."""
43
 
    if not message.is_multipart():
44
 
        return message.get_payload()
45
 
    for part in email.iterators.typed_subpart_iterator(message, 'multipart'):
46
 
        subparts = part.get_payload()
47
 
        for subpart in subparts:
48
 
            if subpart.get_content_type() == 'text/plain':
49
 
                return subpart.get_payload().strip()
50
 
    return ''
51
 
 
52
 
 
53
 
def make_json_message(message_id, raw_message, hidden=False):
54
 
    message = email.message_from_string(raw_message)
55
 
    headers = dict(message.items())
56
 
    message = {
57
 
        'message_id': message_id,
58
 
        'headers': headers,
59
 
        # This is broken because the in-reply-to must be encoded.
60
 
        # X-Message-ID-Hash is calculated from the Base 32.
61
 
        'thread_id': headers.get('in-reply-to', message_id),
62
 
        'date': headers.get('date'),
63
 
        'subject': headers.get('subject'),
64
 
        'author': headers.get('from'),
65
 
        'hidden': hidden,
66
 
        'attachments': [],
67
 
        'replies': headers.get('in-reply-to'),
68
 
        'body': get_body_text(message),
69
 
        }
70
 
    return message
71
 
 
72
 
 
73
 
class MemoryStore:
74
 
    """A memory-backed message store."""
75
 
 
76
 
    def __init__(self, message_archives):
77
 
        """Constructor."""
78
 
        self.message_archives = message_archives
79
 
 
80
 
    @staticmethod
81
 
    def is_multipart(message):
82
 
        return isinstance(message['body'], list)
83
 
 
84
 
    def put_message(self, archive_id, message_id, raw_message):
85
 
        # XXX sinzui 2012-02-29: this needs to raise an error
86
 
        # if the th archive_id is invalid, message_id is not base32
87
 
        # or the raw message is not an email.
88
 
        if archive_id not in self.message_archives:
89
 
            raise ArchiveIdNotFound()
90
 
        if not raw_message:
91
 
            raise ValueError('raw_message is not a message.')
92
 
        json_message = make_json_message(message_id, raw_message)
93
 
        messages = self.message_archives[archive_id]
94
 
        messages.append(json_message)
95
 
 
96
 
    def get_messages(self, archive_id, query_string):
97
 
        """Return matching messages.
98
 
 
99
 
        :param archive_id: The archive to retrieve from.
100
 
        :param query_string: Contains 'parameters', which is a JSON-format
101
 
            string describing parameters.
102
 
        """
103
 
        query = parse_qs(query_string)
104
 
        parameters = simplejson.loads(query['parameters'][0])
105
 
        order = parameters.get('order')
106
 
        messages = self.message_archives[archive_id]
107
 
        if order is not None:
108
 
            if order not in SUPPORTED_ORDERS:
109
 
                raise UnsupportedOrder
110
 
            elif order.startswith('thread_'):
111
 
                threaded = threaded_messages(messages)
112
 
                messages = []
113
 
                if order == 'thread_subject':
114
 
                    threaded.sort(key=lambda t: t[0]['subject'])
115
 
                if order == 'thread_oldest':
116
 
                    threaded.sort(key=lambda t: min(m['date'] for m in t))
117
 
                if order == 'thread_newest':
118
 
                    threaded.sort(key=lambda t: max(m['date'] for m in t))
119
 
                for thread in threaded:
120
 
                    messages.extend(thread)
121
 
            else:
122
 
                messages.sort(key=lambda m: m[order])
123
 
        display_type = parameters.get('display_type', 'all')
124
 
        if display_type not in SUPPORTED_DISPLAY_TYPES:
125
 
            raise UnsupportedDisplayType
126
 
        if 'date_range' in parameters:
127
 
            try:
128
 
                start_date, end_date = parameters['date_range'].split('..')
129
 
                if not start_date or not end_date:
130
 
                    raise UnparsableDateRange
131
 
            except ValueError:
132
 
                raise UnparsableDateRange
133
 
        new_messages = []
134
 
        for message in messages:
135
 
            if (not parameters['include_hidden'] and message['hidden']):
136
 
                continue
137
 
            if ('message_ids' in parameters
138
 
                and message['message_id'] not in parameters['message_ids']):
139
 
                continue
140
 
            if ('date_range' in parameters
141
 
                and (message['date'] < start_date
142
 
                     or message['date'] > end_date)):
143
 
                continue
144
 
            message = dict(message)
145
 
            if 'headers' in parameters:
146
 
                headers = dict(
147
 
                    (k, v) for k, v in message['headers'].iteritems()
148
 
                    if k in parameters['headers'])
149
 
                message['headers'] = headers
150
 
            if display_type == 'headers-only':
151
 
                del message['body']
152
 
            elif display_type == 'text-only' and self.is_multipart(message):
153
 
                text_parts = [
154
 
                    part.get_payload() for part in message['body']
155
 
                    if part.get_content_type() == 'text/plain']
156
 
                message['body'] = '\n\n'.join(text_parts)
157
 
            elif display_type == 'all' and self.is_multipart(message):
158
 
                parts = [str(part.get_payload()) for part in message['body']]
159
 
                message['body'] = '\n\n'.join(parts)
160
 
            max_body = parameters.get('max_body_length')
161
 
            if max_body is not None and display_type != 'headers-only':
162
 
                message['body'] = message['body'][:max_body]
163
 
            new_messages.append(message)
164
 
        messages = new_messages
165
 
        limit = parameters.get('limit', 100)
166
 
        memo = parameters.get('memo')
167
 
        message_id_indices = dict(
168
 
            (m['message_id'], idx) for idx, m in enumerate(messages))
169
 
        if memo is None:
170
 
            start = 0
171
 
        else:
172
 
            start = message_id_indices[memo.encode('rot13')]
173
 
        if start > 0:
174
 
            previous_memo = messages[start - 1]['message_id'].encode('rot13')
175
 
        else:
176
 
            previous_memo = None
177
 
        end = min(start + limit, len(messages))
178
 
        if end < len(messages):
179
 
            next_memo = messages[end]['message_id'].encode('rot13')
180
 
        else:
181
 
            next_memo = None
182
 
        messages = messages[start:end]
183
 
 
184
 
        response = {
185
 
            'messages': messages,
186
 
            'next_memo': next_memo,
187
 
            'previous_memo': previous_memo
188
 
            }
189
 
        return response
190
 
 
191
 
    def hide_message(self, archive_id, message_id, query_string):
192
 
        """Change the visbility of a message in an archive.
193
 
 
194
 
        :param archive_id: The archive to retrieve from.
195
 
        :param query_string: Contains 'parameters', which is a JSON-format
196
 
            string describing parameters.
197
 
        """
198
 
        query = parse_qs(query_string)
199
 
        parameters = simplejson.loads(query['parameters'][0])
200
 
        hidden = parameters['hidden']
201
 
        messages = self.message_archives[archive_id]
202
 
        for message in messages:
203
 
            if message['message_id'] == message_id:
204
 
                message['hidden'] = hidden
205
 
            response = {
206
 
                'message_id': message_id,
207
 
                'hidden': hidden,
208
 
                }
209
 
            return response
210
 
        raise MessageIdNotFound