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

« back to all changes in this revision

Viewing changes to grackle/wsgi.py

  • Committer: Aaron Bentley
  • Date: 2012-01-10 11:16:56 UTC
  • Revision ID: aaron@canonical.com-20120110111656-9daoyj766z8r184a
Add test framework.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2011 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
 
__metaclass__ = type
18
 
 
19
 
import json
20
 
import uuid
21
 
 
22
 
import web
23
 
 
24
 
from grackle.model import CassandraConnection
25
 
 
26
 
conn = None
27
 
 
28
 
 
29
 
class root:
30
 
 
31
 
    def GET(self):
32
 
        web.header('Content-Type', 'text/plain')
33
 
        return (
34
 
            'Service endpoint for Grackle.\n\n'
35
 
            'When you want mail that was archived in Grackle, you can get it '
36
 
            'from Grackle')
37
 
 
38
 
 
39
 
class archive:
40
 
 
41
 
    def GET(self, name):
42
 
        web.header('Content-Type', 'text/plain')
43
 
        args = web.input()
44
 
        if not getattr(args, 'op', None):
45
 
            return self.index(name)
46
 
 
47
 
        if args.op == 'get_messages':
48
 
            return self.get_messages(name, args)
49
 
 
50
 
    def POST(self, name):
51
 
        data = web.data()
52
 
        conn.add_message(name, data)
53
 
        web.ctx.status = '201 Created'
54
 
 
55
 
    def index(self, name):
56
 
        return ('Service for archive %s.' % name)
57
 
 
58
 
    def get_messages(self, name, args):
59
 
        try:
60
 
            count = int(args.count)
61
 
        except (AttributeError, ValueError):
62
 
            count = 10
63
 
        order = getattr(args, 'order', '-date')
64
 
        memo = getattr(args, 'memo', '')
65
 
        prev, messages, next = conn.get_messages(name, order, count, memo)
66
 
        return json.dumps({
67
 
            'prev_memo': prev,
68
 
            'messages': messages,
69
 
            'next_memo': next,
70
 
            })
71
 
 
72
 
 
73
 
urls = (
74
 
    '/', 'root',
75
 
    '/archive/(.+)', 'archive',
76
 
    )
77
 
 
78
 
app = web.application(urls, globals(), autoreload=False)
79
 
 
80
 
if __name__ == "__main__":
81
 
    import logging
82
 
    logging.basicConfig(
83
 
        format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
84
 
        level=logging.DEBUG)
85
 
    conn = CassandraConnection('grackle', ['localhost:9160'])
86
 
    app.run()