svgui/pyjs/jsonrpc/jsonrpc.py
author Surkov Sergey <surkovsv93@gmail.com>
Mon, 28 Aug 2017 13:43:46 +0300
changeset 1793 c2f48d791d9f
parent 1780 c52d1460cea8
child 1832 0f1081928d65
permissions -rw-r--r--
fix bug with unfinished debug thread
this bug occurs when user doesn't stop debugging process in current project, and opens another project
import gluon.contrib.simplejson as simplejson
import types
import sys


class JSONRPCServiceBase:

    def __init__(self):
        self.methods = {}

    def response(self, id, result):
        return simplejson.dumps({'version': '1.1', 'id': id,
                                 'result': result, 'error': None})

    def error(self, id, code, message):
        return simplejson.dumps({
            'id': id,
            'version': '1.1',
            'error': {'name': 'JSONRPCError',
                      'code': code,
                      'message': message}
        })

    def add_method(self, name, method):
        self.methods[name] = method

    def process(self, data):
        data = simplejson.loads(data)
        id, method, params = data["id"], data["method"], data["params"]
        if method in self.methods:
            try:
                result = self.methods[method](*params)
                return self.response(id, result)
            except BaseException:
                etype, eval, etb = sys.exc_info()
                return self.error(id, 100, '%s: %s' % (etype.__name__, eval))
            except Exception:
                etype, eval, etb = sys.exc_info()
                return self.error(id, 100, 'Exception %s: %s' % (etype, eval))
        else:
            return self.error(id, 100, 'method "%s" does not exist' % method)

    def listmethods(self):
        return self.methods.keys()