Skip to content

Commit

Permalink
Custom error class and continue docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Moosems committed Jul 29, 2024
1 parent 1ae4972 commit 03b4272
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions collegamento/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from .simple_client_server import ( # noqa: F401, E402
USER_FUNCTION,
CollegamentoError,
Notification,
Request,
Response,
Expand Down
3 changes: 2 additions & 1 deletion collegamento/files_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .simple_client_server import (
USER_FUNCTION,
CollegamentoError,
Notification,
Request,
SimpleClient,
Expand Down Expand Up @@ -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!"
)

Expand Down
1 change: 1 addition & 0 deletions collegamento/simple_client_server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .client import SimpleClient # noqa: F401, E402
from .misc import ( # noqa: F401, E402
USER_FUNCTION,
CollegamentoError,
Notification,
Request,
RequestQueueType,
Expand Down
23 changes: 12 additions & 11 deletions collegamento/simple_client_server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .misc import (
USER_FUNCTION,
CollegamentoError,
Notification,
Request,
RequestQueueType,
Expand All @@ -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__(
Expand Down Expand Up @@ -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")

Expand All @@ -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}!"
)

Expand All @@ -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}"
)

Expand Down Expand Up @@ -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}"
)

Expand All @@ -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"
)

Expand Down
3 changes: 3 additions & 0 deletions collegamento/simple_client_server/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ class Response(Message):
else:
ResponseQueueType = GenericQueueClass
RequestQueueType = GenericQueueClass


class CollegamentoError(Exception): ... # I don't like the boilerplate either
20 changes: 12 additions & 8 deletions docs/source/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion tests/test_file_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_Client_Server():

sleep(1)

context.add_commmand("test1", foo)
context.add_command("test1", foo)

sleep(1)

Expand Down

0 comments on commit 03b4272

Please sign in to comment.