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

« back to all changes in this revision

Viewing changes to grackle/server/model.py

  • Committer: William Grant
  • Date: 2012-01-22 08:27:01 UTC
  • Revision ID: william.grant@canonical.com-20120122082701-b7iiqxdxsbpj3krq
Test add_message a little.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2012 Canonical Ltd
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as published by
 
5
# the Free Software Foundation, either version 3 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public
 
14
# License along with this program. If not, see
 
15
# <http://www.gnu.org/licenses/>.
 
16
 
 
17
import datetime
 
18
import dateutil.tz
 
19
import email.parser
 
20
from email.utils import parsedate_tz
 
21
import logging
 
22
import uuid
 
23
 
 
24
import pycassa
 
25
from pycassa.system_manager import (
 
26
    LEXICAL_UUID_TYPE,
 
27
    SystemManager,
 
28
    TIME_UUID_TYPE,
 
29
    )
 
30
 
 
31
from grackle.server.cassandra import workaround_1779
 
32
 
 
33
 
 
34
def create_schema(host, keyspace, clobber=False):
 
35
    mgr = SystemManager(host)
 
36
 
 
37
    if clobber:
 
38
        for cf in mgr.get_keyspace_column_families(keyspace):
 
39
            mgr.drop_column_family(keyspace, cf)
 
40
 
 
41
    try:
 
42
        workaround_1779(
 
43
            mgr.create_column_family, keyspace, 'message',
 
44
            key_validation_class=LEXICAL_UUID_TYPE)
 
45
        workaround_1779(
 
46
            mgr.create_column_family, keyspace, 'archive_message',
 
47
            comparator_type=TIME_UUID_TYPE,
 
48
            default_validation_class=LEXICAL_UUID_TYPE)
 
49
        pass
 
50
    finally:
 
51
        mgr.close()
 
52
 
 
53
 
 
54
class CassandraConnection(object):
 
55
 
 
56
    def __init__(self, keyspace, host):
 
57
        self._keyspace = keyspace
 
58
        self._host = host
 
59
        self._connection = self._connect()
 
60
        self.messages = self._column_family('message')
 
61
        self.archive_messages = self._column_family('archive_message')
 
62
 
 
63
    def _connect(self):
 
64
        return pycassa.connect(self._keyspace, self._host)
 
65
 
 
66
    def _column_family(self, name):
 
67
        return pycassa.ColumnFamily(self._connection, name)
 
68
 
 
69
    def add_message(self, archive_uuid, message):
 
70
        message_uuid = uuid.uuid4()
 
71
        parsed = email.parser.Parser().parsestr(message)
 
72
        date = parsed.get('date')
 
73
        if date is not None:
 
74
            try:
 
75
                pdate = parsedate_tz(date)
 
76
                date = datetime.datetime(
 
77
                    *pdate[:6],
 
78
                    tzinfo=dateutil.tz.tzoffset('', pdate[9]))
 
79
            except ValueError:
 
80
                pass
 
81
        self.messages.insert(
 
82
            message_uuid,
 
83
            {'date_created': datetime.datetime.utcnow().isoformat() + 'Z',
 
84
             'content': message,
 
85
             'from': parsed.get('From'),
 
86
             'to': parsed.get('To'),
 
87
             'subject': parsed.get('Subject'),
 
88
             'date': date.isoformat() if date is not None else None,
 
89
             'message_id': parsed.get('Message-ID'),
 
90
             })
 
91
        self.archive_messages.insert(
 
92
            archive_uuid,
 
93
            {date.astimezone(dateutil.tz.tzutc()): message_uuid})
 
94
        logging.debug(
 
95
            'Imported %s into %s' % (parsed.get('Message-ID'), archive_uuid))
 
96
        return message_uuid
 
97
 
 
98
    def _format_message(self, message):
 
99
        return {
 
100
            'date': message['date'],
 
101
            'from': message['from'],
 
102
            'subject': message['subject'],
 
103
            }
 
104
 
 
105
    def get_messages(self, archive_uuid, order, count, start):
 
106
        if order in ("date", "-date"):
 
107
            reversed = order[0] == '-'
 
108
        else:
 
109
            raise AssertionError("Unsupported order.")
 
110
        pairs = self.archive_messages.get(
 
111
            archive_uuid, column_count=count + 1,
 
112
            column_start=start, column_reversed=reversed).items()
 
113
        ids = [v for k, v in pairs]
 
114
        messages = self.messages.multiget(
 
115
            ids, columns=['date', 'from', 'subject'])
 
116
        actual_count = len(pairs)
 
117
        if len(pairs) > count:
 
118
            assert len(pairs) == count + 1
 
119
            actual_count -= 1
 
120
            next_memo = str(pairs[count][0])
 
121
        else:
 
122
            next_memo = None
 
123
        return (
 
124
            [self._format_message(messages[id]) for id in ids[:actual_count]],
 
125
            next_memo,
 
126
            )