File: gateway.py

package info (click to toggle)
pyamf 0.6.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,692 kB
  • sloc: python: 17,944; xml: 455; makefile: 116; sql: 38; java: 11; sh: 7
file content (88 lines) | stat: -rw-r--r-- 2,168 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (c) The PyAMF Project.
# See LICENSE.txt for details.

"""
Gateway for RecordSet remoting service.

@since: 0.1.0
"""

from sqlalchemy.sql import select

from pyamf import register_class, amf0

import db

def as_recordset(result):
    keys = None

    if hasattr(result, 'keys'):
        keys = result.keys
    elif hasattr(result, '_ResultProxy__keys'):
        keys = result._ResultProxy__keys

    if keys is None:
        raise AttributeError('Unknown keys for result')

    return amf0.RecordSet(keys, [list(x) for x in result])

class SoftwareService(object):
    def __init__(self, engine):
        self.engine = engine

    def getLanguages(self):
        """
        Returns all the languages.
        """
        return as_recordset(self.engine.execute(
            select([db.language]).order_by(db.language.c.Name.desc())
        ))

    def getSoftware(self, lang):
        """
        Returns all the software projects for the selected language.
        """
        return as_recordset(self.engine.execute(
            select([db.software], db.software.c.CategoryID == lang)
        ))

def parse_args(args):
    """
    Parse commandline options.
    """
    from optparse import OptionParser

    parser = OptionParser()
    parser.add_option('--host', dest='host', default='localhost',
                      help='The host address for the AMF gateway')
    parser.add_option('-p', '--port', dest='port', default=8000,
                      help='The port number the server uses')

    return parser.parse_args(args)

if __name__ == '__main__':
    import sys
    from pyamf.remoting.gateway.wsgi import WSGIGateway
    from wsgiref import simple_server

    options = parse_args(sys.argv[1:])[0]
    service = {'service': SoftwareService(db.get_engine())}

    host = options.host
    port = int(options.port)

    gw = WSGIGateway(service)

    httpd = simple_server.WSGIServer(
        (host, port),
        simple_server.WSGIRequestHandler,
    )

    httpd.set_app(gw)

    print 'Started RecordSet example server on http://%s:%s' % (host, str(port) )

    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass