Skip to content

Commit

Permalink
Use keyword args instead of a dict (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moosems authored Sep 6, 2024
1 parent ca68dde commit a252db8
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 36 deletions.
8 changes: 2 additions & 6 deletions collegamento/client_server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,9 @@ def create_message_id(self) -> int:

return id

def request(
self,
request_details: dict,
) -> None:
def request(self, command: str, **kwargs) -> None:
"""Sends the main process a request of type command with given kwargs - external API"""

command = request_details["command"]
if command not in self.commands:
raise CollegamentoError(
f"Command {command} not in builtin commands. Those are {self.commands}!"
Expand All @@ -129,7 +125,7 @@ def request(
"type": "request",
"command": command,
}
final_request.update(request_details)
final_request.update(**kwargs)

if self.commands[command][1]:
self.current_ids[id] = command
Expand Down
15 changes: 6 additions & 9 deletions collegamento/files_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,15 @@ def create_server(self) -> None:
for file, data in files_copy.items():
self.update_file(file, data)

def request(
self,
request_details: dict,
) -> None:
if "file" in request_details:
file = request_details["file"]
def request(self, command: str, **kwargs) -> None:
if "file" in kwargs:
file = kwargs["file"]
if file not in self.files:
raise CollegamentoError(
f"File {file} not in files! Files are {self.files.keys()}"
)

super().request(request_details)
super().request(command, **kwargs)

def update_file(self, file: str, current_state: str) -> None:
"""Updates files in the system - external API"""
Expand All @@ -77,7 +74,7 @@ def update_file(self, file: str, current_state: str) -> None:
"contents": self.files[file],
}

super().request(file_notification)
super().request(**file_notification)

def remove_file(self, file: str) -> None:
"""Removes a file from the main_server - external API"""
Expand All @@ -92,7 +89,7 @@ def remove_file(self, file: str) -> None:
"remove": True,
}

super().request(file_notification)
super().request(**file_notification)


class FileServer(Server):
Expand Down
4 changes: 3 additions & 1 deletion docs/source/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The ``Response`` class is what is returned by the "ref:`Client Overview` or one

The ``Client`` class can do:

- ``Client.request(request_details: dict)`` (all details in request_details are specific to the command in the request_details)
- ``Client.request(command: str, **kwargs)``
- ``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)
Expand All @@ -44,6 +44,8 @@ By default ``Collegamento`` assumes you only want the newest request but chooses

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.

When it comes to requesting the server to run a command, you give the command as the first argument and all subsequent args for the function the ``Server`` calls are given as kwargs that are passed on.

.. _Server Overview:

``Server``
Expand Down
2 changes: 1 addition & 1 deletion docs/source/example-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Now that you have ``Collegamento`` installed, let's try running a simple example
# like so: {"test": (foo, True)} (using (foo, False)) is the default (only newest request)
context = Client({"test": foo})
context.request({"command": "test"})
context.request("test")
sleep(1)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/class_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Class Example
self.context.update_file("user_file", new_contents)
def request_split(self) -> None:
self.context.request({"command": "MyClientFunc", "file": "user_file"})
self.context.request("MyClientFunc", file="user_file")
def check_split(self) -> list[str] | None:
output = self.context.get_response("MyClientFunc")
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/file_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ File Example
context.update_file("test", "test contents")
sleep(1)
context.request({"command": "test", "file": "test"})
context.request("test", file="test")
sleep(1)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/simple_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Simple Example
# like so: {"test": (foo, True)} (using (foo, False)) is the default (only newest request)
context = Client({"test": foo})
context.request({"command": "test"})
context.request("test")
sleep(1)
Expand Down
2 changes: 1 addition & 1 deletion examples/class_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def change_file(self, new_contents: str) -> None:
self.context.update_file("user_file", new_contents)

def request_split(self) -> None:
self.context.request({"command": "MyClientFunc", "file": "user_file"})
self.context.request("MyClientFunc", file="user_file")

def check_split(self) -> list[str] | None:
output = self.context.get_response("MyClientFunc")
Expand Down
2 changes: 1 addition & 1 deletion examples/file_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main():

context.update_file("test", "test contents")
sleep(1)
context.request({"command": "test", "file": "test"})
context.request("test", file="test")

sleep(1)

Expand Down
2 changes: 1 addition & 1 deletion examples/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def main():
# like so: {"test": (foo, True)} (using (foo, False)) is the default (only newest request)
context = Client({"test": foo})

context.request({"command": "test"})
context.request("test")

sleep(1)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_file_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_file_variants():

context.update_file("test", "test contents")
context.update_file("test2", "test contents2")
context.request({"command": "test"})
context.request("test")

sleep(1)

Expand All @@ -26,8 +26,8 @@ def test_file_variants():
assert output["result"] is True # noqa: E712 # type: ignore

context.add_command("test1", split_str, True)
context.request({"command": "test1", "file": "test"})
context.request({"command": "test1", "file": "test2"})
context.request("test1", file="test")
context.request("test1", file="test2")

sleep(1)

Expand Down
18 changes: 8 additions & 10 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ def test_normal_client():
Client({"foo": foo})
x = Client({"foo": (foo, True), "foo2": foo})

x.request({"command": "foo"})
x.request({"command": "foo"})
x.request({"command": "foo2"})
x.request(
{"command": "foo2"}
) # If you see six "Foo called"'s, thats bad news bears
x.request("foo")
x.request("foo")
x.request("foo2")
x.request("foo2") # If you see six "Foo called"'s, thats bad news bears
x.add_command("foo3", foo)
x.request({"command": "foo3"})
x.request("foo3")
x.add_command("foo4", foo, True)
x.request({"command": "foo4"})
x.request({"command": "foo4"})
x.request("foo4")
x.request("foo4")

sleep(1)

Expand All @@ -44,7 +42,7 @@ def test_normal_client():
assert x.all_ids == []

Client()
Client({"foo": foo}).request({"command": "foo"})
Client({"foo": foo}).request("foo")
Client().kill_IPC()
Client().create_server()

Expand Down

0 comments on commit a252db8

Please sign in to comment.