Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Moosems committed Sep 6, 2024
1 parent b8475d4 commit acfab49
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
3 changes: 2 additions & 1 deletion collegamento/client_server/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# TODO: make sure everything is type hinted while removing redundancy
"""Defines the Client and Server class which provides a convenient and easy to use IPC interface.
>>> def test(*args):
Expand Down Expand Up @@ -31,7 +32,7 @@ class Client:
The public API includes the following methods:
- Client.add_command(name: str, command: USER_FUNCTION, multiple_requests: bool = False)
- Client.request(request_details: dict) -> None | int
- Client.get_response(command: str | int) -> Response | list[Response]
- Client.get_response(command: str) -> Response | list[Response] | None
- Client.kill_IPC()
"""

Expand Down
34 changes: 22 additions & 12 deletions docs/source/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,35 @@ The ``Request`` class is a TypedDict meant to provide a framework for items give
``Response``
************

The ``Response`` class is what is returned by the "ref:`SimpleClient Overview` or one of it's variants to the user. The useful data is found at ``some_response["result"]``.
The ``Response`` class is what is returned by the "ref:`Client Overview` or one of it's variants to the user. The useful data is found at ``some_response["result"]``.

.. _SimpleClient Overview:
.. _Client Overview:

``SimpleClient``
``Client``
****************

The ``SimpleClient`` class can do:
The ``Client`` class can do:

- ``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 :ref:`Request Overview` and returns anything``
- ``SimpleClient.kill_IPC()`` (kills the IPC server)
- ``Client.request(request_details: dict)`` (all details in request_details are specific to the command in the request_details)
- ``Client.add_command(name: str, command: USER_FUNCTION, multiple_requests: bool = False)`` (adds the function with the name provided that takes input of :ref:`Request Overview` and returns anything)
- ``Client.get_response(command: str) -> Response | list[Response] | None`` (returns a list of ``Response``'s if the command allows multiple requests otherwise a single ``Response`` if there is were any responses ohterwise ``None``)
- ``Client.kill_IPC()`` (kills the IPC server)

.. _SimpleServer Overview:
When using the ``Client`` class you give the commands as a dict. Below are the ways it can be specified:
- ``{"foo": foo}`` (this means the command foo can only take the newest request given
- ``{"foo": (foo, False)}`` (this means the command foo can only take the newest request given
- ``{"foo": (foo, True)}`` (this means the command foo can take all requests given (new or old)

``SimpleServer``
By default ``Collegamento`` assumes you only want the newest request but chooses to still give the option to make multiple requests. For ``.get_response()`` the output changes based on how this was specified by giving ``None`` if there was no response, ``Response`` if the command only allows the newest request, and ``list[Response]`` if it allows multiple regardless of how many times you made a request for it.

Note that because of the way that the commands are handed to the ``Server`` and run, they can actually modify its attributes and theoretically even the functions the ``Server`` runs. This high flexibility also requires the user to ensure that they properly manage any attributes they mess with.

.. _Server Overview:

``Server``
****************

The SimpleServer is a backend piece of code made visible for commands that can be given to a ``SimpleClient``. If you want to know more about it, check out the source code ;).
The ``Server`` is a backend piece of code made visible for commands that can be given to a ``Client``. If you want to know more about it, check out the source code ;). The reason it is mentioned is because it is given as an argument to :ref:`USER_FUNCTION Overview`'s and we choose to let type declarations exist.

.. _FileClient Overview:

Expand All @@ -51,11 +61,11 @@ The SimpleServer is a backend piece of code made visible for commands that can b
- ``FileClient.update_file(file: str, current_state: str)`` (adds or updates the file with the new contents and notifies server of changes)
- ``FileClient.remove_file(file: str)`` (removes the file specified from the system and notifies the server to fo the same)

This class also has some changed functionality. When you make a ``.request()`` and add a file to the request, it chnages the request's name to its contents for the function to use.
This class also has some changed functionality. When you make a ``.request()`` and add a file to the request, it changes the request's file name to its contents for the function to use. This isn't technically necessary as the function called can access the files in the Server and modify them as it pleases since it has full access to all the Server's resources.

.. _FileServer Overview:

``FileServer``
**************

The ``FileServer`` is a backend piece of code made visible for commands that can be given to a ``FileClient``. If you want to know more about it, check out the source code ;).
The ``FileServer`` is a backend piece of code made visible for commands that can be given to a ``FileClient``. See my explanation on :ref:`Server Overview`
15 changes: 8 additions & 7 deletions docs/source/example-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@ Now that you have ``Collegamento`` installed, let's try running a simple example
from time import sleep
from collegamento import USER_FUNCTION, Request, Response, SimpleClient, SimpleServer
from collegamento import Client, Request, Response, Server
def foo(server: "SimpleServer", bar: Request) -> bool:
def foo(server: "Server", bar: Request) -> bool:
if bar["command"] == "test":
return True
return False
def main():
commands: dict[str, USER_FUNCTION] = {"test": foo}
context = SimpleClient(commands)
# As of collegamento v0.3.0 you can allow multiple requests for the same command
# like so: {"test": (foo, True)} (using (foo, False)) is the default (only newest request)
context = Client({"test": foo})
context.request({"command": "test"})
sleep(1)
output: Response | None = context.get_response("test")
if output is not None and output["result"]: # type: ignore
output: Response = context.get_response("test") # type: ignore
if output and output[0]["result"]: # type: ignore
print("Yippee! It worked!")
else:
print("Aww, maybe your compute is just a little slow?")
print("Aww, maybe your computer is just a little slow?")
context.kill_IPC()
Expand Down
2 changes: 1 addition & 1 deletion docs/source/variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Variables
``USER_FUNCTION``
*****************

``USER_FUNCTION`` is a type variable that simply states that any function that matches this type takes in a :ref:`SimpleServer Overview` and :ref:`Request Overview` class (positionally) and returns anything or even nothing.
``USER_FUNCTION`` is a type variable that simply states that any function that matches this type takes in a :ref:`Server Overview` and :ref:`Request Overview` class (positionally) and can return anything it pleases (though this will never be used).

0 comments on commit acfab49

Please sign in to comment.