~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-23 05:57:50 UTC
  • Revision ID: william.grant@canonical.com-20120123055750-veg4p0rv3fnef9z3
So it turns out that calendar.timegm is a UTC equivalent of time.mktime.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# License along with this program. If not, see
15
15
# <http://www.gnu.org/licenses/>.
16
16
 
 
17
import calendar
17
18
import datetime
18
19
import dateutil.tz
19
20
import email.parser
20
21
from email.utils import parsedate_tz
 
22
import functools
21
23
import logging
22
 
import time
23
24
import uuid
24
25
 
25
26
import pycassa
85
86
 
86
87
 
87
88
def _utc_timestamp(dt):
88
 
    return time.mktime(_utc_datetime(dt).timetuple()) - time.timezone
 
89
    return calendar.timegm(_utc_datetime(dt).timetuple())
89
90
 
90
91
 
91
92
def _utc_timeuuid(dt, lowest_val=True):
121
122
    return memo, start, finish
122
123
 
123
124
 
 
125
LEGAL_HEADERS = set([
 
126
    'date', 'from', 'subject', 'message-id',
 
127
    ])
 
128
 
 
129
 
 
130
def _format_message(message, headers=[], include_raw=False):
 
131
    data = {}
 
132
 
 
133
    if headers:
 
134
        assert not set(headers).difference(LEGAL_HEADERS)
 
135
        hdict = {}
 
136
        for header in headers:
 
137
            hdict[header] = message.get(header)
 
138
        data['headers'] = hdict
 
139
 
 
140
    if include_raw:
 
141
        data['raw'] = message['raw']
 
142
 
 
143
    return data
 
144
 
 
145
 
124
146
class CassandraConnection(object):
125
147
 
126
148
    def __init__(self, keyspace, host):
139
161
    def add_message(self, archive_uuid, message):
140
162
        message_uuid = uuid.uuid4()
141
163
        message_date, message_dict = _parse_message(message)
142
 
        message_dict['content'] = message
 
164
        message_dict['raw'] = message
143
165
        message_dict['date_created'] = (
144
166
            datetime.datetime.utcnow().isoformat() + 'Z')
145
167
        self.messages.insert(message_uuid, message_dict)
151
173
            % (message_dict.get('message-id', None), archive_uuid))
152
174
        return message_uuid
153
175
 
154
 
    def _format_message(self, message):
155
 
        return {
156
 
            'date': message.get('date'),
157
 
            'from': message.get('from'),
158
 
            'subject': message.get('subject'),
159
 
            'message-id': message.get('message-id'),
160
 
            }
161
 
 
162
176
    def _trim(self, sequence, end):
163
177
        """Return the sequence with one of the ends trimmed.
164
178
 
171
185
            return sequence[1:]
172
186
 
173
187
    def get_messages(self, archive_uuid, order, count, memo, backward=False,
174
 
                     start_date=None, finish_date=None):
 
188
                     start_date=None, finish_date=None, format='all',
 
189
                     headers=['from', 'date', 'subject', 'message-id']):
175
190
        if order in ("date", "-date"):
176
191
            reversed = order[0] == '-'
177
192
        else:
203
218
 
204
219
        # We've narrowed down the message references. Fetch the messages.
205
220
        ids = [v for k, v in pairs]
206
 
        messages = self.messages.multiget(
207
 
            ids, columns=['date', 'from', 'subject', 'message-id'])
 
221
        formatter = functools.partial(
 
222
            _format_message, headers=headers, include_raw=True)
 
223
        # XXX: No need to get all columns. Restrict based on format.
 
224
        messages = self.messages.multiget(ids)
208
225
 
209
226
        return (
210
227
            str(pairs[0][0]),
211
 
            [self._format_message(messages[id]) for id in ids],
 
228
            [formatter(messages[id]) for id in ids],
212
229
            str(pairs[-1][0]),
213
230
            )