From 03b427270b4d551988b0f65ac80c3b5b9a4c07ad Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Mon, 29 Jul 2024 01:29:38 -0600 Subject: [PATCH] Custom error class and continue docs --- collegamento/__init__.py | 1 + collegamento/files_variant.py | 3 ++- collegamento/simple_client_server/__init__.py | 1 + collegamento/simple_client_server/client.py | 23 ++++++++++--------- collegamento/simple_client_server/misc.py | 3 +++ docs/source/classes.rst | 20 +++++++++------- tests/test_file_variant.py | 2 +- tests/test_simple.py | 2 +- 8 files changed, 33 insertions(+), 22 deletions(-) diff --git a/collegamento/__init__.py b/collegamento/__init__.py index feb88cd..609381e 100644 --- a/collegamento/__init__.py +++ b/collegamento/__init__.py @@ -10,6 +10,7 @@ ) from .simple_client_server import ( # noqa: F401, E402 USER_FUNCTION, + CollegamentoError, Notification, Request, Response, diff --git a/collegamento/files_variant.py b/collegamento/files_variant.py index 08c37de..0a869d2 100644 --- a/collegamento/files_variant.py +++ b/collegamento/files_variant.py @@ -4,6 +4,7 @@ from .simple_client_server import ( USER_FUNCTION, + CollegamentoError, Notification, Request, SimpleClient, @@ -69,7 +70,7 @@ def remove_file(self, file: str) -> None: self.logger.exception( f"Cannot remove file {file} as file is not in file database!" ) - raise Exception( + raise CollegamentoError( f"Cannot remove file {file} as file is not in file database!" ) diff --git a/collegamento/simple_client_server/__init__.py b/collegamento/simple_client_server/__init__.py index ca06316..1ae4f93 100644 --- a/collegamento/simple_client_server/__init__.py +++ b/collegamento/simple_client_server/__init__.py @@ -1,6 +1,7 @@ from .client import SimpleClient # noqa: F401, E402 from .misc import ( # noqa: F401, E402 USER_FUNCTION, + CollegamentoError, Notification, Request, RequestQueueType, diff --git a/collegamento/simple_client_server/client.py b/collegamento/simple_client_server/client.py index aa7dcbb..a457705 100644 --- a/collegamento/simple_client_server/client.py +++ b/collegamento/simple_client_server/client.py @@ -4,6 +4,7 @@ from .misc import ( USER_FUNCTION, + CollegamentoError, Notification, Request, RequestQueueType, @@ -15,11 +16,11 @@ class SimpleClient: """The IPC class is used to talk to the server and run commands. The public API includes the following methods: - - Client.notify_server() - - Client.request() - - Client.add_command() - - Client.cancel_request() - - Client.kill_IPC() + - SimpleClient.notify_server() + - SimpleClient.request() + - SimpleClient.add_command() + - SimpleClient.cancel_request() + - SimpleClient.kill_IPC() """ def __init__( @@ -85,7 +86,7 @@ def create_message_id(self) -> int: def notify_server( self, - notification_dict: Notification, + notification_dict: dict, ) -> None: self.logger.info("Creating notification for server") @@ -112,7 +113,7 @@ def request( self.logger.exception( f"Command {command} not in builtin commands. Those are {self.commands}!" ) - raise Exception( + raise CollegamentoError( f"Command {command} not in builtin commands. Those are {self.commands}!" ) @@ -138,7 +139,7 @@ def cancel_request(self, command: str) -> None: self.logger.exception( f"Cannot cancel command {command}, valid commands are {self.commands}" ) - raise Exception( + raise CollegamentoError( f"Cannot cancel command {command}, valid commands are {self.commands}" ) @@ -181,7 +182,7 @@ def get_response(self, command: str) -> Response | None: self.logger.exception( f"Cannot get response of command {command}, valid commands are {self.commands}" ) - raise Exception( + raise CollegamentoError( f"Cannot get response of command {command}, valid commands are {self.commands}" ) @@ -191,12 +192,12 @@ def get_response(self, command: str) -> Response | None: self.logger.info("Response retrieved") return response - def add_commmand(self, name: str, command: USER_FUNCTION) -> None: + def add_command(self, name: str, command: USER_FUNCTION) -> None: if name == "add-command": self.logger.exception( "Cannot add command add-command as it is a special builtin" ) - raise Exception( + raise CollegamentoError( "Cannot add command add-command as it is a special builtin" ) diff --git a/collegamento/simple_client_server/misc.py b/collegamento/simple_client_server/misc.py index b734b13..8b7a86b 100644 --- a/collegamento/simple_client_server/misc.py +++ b/collegamento/simple_client_server/misc.py @@ -41,3 +41,6 @@ class Response(Message): else: ResponseQueueType = GenericQueueClass RequestQueueType = GenericQueueClass + + +class CollegamentoError(Exception): ... # I don't like the boilerplate either diff --git a/docs/source/classes.rst b/docs/source/classes.rst index 4cf0b43..a387389 100644 --- a/docs/source/classes.rst +++ b/docs/source/classes.rst @@ -2,26 +2,26 @@ Classes ======= -.. _Notification Overview: +.. _CollegamentoError Overview: -``Notification`` -**************** +``CollegamentoError`` +********************* -The ``Notification`` class is for xyz. +The ``CollegamentoError`` class is a simple error class for ``Collegamento``. .. _Request Overview: ``Request`` *********** -The ``Request`` class is for xyz. +The ``Request`` class is a TypedDict meant to provide a framework for items given to functions used by the IPC. It *will* almsot always contain extra items regardless of the fact that that's not supposed to happen (just add ``# type: ignore`` to the end of the line to shut up the langserver). The data provided will not be typed checked to make sure its proper. The responsibility of data rests on the user. .. _Response Overview: ``Response`` ************ -The ``Response`` class is for xyz. +The ``Response`` class is what is returned by the ``SimpleClient`` or one of it's variants to the user. The useful data is found at ``some_response["result"]``. .. _SimpleClient Overview: @@ -30,8 +30,12 @@ The ``Response`` class is for xyz. The ``SimpleClient`` class can do: -- ``SimpleClient.xyz`` -- ``SimpleClient.yippee`` +- ``SimpleClient.notify_server(notification_dict: dict)`` (as a base class, this has no use case, but it will likely be used by any given subclass) +- ``SimpleClient.request(request_details: dict)`` (all details in request_details are specific to the command in the request_details) +- ``SimpleClient.add_command(name: str, command: USER_FUNCTION)`` (adds the function with the name provided that takes input of ``Request`` and returns anything`` +- ``SimpleClient.cancel_request(command: str)`` (will not give output for any requests of type command. When is this ever used? No idea! Why? Because it doesn't actually stop the server from completing it) +TODO: remove this stupid feature. No point in it. +- ``SimpleClient.kill_IPC()`` (kills the IPC server) .. _FileClient Overview: diff --git a/tests/test_file_variant.py b/tests/test_file_variant.py index ca2174c..025890e 100644 --- a/tests/test_file_variant.py +++ b/tests/test_file_variant.py @@ -25,7 +25,7 @@ def test_file_variants(): assert output is not None # noqa: E711 assert output["result"] is True # noqa: E712 # type: ignore - context.add_commmand("test1", split_str) + context.add_command("test1", split_str) context.request({"command": "test1", "file": "test"}) sleep(1) diff --git a/tests/test_simple.py b/tests/test_simple.py index d943df3..b4c623b 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -17,7 +17,7 @@ def test_Client_Server(): sleep(1) - context.add_commmand("test1", foo) + context.add_command("test1", foo) sleep(1)