--- a/src/eric7/Preferences/ShortcutsFile.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/Preferences/ShortcutsFile.py Wed Jul 13 14:55:47 2022 +0200 @@ -26,20 +26,22 @@ """ Class representing the shortcuts JSON file. """ + def __init__(self: "ShortcutsFile", parent: QObject = None) -> None: """ Constructor - + @param parent reference to the parent object (defaults to None) @type QObject (optional) """ super().__init__(parent) - - def __addActionsToDict(self: "ShortcutsFile", category: str, actions: list, - actionsDict: dict) -> None: + + def __addActionsToDict( + self: "ShortcutsFile", category: str, actions: list, actionsDict: dict + ) -> None: """ Private method to add a list of actions to the actions dictionary. - + @param category category of the actions @type str @param actions list of actions @@ -55,14 +57,15 @@ # shortcuts are only exported, if their objectName is set actionsDict[category][act.objectName()] = ( act.shortcut().toString(), - act.alternateShortcut().toString() + act.alternateShortcut().toString(), ) - - def writeFile(self: "ShortcutsFile", filename: str, - helpViewer: HelpViewer = None) -> bool: + + def writeFile( + self: "ShortcutsFile", filename: str, helpViewer: HelpViewer = None + ) -> bool: """ Public method to write the shortcuts data to a shortcuts JSON file. - + @param filename name of the shortcuts file @type str @param helpViewer reference to the help window object @@ -71,94 +74,86 @@ @rtype bool """ actionsDict = {} - + # step 1: collect all the shortcuts if helpViewer is None: self.__addActionsToDict( - "Project", - ericApp().getObject("Project").getActions(), - actionsDict + "Project", ericApp().getObject("Project").getActions(), actionsDict ) self.__addActionsToDict( "General", - ericApp().getObject("UserInterface").getActions('ui'), - actionsDict + ericApp().getObject("UserInterface").getActions("ui"), + actionsDict, ) self.__addActionsToDict( "Wizards", - ericApp().getObject("UserInterface").getActions('wizards'), - actionsDict + ericApp().getObject("UserInterface").getActions("wizards"), + actionsDict, ) self.__addActionsToDict( - "Debug", - ericApp().getObject("DebugUI").getActions(), - actionsDict + "Debug", ericApp().getObject("DebugUI").getActions(), actionsDict ) self.__addActionsToDict( "Edit", - ericApp().getObject("ViewManager").getActions('edit'), - actionsDict + ericApp().getObject("ViewManager").getActions("edit"), + actionsDict, ) self.__addActionsToDict( "File", - ericApp().getObject("ViewManager").getActions('file'), - actionsDict + ericApp().getObject("ViewManager").getActions("file"), + actionsDict, ) self.__addActionsToDict( "Search", - ericApp().getObject("ViewManager").getActions('search'), - actionsDict + ericApp().getObject("ViewManager").getActions("search"), + actionsDict, ) self.__addActionsToDict( "View", - ericApp().getObject("ViewManager").getActions('view'), - actionsDict + ericApp().getObject("ViewManager").getActions("view"), + actionsDict, ) self.__addActionsToDict( "Macro", - ericApp().getObject("ViewManager").getActions('macro'), - actionsDict + ericApp().getObject("ViewManager").getActions("macro"), + actionsDict, ) self.__addActionsToDict( "Bookmarks", - ericApp().getObject("ViewManager").getActions('bookmark'), - actionsDict + ericApp().getObject("ViewManager").getActions("bookmark"), + actionsDict, ) self.__addActionsToDict( "Spelling", - ericApp().getObject("ViewManager").getActions('spelling'), - actionsDict + ericApp().getObject("ViewManager").getActions("spelling"), + actionsDict, ) self.__addActionsToDict( "Window", - ericApp().getObject("ViewManager").getActions('window'), - actionsDict + ericApp().getObject("ViewManager").getActions("window"), + actionsDict, ) - + for category, ref in ericApp().getPluginObjects(): if hasattr(ref, "getActions"): - self.__addActionsToDict( - category, ref.getActions(), actionsDict - ) - + self.__addActionsToDict(category, ref.getActions(), actionsDict) + else: self.__addActionsToDict( - helpViewer.getActionsCategory(), - helpViewer.getActions(), - actionsDict + helpViewer.getActionsCategory(), helpViewer.getActions(), actionsDict ) - + # step 2: assemble the data structure to be written shortcutsDict = {} # step 2.0: header shortcutsDict["header"] = { "comment": "eric keyboard shortcuts file", - "saved": time.strftime('%Y-%m-%d, %H:%M:%S'), + "saved": time.strftime("%Y-%m-%d, %H:%M:%S"), "author": Preferences.getUser("Email"), } # step 2.1: keyboard shortcuts shortcutsDict["shortcuts"] = actionsDict - + try: jsonString = json.dumps(shortcutsDict, indent=2) with open(filename, "w") as f: @@ -171,16 +166,16 @@ self.tr( "<p>The keyboard shortcuts file <b>{0}</b> could not" " be written.</p><p>Reason: {1}</p>" - ).format(filename, str(err)) + ).format(filename, str(err)), ) return False - + return True - + def readFile(self: "ShortcutsFile", filename: str) -> bool: """ Public method to read the shortcuts data from a shortcuts JSON file. - + @param filename name of the shortcuts file @type str @return Dictionary of dictionaries of shortcuts. The keys of the @@ -200,8 +195,8 @@ self.tr( "<p>The keyboard shortcuts file <b>{0}</b> could not be" " read.</p><p>Reason: {1}</p>" - ).format(filename, str(err)) + ).format(filename, str(err)), ) return {} - + return shortcutsDict["shortcuts"]