jsonrpc-base

Pure python standard library JSONRPC data interface
git clone git://git.defalsify.org/python-jsonrpc-base.git
Log | Files | Refs | LICENSE

commit 4d0bdb8806b22605492a99c7fbb51961f8266f94
parent 6082d05093983f118f265e88917e210a64f96aee
Author: nolash <dev@holbrook.no>
Date:   Fri,  9 Apr 2021 11:28:38 +0200

Rename package

Diffstat:
Djsonrpc_base/base.py | 4----
Djsonrpc_base/error.py | 86-------------------------------------------------------------------------------
Djsonrpc_base/interface.py | 68--------------------------------------------------------------------
Msetup.cfg | 2+-
Mtests/test_error.py | 4++--
Mtests/test_request.py | 4++--
6 files changed, 5 insertions(+), 163 deletions(-)

diff --git a/jsonrpc_base/base.py b/jsonrpc_base/base.py @@ -1,4 +0,0 @@ -class JSONRPCBase: - major_version = 2 - minor_version = 0 - version_string = '2.0' diff --git a/jsonrpc_base/error.py b/jsonrpc_base/error.py @@ -1,86 +0,0 @@ -# local imports -from .base import JSONRPCBase - -class JSONRPCException(Exception, JSONRPCBase): - message = 'Unknown' - - def __init__(self, v): - context_v = '{} error'.format(self.message) - if v != None: - context_v += ': ' + v - - super(JSONRPCException, self).__init__(context_v) - - -class JSONRPCCustomException(JSONRPCException): - code = -32000 - message = 'Server' - - -class JSONRPCParseError(JSONRPCException): - code = -32700 - message = 'Parse' - - -class JSONRPCInvalidRequestError(JSONRPCException): - code = -32600 - message = 'Invalid request' - - -class JSONRPCMethodNotFoundError(JSONRPCException): - code = -32601 - message = 'Method not found' - - -class JSONRPCInvalidParametersError(JSONRPCException): - code = -32602 - message = 'Invalid parameters' - - -class JSONRPCInternalError(JSONRPCException): - code = -32603 - message = 'Internal' - - -class JSONRPCUnhandledErrorException(KeyError): - pass - - -class JSONRPCErrors: - reserved_max = -31999 - reserved_min = -32768 - local_max = -32000 - local_min = -32099 - - translations = { - -32700: JSONRPCParseError, - -32600: JSONRPCInvalidRequestError, - -32601: JSONRPCMethodNotFoundError, - -32602: JSONRPCInvalidParametersError, - -32603: JSONRPCInternalError, - } - - @classmethod - def add(self, code, exception_object): - if code < self.local_min or code > self.local_max: - raise ValueError('code must be in range <{},{}>'.format(self.local_min, self.local_max)) - exc = self.translations.get(code) - if exc != None: - raise ValueError('code already registered with {}'.format(exc)) - - if not issubclass(exception_object, JSONRPCCustomException): - raise ValueError('exception object must be a subclass of jsonrpc_base.error.JSONRPCCustomException') - - self.translations[code] = exception_object - - - @classmethod - def get(self, code, v=None): - e = self.translations.get(code) - if e == None: - raise JSONRPCUnhandledErrorException(code) - return e(v) - - -class InvalidJSONRPCError(ValueError): - pass diff --git a/jsonrpc_base/interface.py b/jsonrpc_base/interface.py @@ -1,68 +0,0 @@ -# standard imports -import uuid - -# local imports -from .base import JSONRPCBase -from .error import ( - JSONRPCErrors, - ) - - -class DefaultErrorParser: - - def translate(self, error): - code = error['error']['code'] - message = error['error']['message'] - if type(code).__name__ != 'int': - raise ValueError('error code is not int by {} in error {}'.format(type(code), error)) - - exc = None - try: - exc = JSONRPCErrors.get(code, message) - except KeyError: - return JSONRPCUndefinedError(code, message) - - -def jsonrpc_template(): - return { - 'jsonrpc': JSONRPCBase.version_string, - 'id': str(uuid.uuid4()), - 'method': None, - 'params': [], - } - - -def jsonrpc_request(method): - req = jsonrpc_template() - req['method'] = method - return req - - -def jsonrpc_result(o, ep): - if o.get('error') != None: - raise ep.translate(o) - return o['result'] - - -def jsonrpc_response(request_id, result): - return { - 'jsonrpc': JSONRPCBase.version_string, - 'id': request_id, - 'result': result, - } - - -def jsonrpc_error(request_id, code, message=None): - e = JSONRPCErrors.get(code, message) - return { - 'jsonrpc': JSONRPCBase.version_string, - 'id': request_id, - 'error': { - 'code': code, - 'message': str(e), - }, - } - - -def jsonrpc_is_response_to(request, response): - return request['id'] == response['id'] diff --git a/setup.cfg b/setup.cfg @@ -1,5 +1,5 @@ [metadata] -name = jsonrpc_base +name = jsonrpc_std version = 0.0.1a1 description = Pure python standard library JSONRPC data interface author = Louis Holbrook diff --git a/tests/test_error.py b/tests/test_error.py @@ -2,13 +2,13 @@ import unittest # local imports -from jsonrpc_base.error import ( +from jsonrpc_std.error import ( JSONRPCException, JSONRPCCustomException, JSONRPCErrors, JSONRPCParseError, ) -from jsonrpc_base.interface import ( +from jsonrpc_std.interface import ( jsonrpc_error, DefaultErrorParser, ) diff --git a/tests/test_request.py b/tests/test_request.py @@ -2,8 +2,8 @@ import unittest # local imports -from jsonrpc_base.base import JSONRPCBase -from jsonrpc_base.interface import ( +from jsonrpc_std.base import JSONRPCBase +from jsonrpc_std.interface import ( jsonrpc_request, jsonrpc_response, jsonrpc_result,