~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 06:35:09 UTC
  • Revision ID: william.grant@canonical.com-20120122063509-4dbvndxl7jhg7mvv
Extract some message metadata. Log betterer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# <http://www.gnu.org/licenses/>.
16
16
 
17
17
import datetime
 
18
import dateutil.tz
 
19
import email.parser
 
20
from email.utils import parsedate_tz
 
21
import logging
18
22
import uuid
19
23
 
20
24
import pycassa
64
68
 
65
69
    def add_message(self, archive_uuid, message):
66
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
67
81
        self.messages.insert(
68
82
            message_uuid,
69
83
            {'date_created': datetime.datetime.utcnow().isoformat() + 'Z',
70
84
             'content': message,
 
85
             'from': parsed.get('From'),
 
86
             'subject': parsed.get('Subject'),
 
87
             'date': date.isoformat() if date is not None else None,
 
88
             'message_id': parsed.get('Message-ID'),
71
89
             })
72
90
        self.archive_messages.insert(
73
 
            archive_uuid, {datetime.datetime.now(): message_uuid})
 
91
            archive_uuid,
 
92
            {date.astimezone(dateutil.tz.tzutc()): message_uuid})
 
93
        logging.debug(
 
94
            'Imported %s into %s' % (parsed.get('Message-ID'), archive_uuid))
74
95
        return message_uuid
75
96
 
 
97
    def _format_message(self, message):
 
98
        return {
 
99
            'date': message['date'],
 
100
            'from': message['from'],
 
101
            'subject': message['subject'],
 
102
            }
 
103
 
76
104
    def get_messages(self, archive_uuid):
77
105
        ids = self.archive_messages.get(
78
106
            archive_uuid, column_count=10, column_reversed=True).values()
79
107
        messages = self.messages.multiget(ids)
80
 
        return [messages[id]['content'] for id in ids]
 
108
        return [self._format_message(messages[id]) for id in ids]