File: JXGServer.py

package info (click to toggle)
jsxgraph 0.83%2Bsvn1872~dfsg1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 35,272 kB
  • sloc: xml: 5,881; java: 1,072; python: 884; php: 238; makefile: 70; objc: 30; sh: 1
file content (99 lines) | stat: -rwxr-xr-x 2,289 bytes parent folder | download | duplicates (7)
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
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python

# ZIP compression
import zlib
# Base64 en-/decoding
import base64
# CGI variables handling
import cgi
#import cgitb
#cgitb.enable()

import JXG
import inspect


# Base plugin
from JXGServerModule import JXGServerModule

def print_httpheader():
    print """\
Content-Type: text/plain\n
"""


def default_action(req, resp):
    action = req.getvalue('action', 'empty')
    resp.error("action \"" + action + "\" is undefined")
    return resp.dump()
    

def import_module(plugin, resp):
    try:
        __import__(plugin, None, None, [''])

        tp = JXGServerModule.__subclasses__()
        if len(tp) == 0:
            resp.error("error loading module \"" + plugin + "\"")
            return tp
        tp = tp[0]()

        if not tp.isJXGServerModule:
            resp.error("not a jxg server module: \"" + plugin + "\"")
            return tp
    except Exception as e:
        resp.error("error loading jxg server module: \"" + plugin + "\": " + e.__str__())
        return

    return tp

def load_module(req, resp):
    plugin = req.getValue("module", "none")
    tp = import_module(plugin, resp)

    if resp._type != 'error':
        tp.init(resp)
    return resp.dump()


def exec_module(req, resp):
    handler = req.getValue('handler', 'none')
    module = req.getValue('module', 'none')

    m = import_module(module, resp)
    if resp._type == 'error':
        return resp.dump()

    method = getattr(m, handler)
    params = ()
    args = inspect.getargspec(method)
    for i in range(0, len(args.args)):
        if args.args[i] == 'self':
            pass
        elif args.args[i] == 'resp':
            params += (resp, )
        else:
            params += (req.getValue(args.args[i]), )

    apply(method, params)

    return resp.dump()


# Get Data from post/get parameters
form = cgi.FieldStorage();

action = form.getfirst('action', 'empty')
id = form.getfirst('id', 'none')
data = base64.b64decode(form.getfirst('dataJSON', ''))

actions_map = {                         \
                 'load': load_module,   \
                 'exec': exec_module    \
              }
ret = actions_map.get(action, default_action)(JXG.Request(action, id, data), JXG.Response(id))

print_httpheader()

print base64.b64encode(zlib.compress(ret, 9))