svgui/pyjs/jsonrpc/jsonrpc.py
author Edouard Tisserant
Mon, 15 Mar 2021 11:25:24 +0100
branchsvghmi
changeset 3188 c173452bf894
parent 1881 091005ec69c4
permissions -rw-r--r--
SVGHMI: Fixed problems happening when value is higher than maximum or lower than minimum in Input widget. "max" and "min" string was getting into keypad text field, was not diplayed systematically, and was staying forever sometime.
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1866
diff changeset
     1
from __future__ import absolute_import
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     2
import sys
1832
0f1081928d65 fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
     3
import gluon.contrib.simplejson as simplejson
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     4
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
     5
1851
1b8b5324506c fix pylint warning '(old-style-class) Old-style class defined.'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
     6
class JSONRPCServiceBase(object):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     7
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     8
    def __init__(self):
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
     9
        self.methods = {}
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    10
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    11
    def response(self, id, result):
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    12
        return simplejson.dumps({'version': '1.1', 'id': id,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    13
                                 'result': result, 'error': None})
1751
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
    14
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    15
    def error(self, id, code, message):
1773
38fde37c3766 clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
    16
        return simplejson.dumps({
38fde37c3766 clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
    17
            'id': id,
38fde37c3766 clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
    18
            'version': '1.1',
38fde37c3766 clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
    19
            'error': {'name': 'JSONRPCError',
38fde37c3766 clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
    20
                      'code': code,
38fde37c3766 clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
    21
                      'message': message}
38fde37c3766 clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
    22
        })
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    23
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    24
    def add_method(self, name, method):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    25
        self.methods[name] = method
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    26
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
    def process(self, data):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    28
        data = simplejson.loads(data)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
        id, method, params = data["id"], data["method"], data["params"]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    30
        if method in self.methods:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    31
            try:
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    32
                result = self.methods[method](*params)
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    33
                return self.response(id, result)
1866
9d1e68d557bf fix pylint error '(bad-except-order) Bad except clauses order (X is an ancestor class of Y)'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1851
diff changeset
    34
            except Exception:
9d1e68d557bf fix pylint error '(bad-except-order) Bad except clauses order (X is an ancestor class of Y)'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1851
diff changeset
    35
                etype, eval, _etb = sys.exc_info()
9d1e68d557bf fix pylint error '(bad-except-order) Bad except clauses order (X is an ancestor class of Y)'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1851
diff changeset
    36
                return self.error(id, 100, 'Exception %s: %s' % (etype, eval))
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    37
            except BaseException:
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1832
diff changeset
    38
                etype, eval, _etb = sys.exc_info()
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    39
                return self.error(id, 100, '%s: %s' % (etype.__name__, eval))
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    40
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    41
            return self.error(id, 100, 'method "%s" does not exist' % method)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    42
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    43
    def listmethods(self):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
    44
        return self.methods.keys()