add loader to handle userprofiles in vm-compact

ensure we reload user profiles into memory
when rebuilding the config menu

patch bump
This commit is contained in:
onyx-and-iris 2023-06-26 16:08:58 +01:00
parent d896193ade
commit 5ab1fd7102
4 changed files with 61 additions and 2 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "voicemeeter-compact" name = "voicemeeter-compact"
version = "1.7.0" version = "1.7.1"
description = "A Compact Voicemeeter Remote App" description = "A Compact Voicemeeter Remote App"
authors = ["onyx-and-iris <code@onyxandiris.online>"] authors = ["onyx-and-iris <code@onyxandiris.online>"]
license = "MIT" license = "MIT"

View File

@ -1,9 +1,11 @@
import tkinter as tk import tkinter as tk
from functools import cached_property
from pathlib import Path from pathlib import Path
from tkinter import ttk from tkinter import ttk
from typing import NamedTuple from typing import NamedTuple
from .builders import MainFrameBuilder from .builders import MainFrameBuilder
from .configurations import loader
from .data import _base_values, _configuration, _kinds_all from .data import _base_values, _configuration, _kinds_all
from .errors import VMCompactError from .errors import VMCompactError
from .menu import Menus from .menu import Menus
@ -41,6 +43,7 @@ class App(tk.Tk):
self.iconbitmap(str(icon_path)) self.iconbitmap(str(icon_path))
self.minsize(275, False) self.minsize(275, False)
self.subject = Subject() self.subject = Subject()
self._configs = None
self["menu"] = Menus(self, vmr) self["menu"] = Menus(self, vmr)
self.styletable = ttk.Style() self.styletable = ttk.Style()
if _configuration.config: if _configuration.config:
@ -130,6 +133,11 @@ class App(tk.Tk):
self.drag_id = "" self.drag_id = ""
_base_values.dragging = False _base_values.dragging = False
@cached_property
def userconfigs(self):
self._configs = loader(self.kind.name)
return self._configs
_apps = {kind.name: App.make(kind) for kind in _kinds_all} _apps = {kind.name: App.make(kind) for kind in _kinds_all}

View File

@ -26,7 +26,7 @@ for configpath in configpaths:
try: try:
with open(filepath, "rb") as f: with open(filepath, "rb") as f:
configs[filename] = tomllib.load(f) configs[filename] = tomllib.load(f)
logger.info(f"{filename} loaded into memory") logger.info(f"configuration: {filename} loaded into memory")
except tomllib.TOMLDecodeError: except tomllib.TOMLDecodeError:
logger.error(f"Invalid TOML config: configs/{filename.stem}") logger.error(f"Invalid TOML config: configs/{filename.stem}")
@ -66,3 +66,19 @@ else:
def get_configuration(key): def get_configuration(key):
if key in configuration: if key in configuration:
return 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

View File

@ -93,6 +93,14 @@ class Menus(tk.Menu):
for profile in self.target.configs.keys() for profile in self.target.configs.keys()
if profile not in self.config_defaults 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: else:
self.menu_configs.entryconfig(0, state="disabled") self.menu_configs.entryconfig(0, state="disabled")
self.menu_configs.add_command( self.menu_configs.add_command(
@ -213,6 +221,10 @@ class Menus(tk.Menu):
self._unlock.set(not self._lock.get()) self._unlock.set(not self._lock.get())
setattr(self.target.command, cmd, val) setattr(self.target.command, cmd, val)
def load_custom_profile(self, profile):
self.logger.info(f"loading user profile {profile}")
self.target.apply(profile)
def load_profile(self, profile): def load_profile(self, profile):
self.logger.info(f"loading user profile {profile}") self.logger.info(f"loading user profile {profile}")
self.target.apply_config(profile) self.target.apply_config(profile)
@ -289,6 +301,11 @@ class Menus(tk.Menu):
for key in self.target.configs.keys() for key in self.target.configs.keys()
if key not in self.config_defaults if key not in self.config_defaults
] ]
[
self.menu_configs_load.delete(key)
for key in self.parent.userconfigs.keys()
if key not in self.config_defaults
]
[ [
self.menu_vban.entryconfig(j, state="disabled") self.menu_vban.entryconfig(j, state="disabled")
@ -307,6 +324,14 @@ class Menus(tk.Menu):
for profile in self.target.configs.keys() for profile in self.target.configs.keys()
if profile not in self.config_defaults 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: else:
self.menu_configs.entryconfig(0, state="disabled") self.menu_configs.entryconfig(0, state="disabled")
@ -346,6 +371,11 @@ class Menus(tk.Menu):
self.menu_layout.entryconfig( self.menu_layout.entryconfig(
0, state=f"{'normal' if kind.name == 'potato' else 'disabled'}" 0, state=f"{'normal' if kind.name == 'potato' else 'disabled'}"
) )
# ensure the configs are reloaded into memory
if "config" in self.parent.target.__dict__:
del self.parent.target.__dict__["config"]
if "userconfigs" in self.parent.__dict__:
del self.parent.__dict__["userconfigs"]
self.menu_setup() self.menu_setup()
def vban_disconnect(self, i): def vban_disconnect(self, i):
@ -366,6 +396,11 @@ class Menus(tk.Menu):
self.menu_layout.entryconfig( self.menu_layout.entryconfig(
0, state=f"{'normal' if kind.name == 'potato' else 'disabled'}" 0, state=f"{'normal' if kind.name == 'potato' else 'disabled'}"
) )
# ensure the configs are reloaded into memory
if "config" in self.parent.target.__dict__:
del self.parent.target.__dict__["config"]
if "userconfigs" in self.parent.__dict__:
del self.parent.__dict__["userconfigs"]
self.menu_setup() self.menu_setup()
self.after(15000, self.enable_vban_menus) self.after(15000, self.enable_vban_menus)