~unity-2d-team/unity-2d/Shell-MultiMonitor

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