File: JXG.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 (74 lines) | stat: -rw-r--r-- 2,585 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
import json
import inspect

class Request(object):

    def __init__(self, action, id, data):
        self._action = action
        self._id = id
        self._data = data

    def getValue(self, item, default = "empty"):
        if item == 'action':
            return self._action
        elif item == 'id':
            return self._id
        else:
            return json.loads(self._data)[item]

    def getList(self, item):
        return self._data.getlist(item)


class Response(object):

    def __init__(self, _id):
        self._id = _id
        self._type = 'response'
        self._data = {}
        self._fields = []
        self._handler = []

    def error(self, msg):
        self._type = 'error'
        self._message = msg

    def dump(self):
        if self._type == 'error':
            # drop all the data and methods, just output the error
            return json.dumps({                                 \
                                'type'    : 'error',            \
                                'id'      : self._id,           \
                                'message' : self._message       \
                              })
        else:
            return json.dumps({                                 \
                                'type'    : 'response',         \
                                'id'      : self._id,           \
                                'fields'  : self._fields,       \
                                'handler' : self._handler,      \
                                'data'    : self._data          \
                              })

    def addField(self, namespace, name, value):
        self._fields.append({                                   \
                              'namespace' : namespace,          \
                              'name'      : name,               \
                              'value'     : value               \
                            })

    def addData(self, name, value):
        self._data[name] = value

    def addHandler(self, function, callback):
        params = [];
        args = inspect.getargspec(function);
        for i in range(0, len(args.args)):
            if (args.args[i] != 'resp') and (args.args[i] != 'self'):
                params.append(args.args[i])

        self._handler.append({                                  \
                               'name'       : function.__name__,\
                               'callback'   : callback,         \
                               'parameters' : params            \
                             })