~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/store.py

  • Committer: Curtis Hovey
  • Date: 2012-02-29 23:55:28 UTC
  • Revision ID: curtis.hovey@canonical.com-20120229235528-7wphq02b2y9vt7ni
Implemented a partial put into the MemoryStore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
__all__ = [
 
2
    'MemoryStore',
 
3
    ]
 
4
 
 
5
import simplejson
 
6
from urlparse import parse_qs
 
7
 
 
8
from grackle.error import (
 
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
def make_json_message(message_id, body='body', headers=None, hidden=False):
 
40
    if headers is None:
 
41
        headers = {}
 
42
    headers['Message-Id'] = message_id
 
43
    message = {
 
44
        'message_id': message_id,
 
45
        'headers': headers,
 
46
        'thread_id': message_id,
 
47
        'date': headers.get('date', '2005-01-01'),
 
48
        'subject': headers.get('subject', 'subject'),
 
49
        'author': headers.get('author', 'author'),
 
50
        'hidden': hidden,
 
51
        'attachments': [],
 
52
        'replies': headers.get('in-reply-to', None),
 
53
        'body': body,
 
54
        }
 
55
    return message
 
56
 
 
57
 
 
58
class MemoryStore:
 
59
    """A memory-backed message store."""
 
60
 
 
61
    def __init__(self, message_archives):
 
62
        """Constructor."""
 
63
        self.message_archives = message_archives
 
64
 
 
65
    @staticmethod
 
66
    def is_multipart(message):
 
67
        return isinstance(message['body'], list)
 
68
 
 
69
    def put_message(self, archive_id, message_id, message):
 
70
        # Make a json message.
 
71
        messages = self.message_archives[archive_id]
 
72
        json_message = make_json_message(message_id, body=message)
 
73
        messages.append(json_message)
 
74
 
 
75
    def get_messages(self, archive_id, query_string):
 
76
        """Return matching messages.
 
77
 
 
78
        :param archive_id: The archive to retrieve from.
 
79
        :param query_string: Contains 'parameters', which is a JSON-format
 
80
            string describing parameters.
 
81
        """
 
82
        query = parse_qs(query_string)
 
83
        parameters = simplejson.loads(query['parameters'][0])
 
84
        order = parameters.get('order')
 
85
        messages = self.message_archives[archive_id]
 
86
        if order is not None:
 
87
            if order not in SUPPORTED_ORDERS:
 
88
                raise UnsupportedOrder
 
89
            elif order.startswith('thread_'):
 
90
                threaded = threaded_messages(messages)
 
91
                messages = []
 
92
                if order == 'thread_subject':
 
93
                    threaded.sort(key=lambda t: t[0]['subject'])
 
94
                if order == 'thread_oldest':
 
95
                    threaded.sort(key=lambda t: min(m['date'] for m in t))
 
96
                if order == 'thread_newest':
 
97
                    threaded.sort(key=lambda t: max(m['date'] for m in t))
 
98
                for thread in threaded:
 
99
                    messages.extend(thread)
 
100
            else:
 
101
                messages.sort(key=lambda m: m[order])
 
102
        display_type = parameters.get('display_type', 'all')
 
103
        if display_type not in SUPPORTED_DISPLAY_TYPES:
 
104
            raise UnsupportedDisplayType
 
105
        if 'date_range' in parameters:
 
106
            try:
 
107
                start_date, end_date = parameters['date_range'].split('..')
 
108
                if not start_date or not end_date:
 
109
                    raise UnparsableDateRange
 
110
            except ValueError:
 
111
                raise UnparsableDateRange
 
112
        new_messages = []
 
113
        for message in messages:
 
114
            if (not parameters['include_hidden'] and message['hidden']):
 
115
                continue
 
116
            if ('message_ids' in parameters
 
117
                and message['message_id'] not in parameters['message_ids']):
 
118
                continue
 
119
            if ('date_range' in parameters
 
120
                and (message['date'] < start_date
 
121
                     or message['date'] > end_date)):
 
122
                continue
 
123
            message = dict(message)
 
124
            if 'headers' in parameters:
 
125
                headers = dict(
 
126
                    (k, v) for k, v in message['headers'].iteritems()
 
127
                    if k in parameters['headers'])
 
128
                message['headers'] = headers
 
129
            if display_type == 'headers-only':
 
130
                del message['body']
 
131
            elif display_type == 'text-only' and self.is_multipart(message):
 
132
                text_parts = [
 
133
                    part.get_payload() for part in message['body']
 
134
                    if part.get_content_type() == 'text/plain']
 
135
                message['body'] = '\n\n'.join(text_parts)
 
136
            elif display_type == 'all' and self.is_multipart(message):
 
137
                parts = [str(part.get_payload()) for part in message['body']]
 
138
                message['body'] = '\n\n'.join(parts)
 
139
            max_body = parameters.get('max_body_length')
 
140
            if max_body is not None and display_type != 'headers-only':
 
141
                message['body'] = message['body'][:max_body]
 
142
            new_messages.append(message)
 
143
        messages = new_messages
 
144
        limit = parameters.get('limit', 100)
 
145
        memo = parameters.get('memo')
 
146
        message_id_indices = dict(
 
147
            (m['message_id'], idx) for idx, m in enumerate(messages))
 
148
        if memo is None:
 
149
            start = 0
 
150
        else:
 
151
            start = message_id_indices[memo.encode('rot13')]
 
152
        if start > 0:
 
153
            previous_memo = messages[start - 1]['message_id'].encode('rot13')
 
154
        else:
 
155
            previous_memo = None
 
156
        end = min(start + limit, len(messages))
 
157
        if end < len(messages):
 
158
            next_memo = messages[end]['message_id'].encode('rot13')
 
159
        else:
 
160
            next_memo = None
 
161
        messages = messages[start:end]
 
162
 
 
163
        response = {
 
164
            'messages': messages,
 
165
            'next_memo': next_memo,
 
166
            'previous_memo': previous_memo
 
167
            }
 
168
        return response
 
169
 
 
170
    def hide_message(self, archive_id, query_string):
 
171
        """Return matching messages.
 
172
 
 
173
        :param archive_id: The archive to retrieve from.
 
174
        :param query_string: Contains 'parameters', which is a JSON-format
 
175
            string describing parameters.
 
176
        """
 
177
        query = parse_qs(query_string)
 
178
        parameters = simplejson.loads(query['parameters'][0])
 
179
        message_id = parameters['message_id']
 
180
        hidden = parameters['hidden']
 
181
        messages = self.message_archives[archive_id]
 
182
        for message in messages:
 
183
            if message['message_id'] == message_id:
 
184
                message['hidden'] = hidden
 
185
            response = {
 
186
                'message_id': message_id,
 
187
                'hidden': hidden,
 
188
                }
 
189
            return response
 
190
        raise MessageIdNotFound