From b60318f304c45a2b7e93d3e1a7912b4eda77b455 Mon Sep 17 00:00:00 2001 From: Agus Danangjoyo <68761014+Danangjoyoo@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:51:55 +0700 Subject: [PATCH 01/13] Create pylint.yml --- .github/workflows/pylint.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/pylint.yml diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml new file mode 100644 index 0000000..c9c962e --- /dev/null +++ b/.github/workflows/pylint.yml @@ -0,0 +1,23 @@ +name: Pylint + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + - name: Analysing the code with pylint + run: | + pylint $(git ls-files '*.py') From 3f03036bfc479a9b6d76c347c79a74cf7d584707 Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Fri, 12 Jan 2024 17:54:54 +0700 Subject: [PATCH 02/13] refactor: import --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 5a91488..029ba89 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ +from pathlib import Path from setuptools import setup, find_packages # read the contents of your README file -from pathlib import Path this_directory = Path(__file__).parent long_description = (this_directory / "README.md").read_text() From 4e52712b47ff6c23dd508f7f83776d97320f502d Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Fri, 12 Jan 2024 17:57:33 +0700 Subject: [PATCH 03/13] chore: pylint --- oborpc/base/meta.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/oborpc/base/meta.py b/oborpc/base/meta.py index 615952e..1c05431 100644 --- a/oborpc/base/meta.py +++ b/oborpc/base/meta.py @@ -7,8 +7,8 @@ class OBORMeta(type): Meta class used """ __obor_registry__ = {} - def __new__(mcls, name, bases, namespace, /, **kwargs): - cls = super().__new__(mcls, name, bases, namespace, **kwargs) + def __new__(mcs, name, bases, namespace, /, **kwargs): + cls = super().__new__(mcs, name, bases, namespace, **kwargs) cls.__oborprocedures__ = { methodname for methodname, value in namespace.items() @@ -23,3 +23,8 @@ class OBORBase(metaclass=OBORMeta): """ Obor Base Class """ + def __repr__(self) -> str: + return "" + + def __str__(self) -> str: + return self.__repr__() From 77c8046af80f0ec1320008a1d1e27754cd587447 Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Fri, 12 Jan 2024 17:59:14 +0700 Subject: [PATCH 04/13] chore: pylint --- oborpc/base/meta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oborpc/base/meta.py b/oborpc/base/meta.py index 1c05431..818fcff 100644 --- a/oborpc/base/meta.py +++ b/oborpc/base/meta.py @@ -19,7 +19,7 @@ def __new__(mcs, name, bases, namespace, /, **kwargs): return cls -class OBORBase(metaclass=OBORMeta): +class OBORBase(metaclass=OBORMeta): # pylint: disable=too-few-public-methods """ Obor Base Class """ From 7b3aae07c5ab5c3d22bd7c53365a57b9f7cca4bc Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Fri, 12 Jan 2024 18:03:25 +0700 Subject: [PATCH 05/13] chore: pylint --- oborpc/base/__init__.py | 3 +++ oborpc/builder/__init__.py | 6 +++++- oborpc/builder/_fastapi.py | 2 ++ oborpc/builder/_flask.py | 13 ++++++++++++- oborpc/builder/_server.py | 9 +++++++++ oborpc/decorator.py | 1 + 6 files changed, 32 insertions(+), 2 deletions(-) diff --git a/oborpc/base/__init__.py b/oborpc/base/__init__.py index e69de29..bad631d 100644 --- a/oborpc/base/__init__.py +++ b/oborpc/base/__init__.py @@ -0,0 +1,3 @@ +""" +OBORPC Bae +""" \ No newline at end of file diff --git a/oborpc/builder/__init__.py b/oborpc/builder/__init__.py index 9d564c3..c7831e1 100644 --- a/oborpc/builder/__init__.py +++ b/oborpc/builder/__init__.py @@ -1,5 +1,9 @@ +""" +OBORPC Builder +""" + from ._base import OBORBuilder from ._client import ClientBuilder from ._server import ServerBuilder from ._fastapi import FastAPIServerBuilder -from ._flask import FlaskServerBuilder \ No newline at end of file +from ._flask import FlaskServerBuilder diff --git a/oborpc/builder/_fastapi.py b/oborpc/builder/_fastapi.py index 42a40f1..dec0100 100644 --- a/oborpc/builder/_fastapi.py +++ b/oborpc/builder/_fastapi.py @@ -1,4 +1,5 @@ """ +FastAPI Server Builder """ import json import asyncio @@ -51,6 +52,7 @@ def build_router_from_instance( generate_unique_id_function: Callable[[APIRoute], str] = Default(generate_unique_id), ): """ + build FastAPI API Router from oborpc instance """ router = APIRouter( prefix=prefix, diff --git a/oborpc/builder/_flask.py b/oborpc/builder/_flask.py index 4d3d8ff..eaeb735 100644 --- a/oborpc/builder/_flask.py +++ b/oborpc/builder/_flask.py @@ -1,18 +1,28 @@ """ +Flask Server Builder """ import functools import json import os +from typing import Callable from flask import request as flask_request, Blueprint from ._server import ServerBuilder from ..base.meta import OBORBase class FlaskServerBuilder(ServerBuilder): + """ + RPC Server Builder for Flask + """ def __init__(self, host, port=None, timeout=1, retry=None): super().__init__(host, port, timeout, retry) def create_remote_responder( - self, instance: OBORBase, router: Blueprint, class_name, method_name, method + self, + instance: OBORBase, + router: Blueprint, + class_name: str, + method_name: str, + method: Callable ): def create_modified_func(): @functools.wraps(method) @@ -41,6 +51,7 @@ def build_blueprint_from_instance( cli_group: str | None = object() ): """ + build Flask blueprint from oborpc instance """ blueprint = Blueprint( blueprint_name, diff --git a/oborpc/builder/_server.py b/oborpc/builder/_server.py index cf15487..77030a6 100644 --- a/oborpc/builder/_server.py +++ b/oborpc/builder/_server.py @@ -9,15 +9,24 @@ def __init__(self, host, port=None, timeout=1, retry=0) -> None: super().__init__(host, port, timeout, retry) def create_remote_responder(self, instance, router, class_name, method_name, method): + """ + Remote RPC Request Responder + """ raise NotImplementedError("method should be overridden") def dispatch_rpc_request(self, instance, method, body): + """ + Dispatch RPC Request + """ args = body.get("args", []) kwargs = body.get("kwargs", {}) res = method(instance, *args, **kwargs) return {"data": res} def setup_server_rpc(self, instance: object, router): + """ + Setup RPC Server + """ _class = instance.__class__ iterator_class = instance.__class__.__base__ method_map = { diff --git a/oborpc/decorator.py b/oborpc/decorator.py index b17ad2f..9593ad7 100644 --- a/oborpc/decorator.py +++ b/oborpc/decorator.py @@ -1,4 +1,5 @@ """ +OBORPC Decorator """ import inspect From a4385f286baabc23f7cc4afdfda932d7246f5d48 Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Fri, 12 Jan 2024 18:08:05 +0700 Subject: [PATCH 06/13] chore: pylint --- .github/workflows/pylint.yml | 3 +++ oborpc/base/__init__.py | 2 +- oborpc/builder/_base.py | 10 ++++++++++ oborpc/builder/_client.py | 10 +++++++++- oborpc/builder/_fastapi.py | 1 + 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index c9c962e..e7e29e5 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -17,6 +17,9 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip + pip install requests + pip install flask + pip install fastapi pip install pylint - name: Analysing the code with pylint run: | diff --git a/oborpc/base/__init__.py b/oborpc/base/__init__.py index bad631d..234f254 100644 --- a/oborpc/base/__init__.py +++ b/oborpc/base/__init__.py @@ -1,3 +1,3 @@ """ OBORPC Bae -""" \ No newline at end of file +""" diff --git a/oborpc/builder/_base.py b/oborpc/builder/_base.py index c30d9fe..e89fc29 100644 --- a/oborpc/builder/_base.py +++ b/oborpc/builder/_base.py @@ -1,7 +1,11 @@ """ +OBORPC Base Builder """ class OBORBuilder(): + """ + OBORPC Builder Class + """ __registered_base = set() def __init__(self, host, port=None, timeout=1, retry=0) -> None: @@ -20,6 +24,9 @@ def __init__(self, host, port=None, timeout=1, retry=0) -> None: self.base_url += f":{port}" def check_has_protocol(self, host: str): + """ + Check whether the given host already defined with protocol or not + """ if host.startswith("http://"): return True if host.startswith("https://"): @@ -27,6 +34,9 @@ def check_has_protocol(self, host: str): return False def check_registered_base(self, base: str): + """ + Check whether the base RPC class is already built + """ if base in OBORBuilder.__registered_base: raise Exception(f"Failed to build client RPC {base} : base class can only built once") OBORBuilder.__registered_base.add(base) diff --git a/oborpc/builder/_client.py b/oborpc/builder/_client.py index b87c9bc..1a39249 100644 --- a/oborpc/builder/_client.py +++ b/oborpc/builder/_client.py @@ -1,4 +1,5 @@ """ +Client RPC Builder """ import inspect import json @@ -11,7 +12,14 @@ class ClientBuilder(OBORBuilder): def __init__(self, host, port=None, timeout=1, retry=0) -> None: super().__init__(host, port, timeout, retry) - def create_remote_caller(self, class_name, method_name, url_prefix, timeout = None, retry = None): + def create_remote_caller( + self, + class_name: str, + method_name: str, + url_prefix: str, + timeout: float = None, + retry: int = None + ): def remote_call(*args, **kwargs): try: t0 = time.time() diff --git a/oborpc/builder/_fastapi.py b/oborpc/builder/_fastapi.py index dec0100..f01bfed 100644 --- a/oborpc/builder/_fastapi.py +++ b/oborpc/builder/_fastapi.py @@ -11,6 +11,7 @@ from ._server import ServerBuilder from ..base.meta import OBORBase + class FastAPIServerBuilder(ServerBuilder): def __init__(self, host, port=None, timeout=1, retry=None): super().__init__(host, port, timeout, retry) From ef5485ef28b11fe2152a6246680be86934cd8677 Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Fri, 12 Jan 2024 18:13:03 +0700 Subject: [PATCH 07/13] chore: pylint --- oborpc/builder/_base.py | 3 ++- oborpc/builder/_client.py | 7 +++++-- oborpc/exception.py | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 oborpc/exception.py diff --git a/oborpc/builder/_base.py b/oborpc/builder/_base.py index e89fc29..ecb56ff 100644 --- a/oborpc/builder/_base.py +++ b/oborpc/builder/_base.py @@ -1,6 +1,7 @@ """ OBORPC Base Builder """ +from ..exception import OBORPCBuildException class OBORBuilder(): """ @@ -38,5 +39,5 @@ def check_registered_base(self, base: str): Check whether the base RPC class is already built """ if base in OBORBuilder.__registered_base: - raise Exception(f"Failed to build client RPC {base} : base class can only built once") + raise OBORPCBuildException(f"Failed to build client RPC {base} : base class can only built once") OBORBuilder.__registered_base.add(base) diff --git a/oborpc/builder/_client.py b/oborpc/builder/_client.py index 1a39249..c7717b6 100644 --- a/oborpc/builder/_client.py +++ b/oborpc/builder/_client.py @@ -6,6 +6,7 @@ import requests import time from ._base import OBORBuilder +from ..exception import RPCCallException class ClientBuilder(OBORBuilder): @@ -35,13 +36,15 @@ def remote_call(*args, **kwargs): timeout=timeout if timeout != None else self.timeout ) if not response: - raise Exception(f"rpc call failed method={method_name}") + raise RPCCallException(f"rpc call failed method={method_name}") return response.json().get("data") except Exception as e: _retry = retry if retry != None else self.retry if _retry: return remote_call(*args, **kwargs, retry=_retry-1) - raise Exception(f"rpc call failed method={method_name}") + if isinstance(e, RPCCallException): + raise e + raise Exception(f"rpc call failed method={method_name}", e) finally: # print("elapsed", time.time() - t0) pass diff --git a/oborpc/exception.py b/oborpc/exception.py new file mode 100644 index 0000000..b97547d --- /dev/null +++ b/oborpc/exception.py @@ -0,0 +1,17 @@ +""" +OBORPC Exceptions +""" + +class OBORPCBuildException(Exception): + """ + Build Related Exceptions + """ + def __init__(self, *args: object) -> None: + super().__init__(*args) + +class RPCCallException(Exception): + """ + Any Exception during RPC Call + """ + def __init__(self, *args: object) -> None: + super().__init__(*args) From fa12bdd2a8b9cd5f69605cc756a7c750a830b7ec Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Fri, 12 Jan 2024 18:14:57 +0700 Subject: [PATCH 08/13] chore: pylint --- oborpc/builder/_base.py | 3 ++- oborpc/builder/_client.py | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/oborpc/builder/_base.py b/oborpc/builder/_base.py index ecb56ff..bc8b8c7 100644 --- a/oborpc/builder/_base.py +++ b/oborpc/builder/_base.py @@ -39,5 +39,6 @@ def check_registered_base(self, base: str): Check whether the base RPC class is already built """ if base in OBORBuilder.__registered_base: - raise OBORPCBuildException(f"Failed to build client RPC {base} : base class can only built once") + msg = f"Failed to build client RPC {base} : base class can only built once" + raise OBORPCBuildException(msg) OBORBuilder.__registered_base.add(base) diff --git a/oborpc/builder/_client.py b/oborpc/builder/_client.py index c7717b6..fee6048 100644 --- a/oborpc/builder/_client.py +++ b/oborpc/builder/_client.py @@ -10,6 +10,9 @@ class ClientBuilder(OBORBuilder): + """ + Client Builder + """ def __init__(self, host, port=None, timeout=1, retry=0) -> None: super().__init__(host, port, timeout, retry) @@ -21,6 +24,8 @@ def create_remote_caller( timeout: float = None, retry: int = None ): + """ + """ def remote_call(*args, **kwargs): try: t0 = time.time() From fd934eed2bfbf642e407da393a5b05a975d23d76 Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Sat, 13 Jan 2024 06:14:04 +0700 Subject: [PATCH 09/13] chore: pylint --- oborpc/builder/_fastapi.py | 6 +++--- oborpc/builder/_flask.py | 23 ++++++++++------------- oborpc/builder/_server.py | 14 +++++++++----- oborpc/decorator.py | 20 +++++++++++++++++++- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/oborpc/builder/_fastapi.py b/oborpc/builder/_fastapi.py index f01bfed..3f063d7 100644 --- a/oborpc/builder/_fastapi.py +++ b/oborpc/builder/_fastapi.py @@ -13,9 +13,9 @@ class FastAPIServerBuilder(ServerBuilder): - def __init__(self, host, port=None, timeout=1, retry=None): - super().__init__(host, port, timeout, retry) - + """ + Dedicated RPC Server Builder for FastAPI + """ def create_remote_responder( self, instance: OBORBase, diff --git a/oborpc/builder/_flask.py b/oborpc/builder/_flask.py index eaeb735..597d7dc 100644 --- a/oborpc/builder/_flask.py +++ b/oborpc/builder/_flask.py @@ -4,18 +4,15 @@ import functools import json import os -from typing import Callable +from typing import Callable, Union from flask import request as flask_request, Blueprint from ._server import ServerBuilder from ..base.meta import OBORBase class FlaskServerBuilder(ServerBuilder): """ - RPC Server Builder for Flask + Dedicated RPC Server Builder for Flask """ - def __init__(self, host, port=None, timeout=1, retry=None): - super().__init__(host, port, timeout, retry) - def create_remote_responder( self, instance: OBORBase, @@ -41,14 +38,14 @@ def build_blueprint_from_instance( instance: OBORBase, blueprint_name: str, import_name: str, - static_folder: str | os.PathLike | None = None, - static_url_path: str | None = None, - template_folder: str | os.PathLike | None = None, - url_prefix: str | None = None, - subdomain: str | None = None, - url_defaults: dict | None = None, - root_path: str | None = None, - cli_group: str | None = object() + static_folder: Union[str, os.PathLike, None] = None, + static_url_path: Union[str, None] = None, + template_folder: Union[str, os.PathLike, None] = None, + url_prefix: Union[str, None] = None, + subdomain: Union[str, None] = None, + url_defaults: Union[dict, None] = None, + root_path: Union[str, None] = None, + cli_group: Union[str, None] = object() ): """ build Flask blueprint from oborpc instance diff --git a/oborpc/builder/_server.py b/oborpc/builder/_server.py index 77030a6..1c0522f 100644 --- a/oborpc/builder/_server.py +++ b/oborpc/builder/_server.py @@ -1,14 +1,15 @@ """ +Server Builder Base """ import inspect from ._base import OBORBuilder class ServerBuilder(OBORBuilder): - def __init__(self, host, port=None, timeout=1, retry=0) -> None: - super().__init__(host, port, timeout, retry) - - def create_remote_responder(self, instance, router, class_name, method_name, method): + """ + Server Builder + """ + def create_remote_responder(self, instance, router, class_name, method_name, method): # pylint: disable=too-many-arguments """ Remote RPC Request Responder """ @@ -38,4 +39,7 @@ def setup_server_rpc(self, instance: object, router): for (name, method) in inspect.getmembers(iterator_class, predicate=inspect.isfunction): if name not in iterator_class.__oborprocedures__: continue - self.create_remote_responder(instance, router, iterator_class.__name__, name, method_map.get(name)) + self.create_remote_responder( + instance, router, iterator_class.__name__, + name, method_map.get(name) + ) diff --git a/oborpc/decorator.py b/oborpc/decorator.py index 9593ad7..af110be 100644 --- a/oborpc/decorator.py +++ b/oborpc/decorator.py @@ -2,8 +2,26 @@ OBORPC Decorator """ import inspect +from typing import Callable -def procedure(fun): +def procedure(fun: Callable): + """ + Marks the method with this decorator to make it available for RPC within the class with + direct inheritance with `OBORBase`. + To make it works also make sure the method is overridden in the server class inheritance of the base class + + from oborpc.base import meta + from oborpc.decorator import procedure + + class Calculator(meta.OborBase): + @procedure + def add(self, a, b): + pass + + class CalculatorServer(Calculator): + def tambah(self, a, b): + return a + b + """ if not inspect.isfunction(fun): raise TypeError("can only applied for function or method") fun.__isoborprocedure__ = True From 47a469a897a377366edcdd5160e476049ce436a4 Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Sat, 13 Jan 2024 06:26:50 +0700 Subject: [PATCH 10/13] chore: pylint --- oborpc/builder/_client.py | 35 ++++++++++++++++++++++------------- oborpc/builder/_fastapi.py | 2 +- oborpc/builder/_flask.py | 4 ++-- oborpc/decorator.py | 7 ++++--- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/oborpc/builder/_client.py b/oborpc/builder/_client.py index fee6048..e9da303 100644 --- a/oborpc/builder/_client.py +++ b/oborpc/builder/_client.py @@ -3,8 +3,9 @@ """ import inspect import json -import requests +import logging import time +import requests from ._base import OBORBuilder from ..exception import RPCCallException @@ -13,9 +14,6 @@ class ClientBuilder(OBORBuilder): """ Client Builder """ - def __init__(self, host, port=None, timeout=1, retry=0) -> None: - super().__init__(host, port, timeout, retry) - def create_remote_caller( self, class_name: str, @@ -23,12 +21,16 @@ def create_remote_caller( url_prefix: str, timeout: float = None, retry: int = None - ): + ): # pylint: disable=too-many-arguments """ + create remote caller """ def remote_call(*args, **kwargs): + """ + remote call wrapper + """ try: - t0 = time.time() + start_time = time.time() data = { "args": args[1:], "kwargs": kwargs @@ -38,21 +40,28 @@ def remote_call(*args, **kwargs): url, headers={"Content-Type": "application/json"}, json=json.dumps(data), - timeout=timeout if timeout != None else self.timeout + timeout=timeout if timeout is not None else self.timeout ) + if not response: - raise RPCCallException(f"rpc call failed method={method_name}") + msg = f"rpc call failed method={method_name}" + raise RPCCallException(msg) + return response.json().get("data") + except Exception as e: - _retry = retry if retry != None else self.retry + _retry = retry if retry is not None else self.retry if _retry: return remote_call(*args, **kwargs, retry=_retry-1) + if isinstance(e, RPCCallException): raise e - raise Exception(f"rpc call failed method={method_name}", e) + msg = f"rpc call failed method={method_name} : {e}" + raise RPCCallException(msg) + finally: - # print("elapsed", time.time() - t0) - pass + logging.debug(f"[RPC-Clientt] remote call take {(time.time() - start_time) * 1000:.2f} ms") + return remote_call def setup_client_rpc(self, instance: object, url_prefix: str = ""): @@ -61,7 +70,7 @@ def setup_client_rpc(self, instance: object, url_prefix: str = ""): self.check_registered_base(_class) - for (name, method) in inspect.getmembers(iterator_class, predicate=inspect.isfunction): + for (name, _) in inspect.getmembers(iterator_class, predicate=inspect.isfunction): if name not in iterator_class.__oborprocedures__: continue setattr(_class, name, self.create_remote_caller(_class.__name__, name, url_prefix)) diff --git a/oborpc/builder/_fastapi.py b/oborpc/builder/_fastapi.py index 3f063d7..ae78ff2 100644 --- a/oborpc/builder/_fastapi.py +++ b/oborpc/builder/_fastapi.py @@ -51,7 +51,7 @@ def build_router_from_instance( deprecated: Optional[bool] = None, include_in_schema: bool = True, generate_unique_id_function: Callable[[APIRoute], str] = Default(generate_unique_id), - ): + ): # pylint: disable=too-many-arguments """ build FastAPI API Router from oborpc instance """ diff --git a/oborpc/builder/_flask.py b/oborpc/builder/_flask.py index 597d7dc..fe7d948 100644 --- a/oborpc/builder/_flask.py +++ b/oborpc/builder/_flask.py @@ -20,7 +20,7 @@ def create_remote_responder( class_name: str, method_name: str, method: Callable - ): + ): # pylint: disable=too-many-arguments def create_modified_func(): @functools.wraps(method) def modified_func(): @@ -46,7 +46,7 @@ def build_blueprint_from_instance( url_defaults: Union[dict, None] = None, root_path: Union[str, None] = None, cli_group: Union[str, None] = object() - ): + ): # pylint: disable=too-many-arguments """ build Flask blueprint from oborpc instance """ diff --git a/oborpc/decorator.py b/oborpc/decorator.py index af110be..30a4c07 100644 --- a/oborpc/decorator.py +++ b/oborpc/decorator.py @@ -6,9 +6,10 @@ def procedure(fun: Callable): """ - Marks the method with this decorator to make it available for RPC within the class with - direct inheritance with `OBORBase`. - To make it works also make sure the method is overridden in the server class inheritance of the base class + Marks the method with this decorator to make it available + for RPC within the class with direct inheritance with `OBORBase`. + To make it works also make sure the method is overridden + in the server class inheritance of the base class from oborpc.base import meta from oborpc.decorator import procedure From f80797d8732507bdea1e70ba2d007307368ac4a9 Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Sat, 13 Jan 2024 06:33:02 +0700 Subject: [PATCH 11/13] chore: pylint --- oborpc/builder/_client.py | 10 +++++++--- oborpc/builder/_fastapi.py | 4 ++-- oborpc/builder/_server.py | 2 +- setup.py | 3 +++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/oborpc/builder/_client.py b/oborpc/builder/_client.py index e9da303..e0c580f 100644 --- a/oborpc/builder/_client.py +++ b/oborpc/builder/_client.py @@ -57,14 +57,18 @@ def remote_call(*args, **kwargs): if isinstance(e, RPCCallException): raise e msg = f"rpc call failed method={method_name} : {e}" - raise RPCCallException(msg) + raise RPCCallException(msg) from e finally: - logging.debug(f"[RPC-Clientt] remote call take {(time.time() - start_time) * 1000:.2f} ms") + elapsed = f"{(time.time() - start_time) * 1000}:.2f" + logging.debug("[RPC-Clientt] remote call take %s ms" % elapsed) return remote_call - def setup_client_rpc(self, instance: object, url_prefix: str = ""): + def build_client_rpc(self, instance: object, url_prefix: str = ""): + """ + Setup client rpc + """ _class = instance.__class__ iterator_class = _class diff --git a/oborpc/builder/_fastapi.py b/oborpc/builder/_fastapi.py index ae78ff2..97eae6b 100644 --- a/oborpc/builder/_fastapi.py +++ b/oborpc/builder/_fastapi.py @@ -4,10 +4,10 @@ import json import asyncio from enum import Enum +from typing import Optional, List, Dict, Union, Type, Any, Sequence, Callable from fastapi import Request, Response, APIRouter, params from fastapi.responses import JSONResponse from fastapi.routing import BaseRoute, APIRoute, ASGIApp, Lifespan, Default, generate_unique_id -from typing import Optional, List, Dict, Union, Type, Any, Sequence, Callable from ._server import ServerBuilder from ..base.meta import OBORBase @@ -30,7 +30,7 @@ def final_func(request: Request): body = json.loads(json.loads(request_body.decode())) return self.dispatch_rpc_request(instance, method, body) - def build_router_from_instance( + def build_router_from_instance( # pylint: disable=too-many-locals self, instance: OBORBase, *, diff --git a/oborpc/builder/_server.py b/oborpc/builder/_server.py index 1c0522f..3a3c304 100644 --- a/oborpc/builder/_server.py +++ b/oborpc/builder/_server.py @@ -30,7 +30,7 @@ def setup_server_rpc(self, instance: object, router): """ _class = instance.__class__ iterator_class = instance.__class__.__base__ - method_map = { + method_map = { # pylint: disable=unnecessary-comprehension name: method for (name, method) in inspect.getmembers( _class, predicate=inspect.isfunction ) diff --git a/setup.py b/setup.py index 029ba89..7896b92 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,6 @@ +""" +Setup script +""" from pathlib import Path from setuptools import setup, find_packages From 4f9b256440dd4a440191f96e8d4763a87cb4e7b5 Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Sat, 13 Jan 2024 06:35:25 +0700 Subject: [PATCH 12/13] chore: pylint --- oborpc/builder/_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/oborpc/builder/_client.py b/oborpc/builder/_client.py index e0c580f..58b625d 100644 --- a/oborpc/builder/_client.py +++ b/oborpc/builder/_client.py @@ -29,8 +29,8 @@ def remote_call(*args, **kwargs): """ remote call wrapper """ + start_time = time.time() try: - start_time = time.time() data = { "args": args[1:], "kwargs": kwargs @@ -61,7 +61,7 @@ def remote_call(*args, **kwargs): finally: elapsed = f"{(time.time() - start_time) * 1000}:.2f" - logging.debug("[RPC-Clientt] remote call take %s ms" % elapsed) + logging.debug("[RPC-Clientt] remote call take %s ms", elapsed) return remote_call From 8f182626304ef2b59410f6bbaea3b578b864fcba Mon Sep 17 00:00:00 2001 From: danjoyboy Date: Sat, 13 Jan 2024 06:36:55 +0700 Subject: [PATCH 13/13] chore: pylint --- oborpc/builder/_fastapi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oborpc/builder/_fastapi.py b/oborpc/builder/_fastapi.py index 97eae6b..85870b3 100644 --- a/oborpc/builder/_fastapi.py +++ b/oborpc/builder/_fastapi.py @@ -23,14 +23,14 @@ def create_remote_responder( class_name: str, method_name: str, method: Callable - ): + ): # pylint: disable=too-many-arguments @router.post(f"{router.prefix}/{class_name}/{method_name}") def final_func(request: Request): request_body = asyncio.run(request.body()) body = json.loads(json.loads(request_body.decode())) return self.dispatch_rpc_request(instance, method, body) - def build_router_from_instance( # pylint: disable=too-many-locals + def build_router_from_instance( self, instance: OBORBase, *, @@ -51,7 +51,7 @@ def build_router_from_instance( # pylint: disable=too-many-locals deprecated: Optional[bool] = None, include_in_schema: bool = True, generate_unique_id_function: Callable[[APIRoute], str] = Default(generate_unique_id), - ): # pylint: disable=too-many-arguments + ): # pylint: disable=too-many-arguments,too-many-locals """ build FastAPI API Router from oborpc instance """