From 80e7b5dd0844311ffa01eb1d01b1d0cbb293c77d Mon Sep 17 00:00:00 2001 From: Jason Jerome Date: Sat, 29 Apr 2023 23:14:29 -0400 Subject: [PATCH] Added new gui themes parameters: reset/resetall - Added "reset" parameter: resets the selected gui theme options to the default values. - Added "resetall" parameter: resets all gui theme options to the default values and removes non-default themes. --- src/plugins/builtin_core/plugin.py | 30 ++++++++++ src/plugins/builtin_core/utility/constants.py | 4 ++ .../builtin_core/utility/theme_utils.py | 58 ++++++++++++++++++- 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/plugins/builtin_core/plugin.py b/src/plugins/builtin_core/plugin.py index ce7a0f3..d9a2bf0 100644 --- a/src/plugins/builtin_core/plugin.py +++ b/src/plugins/builtin_core/plugin.py @@ -117,6 +117,8 @@ def themes(self, data: "Command") -> None: # !themes.delete "theme_name" -> Deletes the specified theme, and falls back to a default theme if it was in use. # !themes.show "theme_name" -> Shows the config data for the specified theme. # !themes.update="theme_name" item1=value1... -> Updates a specified theme with the specified new values. + # !themes.reset "theme_name" -> Resets the specified theme back to default options. + # !themes.resetall -> Resets all the themes back to the default set of themes and options. _parameters = self.verify_parameters(self.themes.__name__, data) if _parameters is None: @@ -173,6 +175,33 @@ def _parameter_themes_new(self, data: "Command", parameter: str) -> None: target_users=mumble_utils.get_user_by_id(data.actor), ) + def _parameter_themes_reset(self, data: "Command", parameter: str) -> None: + _selected_theme = data.message.strip().replace(" ", "_") + if not theme_utils.reset_theme(_selected_theme): + logger.error(f"[{LogOutputIdentifiers.PLUGINS_COMMANDS}]: '{data.command}' command error: failed to reset theme.") + GUIFramework.gui( + text=f"Failed to reset theme: {_selected_theme}", + target_users=mumble_utils.get_user_by_id(data.actor), + ) + return + GUIFramework.gui( + text=f"Resetted selected theme: {_selected_theme}", + target_users=mumble_utils.get_user_by_id(data.actor), + ) + + def _parameter_themes_resetall(self, data: "Command", parameter: str) -> None: + if not theme_utils.reset_all_themes(): + logger.error(f"[{LogOutputIdentifiers.PLUGINS_COMMANDS}]: '{data.command}' command error: failed to reset all themes.") + GUIFramework.gui( + text="Failed to reset all themes.", + target_users=mumble_utils.get_user_by_id(data.actor), + ) + return + GUIFramework.gui( + text="Resetted all themes to defaults.", + target_users=mumble_utils.get_user_by_id(data.actor), + ) + def _parameter_themes_delete(self, data: "Command", parameter: str) -> None: _delete_theme = data.message.strip().replace(" ", "_") if not theme_utils.delete_theme(_delete_theme): @@ -194,6 +223,7 @@ def _parameter_themes_update(self, data: "Command", parameter: str) -> None: f"'{data._command}' command error: a user name was not provided.", target_users=mumble_utils.get_user_by_id(data.actor), ) + return _theme_name: str = parameter_split[1].strip().replace("_", " ") if not _theme_name: diff --git a/src/plugins/builtin_core/utility/constants.py b/src/plugins/builtin_core/utility/constants.py index 46f7d81..62f718f 100644 --- a/src/plugins/builtin_core/utility/constants.py +++ b/src/plugins/builtin_core/utility/constants.py @@ -45,6 +45,8 @@ class Themes: DELETE: str = "delete" UPDATE: str = "update" SHOW: str = "show" + RESET: str = "reset" + RESETALL: str = "resetall" @staticmethod def get_definitions() -> List[str]: @@ -55,4 +57,6 @@ def get_definitions() -> List[str]: ParameterDefinitions.Themes.DELETE, ParameterDefinitions.Themes.UPDATE, ParameterDefinitions.Themes.SHOW, + ParameterDefinitions.Themes.RESET, + ParameterDefinitions.Themes.RESETALL, ] diff --git a/src/plugins/builtin_core/utility/theme_utils.py b/src/plugins/builtin_core/utility/theme_utils.py index 16983a2..37e1fc9 100644 --- a/src/plugins/builtin_core/utility/theme_utils.py +++ b/src/plugins/builtin_core/utility/theme_utils.py @@ -52,7 +52,7 @@ def delete_theme(theme: str) -> bool: def new_theme(theme: str) -> bool: - _template_theme = _get_template_theme() + _template_theme = _get_custom_theme_template() _themes: Optional["Config"] = settings.configs.get_gui_themes() if not _themes: logger.error("Failed to create new gui theme: the gui themes could not be retrieved from settings.") @@ -73,6 +73,42 @@ def new_theme(theme: str) -> bool: return True +def reset_theme(theme: str) -> bool: + _template_theme = _get_custom_theme_template() + _themes: Optional["Config"] = settings.configs.get_gui_themes() + if not _themes: + logger.error("Failed to reset gui theme: the gui themes could not be retrieved from settings.") + return False + + theme = theme.strip().replace(" ", "_") + _selected_theme = _themes.get(theme, None) + if not _selected_theme: + logger.error(f"Failed to reset gui theme: theme '{theme}' does not exist.") + return False + _selected_theme = _template_theme + _themes.update({theme: _selected_theme}) + + _themes.save() + logger.debug(f"Reset gui theme from template: {theme}.") + + return True + + +def reset_all_themes() -> bool: + _template_default_themes = _get_default_themes_template() + _themes: Optional["Config"] = settings.configs.get_gui_themes() + if not _themes: + logger.error("Failed to reset gui themes: the gui themes could not be retrieved from settings.") + return False + _themes.clear() + _themes.update(_template_default_themes) + + _themes.save() + logger.debug("Reset all gui themes from default themes template.") + + return True + + def update_theme(theme: str, items: Dict[str, Any]) -> bool: _themes: Optional["Config"] = settings.configs.get_gui_themes() if not _themes: @@ -96,7 +132,7 @@ def update_theme(theme: str, items: Dict[str, Any]) -> bool: return True -def _get_template_theme() -> "Config": +def _get_custom_theme_template() -> "Config": _config: Optional["Config"] = settings.configs.get_mumimo_config() if not _config: raise PluginError("Unable to get template theme: mumimo config could not be retrieved from settings.", logger=logger) @@ -118,6 +154,24 @@ def _get_template_theme() -> "Config": return _template +def _get_default_themes_template() -> "Config": + _config: Optional["Config"] = settings.configs.get_mumimo_config() + if not _config: + raise PluginError("Unable to get default template themes: mumimo config could not be retrieved from settings.", logger=logger) + + _plugin_path = _config.get(MumimoCfgFields.SETTINGS.PLUGINS.PLUGINS_PATH, None) + if not _plugin_path: + raise PluginError("Unable to get default template themes: mumimo config does not have a defined plugin path.") + + _theme_template_path = pathlib.Path.cwd() / _plugin_path / "builtin_core/resources/gui_themes_template.toml" + _themes_template: "Config" = Config(_theme_template_path) + if _themes_template is None: + raise PluginError(f"Unable to get default template themes: default template themes file is missing. Expected path: {_theme_template_path}") + _themes_template.read() + + return _themes_template + + def _get_theme(theme: str) -> Optional["Config"]: if not theme: logger.error("Failed to get gui theme: no theme name provided.")