From 6d46d9a9a5cf8afe5cd51e82ccc7c79025e17fc1 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sat, 8 Jul 2023 00:22:07 +0100 Subject: [PATCH] userconfigs now returns target.configs patch bump --- pyproject.toml | 2 +- vmcompact/app.py | 2 +- vmcompact/configurations.py | 77 ++++++++++++++++++++----------------- vmcompact/menu.py | 61 ++++++++--------------------- 4 files changed, 61 insertions(+), 81 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 578fe2e..93ef900 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "voicemeeter-compact" -version = "1.8.3" +version = "1.8.4" description = "A Compact Voicemeeter Remote App" authors = ["onyx-and-iris "] license = "MIT" diff --git a/vmcompact/app.py b/vmcompact/app.py index cec16be..dd2b68a 100644 --- a/vmcompact/app.py +++ b/vmcompact/app.py @@ -145,7 +145,7 @@ class App(tk.Tk): @cached_property def userconfigs(self): - self._configs = loader(self.kind.name) + self._configs = loader(self.kind.name, self.target) return self._configs diff --git a/vmcompact/configurations.py b/vmcompact/configurations.py index 6d9cc6b..3863f25 100644 --- a/vmcompact/configurations.py +++ b/vmcompact/configurations.py @@ -10,28 +10,32 @@ logger = logging.getLogger(__name__) configuration = {} -configpaths = [ - Path.cwd() / "configs", - Path.home() / ".config" / "vm-compact" / "configs", - Path.home() / "Documents" / "Voicemeeter" / "configs", -] -for configpath in configpaths: - if configpath.is_dir(): - filepaths = list(configpath.glob("*.toml")) - if any(f.stem in ("app", "vban") for f in filepaths): - configs = {} - for filepath in filepaths: - filename = filepath.with_suffix("").stem - if filename in ("app", "vban"): - try: - with open(filepath, "rb") as f: - configs[filename] = tomllib.load(f) - logger.info(f"configuration: {filename} loaded into memory") - except tomllib.TOMLDecodeError: - logger.error(f"Invalid TOML config: configs/{filename.stem}") - configuration |= configs - break +def get_configpath(): + configpaths = [ + Path.cwd() / "configs", + Path.home() / ".config" / "vm-compact" / "configs", + Path.home() / "Documents" / "Voicemeeter" / "configs", + ] + for configpath in configpaths: + if configpath.exists(): + return configpath + + +if configpath := get_configpath(): + filepaths = list(configpath.glob("*.toml")) + if any(f.stem in ("app", "vban") for f in filepaths): + configs = {} + for filepath in filepaths: + filename = filepath.with_suffix("").stem + if filename in ("app", "vban"): + try: + with open(filepath, "rb") as f: + configs[filename] = tomllib.load(f) + logger.info(f"configuration: {filename} loaded into memory") + except tomllib.TOMLDecodeError: + logger.error(f"Invalid TOML config: configs/{filename.stem}") + configuration |= configs _defaults = { "configs": { @@ -75,17 +79,20 @@ def get_configuration(key): return configuration[key] -def loader(kind_id): - configs = {} - userconfigpath = Path.home() / ".config" / "vm-compact" / "configs" / kind_id - if userconfigpath.exists(): - filepaths = list(userconfigpath.glob("*.toml")) - for filepath in filepaths: - identifier = filepath.with_suffix("").stem - try: - with open(filepath, "rb") as f: - configs[identifier] = tomllib.load(f) - logger.info(f"loader: {identifier} loaded into memory") - except tomllib.TOMLDecodeError: - logger.error(f"Invalid TOML config: configs/{filename.stem}") - return configs +def loader(kind_id, target): + configs = {"reset": target.configs["reset"]} + if configpath := get_configpath(): + userconfigpath = configpath / kind_id + if userconfigpath.exists(): + filepaths = list(userconfigpath.glob("*.toml")) + for filepath in filepaths: + identifier = filepath.with_suffix("").stem + try: + with open(filepath, "rb") as f: + configs[identifier] = tomllib.load(f) + logger.info(f"loader: {identifier} loaded into memory") + except tomllib.TOMLDecodeError: + logger.error(f"Invalid TOML config: configs/{filename.stem}") + + target.configs = configs + return target.configs diff --git a/vmcompact/menu.py b/vmcompact/menu.py index 7cf30d2..a9af0bc 100644 --- a/vmcompact/menu.py +++ b/vmcompact/menu.py @@ -85,24 +85,16 @@ class Menus(tk.Menu): self.menu_configs_load = tk.Menu(self.menu_configs, tearoff=0) self.menu_configs.add_cascade(menu=self.menu_configs_load, label="Load config") self.config_defaults = {"reset"} - if len(self.target.configs) > len(self.config_defaults) and all( - key in self.target.configs for key in self.config_defaults + if len(self.parent.userconfigs) > len(self.config_defaults) and all( + key in self.parent.userconfigs for key in self.config_defaults ): [ self.menu_configs_load.add_command( label=profile, command=partial(self.load_profile, profile) ) - for profile in self.target.configs.keys() + for profile in self.parent.userconfigs.keys() if profile not in self.config_defaults ] - elif self.parent.userconfigs: - [ - self.menu_configs_load.add_command( - label=name, command=partial(self.load_custom_profile, data) - ) - for name, data in self.parent.userconfigs.items() - if name not in self.config_defaults - ] else: self.menu_configs.entryconfig(0, state="disabled") self.menu_configs.add_command( @@ -315,21 +307,13 @@ class Menus(tk.Menu): def menu_teardown(self, i): # remove config load menus - removed = [] - for key in self.target.configs.keys(): - if key not in self.config_defaults: - try: - self.menu_configs_load.delete(key) - removed.append(key) - except tk._tkinter.tclError as e: - self.logger.warning(f"{type(e).__name__}: {e}") - - for key in self.parent.userconfigs.keys(): - if key not in self.config_defaults and key not in removed: - try: - self.menu_configs_load.delete(key) - except tk._tkinter.tclError as e: - self.logger.warning(f"{type(e).__name__}: {e}") + if len(self.parent.userconfigs) > len(self.config_defaults): + for profile in self.parent.userconfigs: + if profile not in self.config_defaults: + try: + self.menu_configs_load.delete(profile) + except tk._tkinter.tclError as e: + self.logger.warning(f"{type(e).__name__}: {e}") [ self.menu_vban.entryconfig(j, state="disabled") @@ -338,24 +322,13 @@ class Menus(tk.Menu): ] def menu_setup(self): - if len(self.target.configs) > len(self.config_defaults) and all( - key in self.target.configs for key in self.config_defaults - ): - [ - self.menu_configs_load.add_command( - label=profile, command=partial(self.load_profile, profile) - ) - for profile in self.target.configs.keys() - if profile not in self.config_defaults - ] - elif self.parent.userconfigs: - [ - self.menu_configs_load.add_command( - label=name, command=partial(self.load_custom_profile, data) - ) - for name, data in self.parent.userconfigs.items() - if name not in self.config_defaults - ] + if len(self.parent.userconfigs) > len(self.config_defaults): + for profile in self.parent.userconfigs: + if profile not in self.config_defaults: + self.menu_configs_load.add_command( + label=profile, command=partial(self.load_profile, profile) + ) + self.menu_configs.entryconfig(0, state="normal") else: self.menu_configs.entryconfig(0, state="disabled")