From 700d2823013ad35cc0ad19cf5bf2f51ebe031f5d Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Thu, 28 Jul 2022 15:58:33 -0500 Subject: [PATCH] Addon Manager: pylint cleanup for install_to_toolbar --- src/Mod/AddonManager/install_to_toolbar.py | 66 ++++++++++++---------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Mod/AddonManager/install_to_toolbar.py b/src/Mod/AddonManager/install_to_toolbar.py index c2457d836f..1868fd044d 100644 --- a/src/Mod/AddonManager/install_to_toolbar.py +++ b/src/Mod/AddonManager/install_to_toolbar.py @@ -19,17 +19,21 @@ # * * # *************************************************************************** +""" A collection of functions to handle installing a macro icon to the toolbar. """ + +import os + import FreeCAD import FreeCADGui from PySide2 import QtCore, QtWidgets import Addon -import os - translate = FreeCAD.Qt.translate def ask_to_install_toolbar_button(repo: Addon) -> None: + """ Presents a dialog to the user asking if they want to install a toolbar button for + a particular macro, and walks through that process if they agree to do so. """ pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons") do_not_show_dialog = pref.GetBool("dontShowAddMacroButtonDialog", False) button_exists = check_for_button(repo) @@ -48,6 +52,7 @@ def ask_to_install_toolbar_button(repo: Addon) -> None: def check_for_button(repo: Addon) -> bool: + """ Returns True if a button already exists for this macro, or False if not. """ command = FreeCADGui.Command.findCustomCommand(repo.macro.filename) if not command: return False @@ -64,7 +69,10 @@ def check_for_button(repo: Addon) -> bool: def ask_for_toolbar( repo: Addon, custom_toolbars -) -> object: # Returns the pref group for the new toolbar +) -> object: + """ Determine what toolbar to add the icon to. The first time it is called it prompts the + user to select or create a toolbar. After that, the prompt is optional and can be configured + via a preference. Returns the pref group for the new toolbar.""" pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons") # In this one spot, default True: if this is the first time we got to @@ -102,24 +110,24 @@ def ask_for_toolbar( pref.SetBool("alwaysAskForToolbar", True) if selection == new_menubar_option_text: return create_new_custom_toolbar() - else: - return get_toolbar_with_name(selection) - else: - return None - else: - custom_toolbar_name = pref.GetString( - "CustomToolbarName", "Auto-Created Macro Toolbar" - ) - toolbar = get_toolbar_with_name(custom_toolbar_name) - if not toolbar: - # They told us not to ask, but then the toolbar got deleted... ask anyway! - ask = pref.RemBool("alwaysAskForToolbar") - return ask_for_toolbar(repo, custom_toolbars) - else: - return toolbar + return get_toolbar_with_name(selection) + return None + + # If none of the above code returned... + custom_toolbar_name = pref.GetString( + "CustomToolbarName", "Auto-Created Macro Toolbar" + ) + toolbar = get_toolbar_with_name(custom_toolbar_name) + if not toolbar: + # They told us not to ask, but then the toolbar got deleted... ask anyway! + ask = pref.RemBool("alwaysAskForToolbar") + return ask_for_toolbar(repo, custom_toolbars) + return toolbar def get_toolbar_with_name(name: str) -> object: + """ Try to find a toolbar with a given name. Returns the preference group for the toolbar + if found, or None if it does not exist. """ top_group = FreeCAD.ParamGet("User parameter:BaseApp/Workbench/Global/Toolbar") custom_toolbars = top_group.GetGroups() for toolbar in custom_toolbars: @@ -133,6 +141,7 @@ def get_toolbar_with_name(name: str) -> object: def create_new_custom_toolbar() -> object: + """ Create a new custom toolbar and returns its preference group. """ # We need two names: the name of the auto-created toolbar, as it will be displayed to the # user in various menus, and the underlying name of the toolbar group. Both must be @@ -140,15 +149,14 @@ def create_new_custom_toolbar() -> object: # First, the displayed name custom_toolbar_name = "Auto-Created Macro Toolbar" - pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons") top_group = FreeCAD.ParamGet("User parameter:BaseApp/Workbench/Global/Toolbar") custom_toolbars = top_group.GetGroups() - name_taken = check_for_toolbar(custom_toolbars, custom_toolbar_name) + name_taken = check_for_toolbar(custom_toolbar_name) if name_taken: i = 2 # Don't use (1), start at (2) while True: test_name = custom_toolbar_name + f" ({i})" - if not check_for_toolbar(custom_toolbars, test_name): + if not check_for_toolbar(test_name): custom_toolbar_name = test_name i = i + 1 @@ -168,15 +176,14 @@ def create_new_custom_toolbar() -> object: return custom_toolbar -def check_for_toolbar(custom_toolbars, toolbar_name: str) -> bool: - tb = get_toolbar_with_name(toolbar_name) - if tb: - return True - else: - return False +def check_for_toolbar(toolbar_name: str) -> bool: + """ Returns True if the toolbar exists, otherwise False """ + return get_toolbar_with_name(toolbar_name) is not None def install_toolbar_button(repo: Addon) -> None: + """ If the user has requested a toolbar button be installed, this function is called + to continue the process and request any additional required information. """ pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons") custom_toolbar_name = pref.GetString( "CustomToolbarName", "Auto-Created Macro Toolbar" @@ -220,11 +227,12 @@ def install_toolbar_button(repo: Addon) -> None: install_macro_to_toolbar(repo, custom_toolbar) else: FreeCAD.Console.PrintMessage( - f"In the end, no custom toolbar was set, bailing out\n" + "In the end, no custom toolbar was set, bailing out\n" ) def install_macro_to_toolbar(repo: Addon, toolbar: object) -> None: + """ Adds an icon for the given macro to the given toolbar. """ menuText = repo.display_name tooltipText = f"{repo.display_name}" if repo.macro.comment: @@ -250,7 +258,7 @@ def install_macro_to_toolbar(repo: Addon, toolbar: object) -> None: icon_file = os.path.normpath( os.path.join(macro_repo_dir, repo.macro.name + "_icon.xpm") ) - with open(icon_file, "w") as f: + with open(icon_file, "w", encoding="utf-8") as f: f.write(repo.macro.xpm) pixmapText = icon_file else: