From f366426699eb64598df886e44525b5dd96446259 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 17 Dec 2023 21:29:43 +0200 Subject: [PATCH] Add a option to turn on saving and choose a folder of notifications. --- notification_server/cli.py | 8 +++++++- notification_server/server.py | 11 ++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/notification_server/cli.py b/notification_server/cli.py index 79099f2..62755ca 100644 --- a/notification_server/cli.py +++ b/notification_server/cli.py @@ -44,6 +44,12 @@ def up( 5554, "--producer-port", "-l", help="The server's port to listen [b]producers[/].", rich_help_panel="Connection", + ), + notification_dir: str = typer.Option( + None, "--notification-dir", "-d", + help="The server will save notifications to the directory by the path. Does not save if this option is not provided.", + rich_help_panel="Persistence", + show_default=False, ) ): """ @@ -51,7 +57,7 @@ def up( """ try: - server = Server(ip, port, producer_port) + server = Server(ip, port, producer_port, notification_dir) asyncio.run(server.up()) except KeyboardInterrupt: console = Console() diff --git a/notification_server/server.py b/notification_server/server.py index 74a5171..69319ab 100644 --- a/notification_server/server.py +++ b/notification_server/server.py @@ -18,18 +18,18 @@ ############################################################################### import struct -from rich.console import Console import asyncio +from rich.console import Console from notification_server.notification_saver import NotificationSaver from notification_server.timestamp import Timestamp class Server: BUFFER_SIZE = 1024 - NOTIFICATION_DIR = 'notifications' + NOTIFICATION_DIR = None console = Console() - def __init__(self, ip: str, port: int, producer_port: int, notification_dir: str = 'notifications'): + def __init__(self, ip: str, port: int, producer_port: int, notification_dir: str | None = None): self.NOTIFICATION_DIR = notification_dir self.__client_socket = None self.__producer_socket = None @@ -88,8 +88,9 @@ class Server: self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The producer ({addr[0]}:{addr[1]}) finished.") break - now = Timestamp.now() - NotificationSaver.save_notification(self.NOTIFICATION_DIR, title, message, now) + if not self.NOTIFICATION_DIR is None: + now = Timestamp.now() + NotificationSaver.save_notification(self.NOTIFICATION_DIR, title, message, now) await self.broadcast_message(title) await self.broadcast_message(message)