--- a/src/eric7/Sessions/SessionFile.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/Sessions/SessionFile.py Wed Jul 13 14:55:47 2022 +0200 @@ -24,23 +24,24 @@ """ Class representing the session JSON file. """ + def __init__(self, isGlobal: bool, parent: QObject = None): """ Constructor - + @param isGlobal flag indicating a file for a global session @type bool @param parent reference to the parent object (defaults to None) @type QObject (optional) """ super().__init__(parent) - + self.__isGlobal = isGlobal - + def writeFile(self, filename: str) -> bool: """ Public method to write the session data to a session JSON file. - + @param filename name of the session file @type str @return flag indicating a successful write @@ -53,36 +54,31 @@ vm = ericApp().getObject("ViewManager") dbg = ericApp().getObject("DebugUI") dbs = ericApp().getObject("DebugServer") - + # prepare the session data dictionary # step 0: header sessionDict = {} sessionDict["header"] = {} if not self.__isGlobal: - sessionDict["header"]["comment"] = ( - "eric session file for project {0}" - .format(project.getProjectName()) - ) - sessionDict["header"]["warning"] = ( - "This file was generated automatically, do not edit." - ) - + sessionDict["header"][ + "comment" + ] = "eric session file for project {0}".format(project.getProjectName()) + sessionDict["header"][ + "warning" + ] = "This file was generated automatically, do not edit." + if Preferences.getProject("TimestampFile") or self.__isGlobal: - sessionDict["header"]["saved"] = ( - time.strftime('%Y-%m-%d, %H:%M:%S') - ) - + sessionDict["header"]["saved"] = time.strftime("%Y-%m-%d, %H:%M:%S") + # step 1: open multi project and project for global session sessionDict["MultiProject"] = "" sessionDict["Project"] = "" if self.__isGlobal: if multiProject.isOpen(): - sessionDict["MultiProject"] = ( - multiProject.getMultiProjectFile() - ) + sessionDict["MultiProject"] = multiProject.getMultiProjectFile() if project.isOpen(): sessionDict["Project"] = project.getProjectFile() - + # step 2: all open (project) filenames and the active editor if vm.canSplit(): sessionDict["ViewManagerSplits"] = { @@ -94,8 +90,8 @@ "Count": 0, "Orientation": 1, } - - editorsDict = {} # remember editors by file name to detect clones + + editorsDict = {} # remember editors by file name to detect clones sessionDict["Editors"] = [] allOpenEditorLists = vm.getOpenEditorsForSession() for splitIndex, openEditorList in enumerate(allOpenEditorLists): @@ -117,7 +113,7 @@ "Editorindex": editorIndex, } sessionDict["Editors"].append(editorDict) - + aw = vm.getActiveName() sessionDict["ActiveWindow"] = {} if aw and (self.__isGlobal or project.isProjectFile(aw)): @@ -126,7 +122,7 @@ "Filename": aw, "Cursor": ed.getCursorPosition(), } - + # step 3: breakpoints allBreaks = Preferences.getProject("SessionAllBreakpoints") projectFiles = project.getSources(True) @@ -135,15 +131,13 @@ sessionDict["Breakpoints"] = bpModel.getAllBreakpoints() else: sessionDict["Breakpoints"] = [ - bp - for bp in bpModel.getAllBreakpoints() - if bp[0] in projectFiles + bp for bp in bpModel.getAllBreakpoints() if bp[0] in projectFiles ] - + # step 4: watch expressions wpModel = dbs.getWatchPointModel() sessionDict["Watchpoints"] = wpModel.getAllWatchpoints() - + # step 5: debug info if self.__isGlobal: if len(dbg.scriptsHistory): @@ -163,9 +157,7 @@ else: dbgEnv = "" if len(dbg.multiprocessNoDebugHistory): - dbgMultiprocessNoDebug = ( - dbg.multiprocessNoDebugHistory[0] - ) + dbgMultiprocessNoDebug = dbg.multiprocessNoDebugHistory[0] else: dbgMultiprocessNoDebug = "" sessionDict["DebugInfo"] = { @@ -201,7 +193,7 @@ "MultiprocessNoDebug": project.dbgMultiprocessNoDebug, "GlobalConfigOverride": project.dbgGlobalConfigOverride, } - + # step 6: bookmarks bookmarksList = [] for fileName in editorsDict: @@ -209,26 +201,29 @@ editor = editorsDict[fileName] bookmarks = editor.getBookmarks() if bookmarks: - bookmarksList.append({ - "Filename": fileName, - "Lines": bookmarks, - }) + bookmarksList.append( + { + "Filename": fileName, + "Lines": bookmarks, + } + ) sessionDict["Bookmarks"] = bookmarksList - + # step 7: state of the various project browsers browsersList = [] for browserName in projectBrowser.getProjectBrowserNames(): - expandedItems = ( - projectBrowser.getProjectBrowser(browserName) - .getExpandedItemNames() - ) + expandedItems = projectBrowser.getProjectBrowser( + browserName + ).getExpandedItemNames() if expandedItems: - browsersList.append({ - "Name": browserName, - "ExpandedItems": expandedItems, - }) + browsersList.append( + { + "Name": browserName, + "ExpandedItems": expandedItems, + } + ) sessionDict["ProjectBrowserStates"] = browsersList - + try: jsonString = json.dumps(sessionDict, indent=2) with open(filename, "w") as f: @@ -241,16 +236,16 @@ self.tr( "<p>The session 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, filename: str) -> bool: """ Public method to read the session data from a session JSON file. - + @param filename name of the project file @type str @return flag indicating a successful read @@ -267,10 +262,10 @@ self.tr( "<p>The session file <b>{0}</b> could not be read.</p>" "<p>Reason: {1}</p>" - ).format(filename, str(err)) + ).format(filename, str(err)), ) return False - + # get references to objects we need project = ericApp().getObject("Project") projectBrowser = ericApp().getObject("ProjectBrowser") @@ -278,35 +273,35 @@ vm = ericApp().getObject("ViewManager") dbg = ericApp().getObject("DebugUI") dbs = ericApp().getObject("DebugServer") - + # step 1: multi project and project # ================================= if sessionDict["MultiProject"]: multiProject.openMultiProject(sessionDict["MultiProject"], False) if sessionDict["Project"]: project.openProject(sessionDict["Project"], False) - + # step 2: (project) filenames and the active editor # ================================================= vm.setSplitOrientation( Qt.Orientation(sessionDict["ViewManagerSplits"]["Orientation"]) ) vm.setSplitCount(sessionDict["ViewManagerSplits"]["Count"]) - + editorsDict = {} for editorDict in sessionDict["Editors"]: if editorDict["Clone"] and editorDict["Filename"] in editorsDict: editor = editorsDict[editorDict["Filename"]] ed = vm.newEditorView( - editorDict["Filename"], editor, editor.getFileType(), - indexes=(editorDict["Splitindex"], - editorDict["Editorindex"]) + editorDict["Filename"], + editor, + editor.getFileType(), + indexes=(editorDict["Splitindex"], editorDict["Editorindex"]), ) else: ed = vm.openSourceFile( editorDict["Filename"], - indexes=(editorDict["Splitindex"], - editorDict["Editorindex"]) + indexes=(editorDict["Splitindex"], editorDict["Editorindex"]), ) editorsDict[editorDict["Filename"]] = ed if ed is not None: @@ -315,28 +310,28 @@ ed.recolor() ed.setContractedFolds(editorDict["Folds"]) ed.setCursorPosition(*editorDict["Cursor"]) - + # step 3: breakpoints # =================== bpModel = dbs.getBreakPointModel() bpModel.addBreakPoints(sessionDict["Breakpoints"]) - + # step 4: watch expressions # ========================= wpModel = dbs.getWatchPointModel() wpModel.addWatchPoints(sessionDict["Watchpoints"]) - + # step 5: debug info # ================== debugInfoDict = sessionDict["DebugInfo"] - + # adjust for newer session types if "GlobalConfigOverride" not in debugInfoDict: debugInfoDict["GlobalConfigOverride"] = { "enable": False, "redirect": True, } - + dbg.lastUsedVenvName = debugInfoDict["VirtualEnv"] with contextlib.suppress(KeyError): dbg.setScriptsHistory(debugInfoDict["ScriptName"]) @@ -351,8 +346,7 @@ dbg.setAutoContinue(debugInfoDict["AutoContinue"]) dbg.setEnableMultiprocess(debugInfoDict["EnableMultiprocess"]) dbg.setMultiprocessNoDebugHistory(debugInfoDict["MultiprocessNoDebug"]) - dbg.setEnableGlobalConfigOverride( - debugInfoDict["GlobalConfigOverride"]) + dbg.setEnableGlobalConfigOverride(debugInfoDict["GlobalConfigOverride"]) if not self.__isGlobal: project.setDbgInfo( debugInfoDict["VirtualEnv"], @@ -369,7 +363,7 @@ debugInfoDict["MultiprocessNoDebug"], debugInfoDict["GlobalConfigOverride"], ) - + # step 6: bookmarks # ================= for bookmark in sessionDict["Bookmarks"]: @@ -377,14 +371,14 @@ if editor is not None: for lineno in bookmark["Lines"]: editor.toggleBookmark(lineno) - + # step 7: state of the various project browsers # ============================================= for browserState in sessionDict["ProjectBrowserStates"]: browser = projectBrowser.getProjectBrowser(browserState["Name"]) if browser is not None: browser.expandItemsByName(browserState["ExpandedItems"]) - + # step 8: active window # ===================== if sessionDict["ActiveWindow"]: @@ -393,5 +387,5 @@ if ed is not None: ed.setCursorPosition(*sessionDict["ActiveWindow"]["Cursor"]) ed.ensureCursorVisible() - + return True