~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:
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
 
    ArchiveIdExists,
11
 
    ArchiveIdNotFound,
12
9
    MessageIdNotFound,
13
10
    UnparsableDateRange,
14
11
    UnsupportedDisplayType,
39
36
    return threads.values()
40
37
 
41
38
 
42
 
def get_body_text(message):
43
 
    """Return the first plain/text messaage part."""
44
 
    if not message.is_multipart():
45
 
        return message.get_payload()
46
 
    for part in email.iterators.typed_subpart_iterator(message, 'multipart'):
47
 
        subparts = part.get_payload()
48
 
        for subpart in subparts:
49
 
            if subpart.get_content_type() == 'text/plain':
50
 
                return subpart.get_payload().strip()
51
 
    return ''
52
 
 
53
 
 
54
 
def make_json_message(message_id, raw_message, hidden=False):
55
 
    message = email.message_from_string(raw_message)
56
 
    headers = dict(message.items())
 
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
57
43
    message = {
58
44
        'message_id': message_id,
59
45
        'headers': headers,
60
 
        # This is broken because the in-reply-to must be encoded.
61
 
        # X-Message-ID-Hash is calculated from the Base 32.
62
 
        'thread_id': headers.get('in-reply-to', message_id),
63
 
        'date': headers.get('date'),
64
 
        'subject': headers.get('subject'),
65
 
        'author': headers.get('from'),
 
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'),
66
50
        'hidden': hidden,
67
51
        'attachments': [],
68
 
        'replies': headers.get('in-reply-to'),
69
 
        'body': get_body_text(message),
 
52
        'replies': headers.get('in-reply-to', None),
 
53
        'body': body,
70
54
        }
71
55
    return message
72
56
 
82
66
    def is_multipart(message):
83
67
        return isinstance(message['body'], list)
84
68
 
85
 
    def put_archive(self, archive_id, raw_archive=None):
86
 
        # XXX sinzui 2012-02-29: this needs to raise an error
87
 
        # if the th archive_id is invalid, or the raw archive is not mbox.
88
 
        if archive_id in self.message_archives:
89
 
            raise ArchiveIdExists()
90
 
        self.message_archives[archive_id] = []
91
 
 
92
 
    def put_message(self, archive_id, message_id, raw_message):
93
 
        # XXX sinzui 2012-02-29: this needs to raise an error
94
 
        # if the th archive_id is invalid, message_id is not base32
95
 
        # or the raw message is not an email.
96
 
        if archive_id not in self.message_archives:
97
 
            raise ArchiveIdNotFound()
98
 
        if not raw_message:
99
 
            raise ValueError('raw_message is not a message.')
100
 
        json_message = make_json_message(message_id, raw_message)
 
69
    def put_message(self, archive_id, message_id, message):
 
70
        # Make a json message.
101
71
        messages = self.message_archives[archive_id]
 
72
        json_message = make_json_message(message_id, body=message)
102
73
        messages.append(json_message)
103
74
 
104
75
    def get_messages(self, archive_id, query_string):
196
167
            }
197
168
        return response
198
169
 
199
 
    def hide_message(self, archive_id, message_id, query_string):
200
 
        """Change the visbility of a message in an archive.
 
170
    def hide_message(self, archive_id, query_string):
 
171
        """Return matching messages.
201
172
 
202
173
        :param archive_id: The archive to retrieve from.
203
174
        :param query_string: Contains 'parameters', which is a JSON-format
205
176
        """
206
177
        query = parse_qs(query_string)
207
178
        parameters = simplejson.loads(query['parameters'][0])
 
179
        message_id = parameters['message_id']
208
180
        hidden = parameters['hidden']
209
181
        messages = self.message_archives[archive_id]
210
182
        for message in messages: