-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Still need to: - finsh docs - add examples - refactor and prettify - Update readme to remove bottom text
- Loading branch information
Showing
19 changed files
with
673 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Upload PROJECT_NAME to Pypi | ||
name: Upload collegamento to Pypi | ||
|
||
on: | ||
release: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ dist/ | |
build/ | ||
.ruff_cache/ | ||
.pytest_cache/ | ||
PROJECT_NAME.egg-info/ | ||
collegamento.egg-info/ | ||
|
||
# Pycharm | ||
.idea |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from beartype.claw import beartype_this_package | ||
|
||
beartype_this_package() | ||
|
||
from .files_variant import ( # noqa: F401, E402 | ||
FileClient, | ||
FileNotification, | ||
FileRequest, | ||
FileServer, | ||
) | ||
|
||
# xyz module level imports go here | ||
from .ipc import USER_FUNCTION # noqa: F401, E402 | ||
from .ipc import Client as SimpleClient # noqa: F401, E402 | ||
from .ipc import Notification, Request, Response # noqa: F401, E402 | ||
from .ipc import Server as SimpleServer # noqa: F401, E402 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
from logging import Logger | ||
from multiprocessing.queues import Queue as GenericQueueClass | ||
from typing import NotRequired | ||
|
||
from .ipc import USER_FUNCTION, Client, Notification, Request, Server | ||
|
||
|
||
class FileRequest(Request): | ||
# There may be commands that don't require a file but some might | ||
file: NotRequired[str] | ||
|
||
|
||
class FileNotification(Notification): | ||
file: str | ||
remove: bool | ||
contents: NotRequired[str] | ||
|
||
|
||
class FileClient(Client): | ||
def __init__( | ||
self, commands: dict[str, USER_FUNCTION], id_max: int = 15_000 | ||
) -> None: | ||
self.files: dict[str, str] = {} | ||
|
||
super().__init__(commands, id_max, FileServer) | ||
|
||
def create_server(self) -> None: | ||
"""Creates the main_server through a subprocess - internal API""" | ||
|
||
super().create_server() | ||
|
||
self.logger.info("Copying files to server") | ||
files_copy = self.files.copy() | ||
self.files = {} | ||
for file, data in files_copy.items(): | ||
self.update_file(file, data) | ||
self.logger.debug("Finished copying files to server") | ||
|
||
def update_file(self, file: str, current_state: str) -> None: | ||
"""Updates files in the system - external API""" | ||
|
||
self.logger.info(f"Updating file: {file}") | ||
self.files[file] = current_state | ||
|
||
self.logger.debug("Creating notification dict") | ||
notification: FileNotification = { | ||
"id": super().create_message_id(), | ||
"type": "notification", | ||
"file": file, | ||
"remove": False, | ||
"contents": self.files[file], | ||
} | ||
|
||
self.logger.debug("Notifying server of file update") | ||
super().notify_server(notification) | ||
|
||
def remove_file(self, file: str) -> None: | ||
"""Removes a file from the main_server - external API""" | ||
if file not in list(self.files.keys()): | ||
self.logger.exception( | ||
f"Cannot remove file {file} as file is not in file database!" | ||
) | ||
raise Exception( | ||
f"Cannot remove file {file} as file is not in file database!" | ||
) | ||
|
||
self.logger.info("Notifying server of file deletion") | ||
# self.create_message("notification", remove=True, file=file) | ||
notification: FileNotification = { | ||
"id": super().create_message_id(), | ||
"type": "notification", | ||
"file": file, | ||
"remove": True, | ||
} | ||
self.logger.debug("Notifying server of file removal") | ||
super().notify_server(notification) | ||
|
||
|
||
class FileServer(Server): | ||
def __init__( | ||
self, | ||
commands: dict[str, USER_FUNCTION], | ||
response_queue: GenericQueueClass, | ||
requests_queue: GenericQueueClass, | ||
logger: Logger, | ||
) -> None: | ||
self.files: dict[str, str] = {} | ||
|
||
super().__init__(commands, response_queue, requests_queue, logger) | ||
|
||
def parse_line(self, message: Request | Notification) -> None: | ||
self.logger.debug("Parsing Message from user - pre-super") | ||
id: int = message["id"] | ||
|
||
if message["type"] == "notification": | ||
self.logger.debug("Mesage is of type notification") | ||
|
||
file: str = message["file"] # type: ignore | ||
|
||
if message["remove"]: # type: ignore | ||
self.logger.info(f"File {file} was requested for removal") | ||
self.files.pop(file) | ||
self.logger.info(f"File {file} has been removed") | ||
else: | ||
contents: str = message["contents"] # type: ignore | ||
self.files[file] = contents | ||
self.logger.info( | ||
f"File {file} has been updated with new contents" | ||
) | ||
|
||
self.simple_id_response(id, False) | ||
return | ||
|
||
super().parse_line(message) | ||
|
||
def handle_request(self, request: Request) -> None: | ||
if "file" in request: | ||
file = request["file"] | ||
request["file"] = self.files[file] | ||
|
||
super().handle_request(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from .client import Client # noqa: F401, E402 | ||
from .misc import ( # noqa: F401, E402 | ||
USER_FUNCTION, | ||
Notification, | ||
Request, | ||
RequestQueueType, | ||
Response, | ||
ResponseQueueType, | ||
) | ||
from .server import Server # noqa: F401, E402 |
Oops, something went wrong.