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

« back to all changes in this revision

Viewing changes to grackle/model.py

  • Committer: William Grant
  • Date: 2012-01-22 10:39:15 UTC
  • Revision ID: william.grant@canonical.com-20120122103915-lqjw4jsaw3ug1q2g
Merge grackle.server into grackle. Alter Makefile to run all the tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import email.parser
20
20
from email.utils import parsedate_tz
21
21
import logging
22
 
import time
23
22
import uuid
24
23
 
25
24
import pycassa
79
78
    return date, message_dict
80
79
 
81
80
 
82
 
def _utc_datetime(dt):
83
 
    return dt.astimezone(dateutil.tz.tzutc())
84
 
 
85
 
 
86
 
def _utc_timestamp(dt):
87
 
    return time.mktime(_utc_datetime(dt).timetuple()) - time.timezone
88
 
 
89
 
 
90
81
class CassandraConnection(object):
91
82
 
92
83
    def __init__(self, keyspace, host):
111
102
        self.messages.insert(message_uuid, message_dict)
112
103
        self.archive_messages.insert(
113
104
            archive_uuid,
114
 
            {_utc_timestamp(message_date): message_uuid})
 
105
            {message_date.astimezone(dateutil.tz.tzutc()): message_uuid})
115
106
        logging.debug(
116
107
            'Imported %s into %s'
117
108
            % (message_dict.get('message-id', None), archive_uuid))
122
113
            'date': message.get('date'),
123
114
            'from': message.get('from'),
124
115
            'subject': message.get('subject'),
125
 
            'message-id': message.get('message-id'),
126
116
            }
127
117
 
128
 
    def _trim(self, sequence, end):
129
 
        """Return the sequence with one of the ends trimmed.
130
 
 
131
 
        :param end: if true, remove the last element. otherwise remove
132
 
            the first.
133
 
        """
134
 
        if end:
135
 
            return sequence[:-1]
136
 
        else:
137
 
            return sequence[1:]
138
 
 
139
 
    def get_messages(self, archive_uuid, order, count, memo, backward=False):
 
118
    def get_messages(self, archive_uuid, order, count, start):
140
119
        if order in ("date", "-date"):
141
120
            reversed = order[0] == '-'
142
121
        else:
143
122
            raise AssertionError("Unsupported order.")
144
 
        if memo != '':
145
 
            memo = uuid.UUID(memo)
146
 
        if backward:
147
 
            start = ''
148
 
            finish = memo
149
 
        else:
150
 
            start = memo
151
 
            finish = ''
152
 
 
153
 
        # Get up to n+1 messages from the memo: the last item of the
154
 
        # previous batch (because that's where the memo starts) + this
155
 
        # batch.
156
123
        pairs = self.archive_messages.get(
157
 
            archive_uuid, column_count=count + 1, column_start=start,
158
 
            column_finish=finish, column_reversed=reversed).items()
159
 
 
160
 
        if len(pairs) and memo and pairs[0][0] <= memo:
161
 
            # The memo (from the previous batch) was included in the result.
162
 
            # Trim it.
163
 
            pairs = self._trim(pairs, False ^ backward)
164
 
        elif len(pairs) > count:
165
 
            # There was no memo in the result, so the n+1th element is
166
 
            # unnecessary. Kill it.
167
 
            pairs = self._trim(pairs, True ^ backward)
168
 
 
169
 
        if len(pairs) == 0:
170
 
            return (None, [], None)
171
 
 
172
 
        assert 0 < len(pairs) <= count
173
 
 
174
 
        # We've narrowed down the message references. Fetch the messages.
 
124
            archive_uuid, column_count=count + 1,
 
125
            column_start=start, column_reversed=reversed).items()
175
126
        ids = [v for k, v in pairs]
176
127
        messages = self.messages.multiget(
177
 
            ids, columns=['date', 'from', 'subject', 'message-id'])
178
 
 
 
128
            ids, columns=['date', 'from', 'subject'])
 
129
        actual_count = len(pairs)
 
130
        if len(pairs) > count:
 
131
            assert len(pairs) == count + 1
 
132
            actual_count -= 1
 
133
            next_memo = str(pairs[count][0])
 
134
        else:
 
135
            next_memo = None
179
136
        return (
180
 
            str(pairs[0][0]),
181
 
            [self._format_message(messages[id]) for id in ids],
182
 
            str(pairs[-1][0]),
 
137
            [self._format_message(messages[id]) for id in ids[:actual_count]],
 
138
            next_memo,
183
139
            )