~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/store.py

  • Committer: Curtis Hovey
  • Date: 2012-02-24 21:43:12 UTC
  • Revision ID: curtis.hovey@canonical.com-20120224214312-zlji369uv0l9v75m
Move errors to their own module.
Remove duplicate definiton of SUPPORTED_DISPLAY_TYPES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
    'MemoryStore',
3
3
    ]
4
4
 
5
 
import email
6
5
import simplejson
7
6
from urlparse import parse_qs
8
7
 
9
8
from grackle.error import (
10
 
    ArchiveIdNotFound,
11
9
    MessageIdNotFound,
12
10
    UnparsableDateRange,
13
11
    UnsupportedDisplayType,
38
36
    return threads.values()
39
37
 
40
38
 
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
39
class MemoryStore:
74
40
    """A memory-backed message store."""
75
41
 
76
 
    def __init__(self, message_archives):
 
42
    def __init__(self, messages):
77
43
        """Constructor."""
78
 
        self.message_archives = message_archives
 
44
        self.messages = messages
79
45
 
80
46
    @staticmethod
81
47
    def is_multipart(message):
82
48
        return isinstance(message['body'], list)
83
49
 
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
50
    def get_messages(self, archive_id, query_string):
97
51
        """Return matching messages.
98
52
 
103
57
        query = parse_qs(query_string)
104
58
        parameters = simplejson.loads(query['parameters'][0])
105
59
        order = parameters.get('order')
106
 
        messages = self.message_archives[archive_id]
 
60
        messages = self.messages[archive_id]
107
61
        if order is not None:
108
62
            if order not in SUPPORTED_ORDERS:
109
63
                raise UnsupportedOrder
188
142
            }
189
143
        return response
190
144
 
191
 
    def hide_message(self, archive_id, message_id, query_string):
192
 
        """Change the visbility of a message in an archive.
 
145
    def hide_message(self, archive_id, query_string):
 
146
        """Return matching messages.
193
147
 
194
148
        :param archive_id: The archive to retrieve from.
195
149
        :param query_string: Contains 'parameters', which is a JSON-format
197
151
        """
198
152
        query = parse_qs(query_string)
199
153
        parameters = simplejson.loads(query['parameters'][0])
 
154
        message_id = parameters['message_id']
200
155
        hidden = parameters['hidden']
201
 
        messages = self.message_archives[archive_id]
 
156
        messages = self.messages[archive_id]
202
157
        for message in messages:
203
158
            if message['message_id'] == message_id:
204
159
                message['hidden'] = hidden