diff --git a/syng/client.py b/syng/client.py index f5da742..af7a55f 100644 --- a/syng/client.py +++ b/syng/client.py @@ -36,6 +36,7 @@ import logging import secrets import string import tempfile +import signal from argparse import ArgumentParser from dataclasses import dataclass from dataclasses import field @@ -374,6 +375,10 @@ async def handle_request_config(data: dict[str, Any]) -> None: await sio.emit("config", {"source": data["source"], "config": config}) +def terminate(*args): + print("OOPS") + + async def start_client(config: dict[str, Any]) -> None: """ Initialize the client and connect to the server. @@ -382,11 +387,12 @@ async def start_client(config: dict[str, Any]) -> None: :type config: dict[str, Any] :rtype: None """ + sources.update(configure_sources(config["sources"])) if "config" in config: last_song = ( - datetime.datetime.fromisoformat(config["config"]["last_song"]) + datetime.datetime.fromisoformat(config["config"]["last_song"]).timestamp() if "last_song" in config["config"] and config["config"]["last_song"] else None ) @@ -403,21 +409,9 @@ async def start_client(config: dict[str, Any]) -> None: await sio.connect(state.config["server"]) await sio.wait() + print("exit") -async def aiomain() -> None: - """ - Async main function. - - Parses the arguments, reads a config file and sets default values. Then - connects to a specified server. - - If no secret is given, a random secret will be generated and presented to - the user. - - """ - pass - def create_async_and_start_client(config): asyncio.run(start_client(config)) diff --git a/syng/gui.py b/syng/gui.py index 3803b87..7a1cf19 100644 --- a/syng/gui.py +++ b/syng/gui.py @@ -21,6 +21,7 @@ from .client import create_async_and_start_client, default_config, start_client from .sources import available_sources from .server import main as server_main + class DateAndTimePickerWindow(customtkinter.CTkToplevel): def __init__(self, parent, input_field): super().__init__(parent) @@ -32,8 +33,10 @@ class DateAndTimePickerWindow(customtkinter.CTkToplevel): # self.timepicker.addAll(constants.HOURS24) self.timepicker.pack(expand=True, fill="both") - button = customtkinter.CTkButton(self, text="Ok", command=partial(self.insert, input_field)) - button.pack(expand=True, fill='x') + button = customtkinter.CTkButton( + self, text="Ok", command=partial(self.insert, input_field) + ) + button.pack(expand=True, fill="x") def insert(self, input_field: customtkinter.CTkTextbox): input_field.delete("0.0", "end") @@ -51,9 +54,6 @@ class DateAndTimePickerWindow(customtkinter.CTkToplevel): self.destroy() - - - class OptionFrame(customtkinter.CTkScrollableFrame): def add_option_label(self, text): customtkinter.CTkLabel(self, text=text, justify="left").grid( @@ -143,12 +143,16 @@ class OptionFrame(customtkinter.CTkScrollableFrame): self.number_of_options += 1 def open_date_and_time_picker(self, name, input_field): - if name not in self.date_and_time_pickers or not self.date_and_time_pickers[name].winfo_exists(): - self.date_and_time_pickers[name] = DateAndTimePickerWindow(self, input_field) + if ( + name not in self.date_and_time_pickers + or not self.date_and_time_pickers[name].winfo_exists() + ): + self.date_and_time_pickers[name] = DateAndTimePickerWindow( + self, input_field + ) else: self.date_and_time_pickers[name].focus() - def add_date_time_option(self, name, description, value): self.add_option_label(description) self.date_time_options[name] = None @@ -279,10 +283,10 @@ class SyngGui(customtkinter.CTk): self.protocol("WM_DELETE_WINDOW", self.on_close) rel_path = os.path.dirname(__file__) - img = PIL.ImageTk.PhotoImage(file=os.path.join(rel_path,"static/syng.png")) + img = PIL.ImageTk.PhotoImage(file=os.path.join(rel_path, "static/syng.png")) self.wm_iconbitmap() self.iconphoto(False, img) - + self.server = None self.client = None @@ -313,17 +317,16 @@ class SyngGui(customtkinter.CTk): ) loadbutton.pack(side="left") - startbutton = customtkinter.CTkButton( + self.startbutton = customtkinter.CTkButton( fileframe, text="Start", command=self.start_client ) - startbutton.pack(side="right") + self.startbutton.pack(side="right") startserverbutton = customtkinter.CTkButton( fileframe, text="Start Server", command=self.start_server ) startserverbutton.pack(side="right") - open_web_button = customtkinter.CTkButton( fileframe, text="Open Web", command=self.open_web ) @@ -365,21 +368,28 @@ class SyngGui(customtkinter.CTk): self.updateQr() def start_client(self): - sources = {} - for source, tab in self.tabs.items(): - sources[source] = tab.get_config() + if self.client is None: + sources = {} + for source, tab in self.tabs.items(): + sources[source] = tab.get_config() - general_config = self.general_config.get_config() + general_config = self.general_config.get_config() - config = {"sources": sources, "config": general_config} - # print(config) - self.client = multiprocessing.Process(target=create_async_and_start_client, args=(config,)) - self.client.start() + config = {"sources": sources, "config": general_config} + # print(config) + self.client = multiprocessing.Process( + target=create_async_and_start_client, args=(config,) + ) + self.client.start() + self.startbutton.configure(text="Stop") + else: + self.client.terminate() + self.client = None + self.startbutton.configure(text="Start") def start_server(self): self.server = multiprocessing.Process(target=server_main) self.server.start() - def open_web(self): config = self.general_config.get_config() diff --git a/syng/sources/__init__.py b/syng/sources/__init__.py index 976615f..c57d3a3 100644 --- a/syng/sources/__init__.py +++ b/syng/sources/__init__.py @@ -26,6 +26,6 @@ def configure_sources(configs: dict[str, Any]) -> dict[str, Source]: configured_sources = {} for source, config in configs.items(): if source in available_sources: - if config["enabled"]: + if "enabled" in config and config["enabled"]: configured_sources[source] = available_sources[source](config) return configured_sources