From c40d910b69bdbc9212814d35db793f19c3ac2703 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Tue, 8 Aug 2023 19:50:20 +0200 Subject: [PATCH] Make backup path always set --- novelwriter/common.py | 3 +- novelwriter/config.py | 33 ++++++++++----------- novelwriter/core/project.py | 18 ++++------- tests/reference/baseConfig_novelwriter.conf | 4 +-- tests/test_base/test_base_config.py | 2 +- tests/test_core/test_core_project.py | 4 --- 6 files changed, 26 insertions(+), 38 deletions(-) diff --git a/novelwriter/common.py b/novelwriter/common.py index a325e604..35db1c9c 100644 --- a/novelwriter/common.py +++ b/novelwriter/common.py @@ -121,8 +121,7 @@ def checkUuid(value: Any, default: str) -> str: def checkPath(value: Any, default: Path) -> Path: - """Check if a value is a valid path. Non-empty strings are accepted. - """ + """Check if a value is a valid path.""" if isinstance(value, Path): return value elif isinstance(value, str): diff --git a/novelwriter/config.py b/novelwriter/config.py index 9c96e1be..1edffa10 100644 --- a/novelwriter/config.py +++ b/novelwriter/config.py @@ -94,8 +94,8 @@ class Config: # User Settings # ============= - self._theme = None - self._recent = RecentProjects(self) + self._themeObj = None + self._recentObj = RecentProjects(self) # General GUI Settings self.guiLocale = self._qLocale.name() @@ -107,7 +107,6 @@ class Config: self.hideVScroll = False # Hide vertical scroll bars on main widgets self.hideHScroll = False # Hide horizontal scroll bars on main widgets self.lastNotes = "0x0" # The latest release notes that have been shown - self._lastPath = self._homePath # The user's last used path # Size Settings self._mainWinSize = [1200, 650] # Last size of the main GUI window @@ -121,7 +120,6 @@ class Config: self.autoSaveProj = 60 # Interval for auto-saving project, in seconds self.autoSaveDoc = 30 # Interval for auto-saving document, in seconds self.emphLabels = True # Add emphasis to H1 and H2 item labels - self._backupPath = None # Backup path to use, can be none self.backupOnClose = False # Flag for running automatic backups self.askBeforeBackup = True # Flag for asking before running automatic backup @@ -174,6 +172,10 @@ class Config: self.fmtPadAfter = "" self.fmtPadThin = False + # User Paths + self._lastPath = self._homePath # The user's last used path + self._backupPath = self._homePath # Backup path to use, can be none + # Spell Checking Settings self.spellLanguage = "en" @@ -238,13 +240,13 @@ class Config: @property def recentProjects(self): - return self._recent + return self._recentObj @property def theme(self) -> GuiTheme: - if self._theme is None: + if self._themeObj is None: raise Exception("Cannot access GUI theme before it is initialised") - return self._theme + return self._themeObj @property def mainWinSize(self): @@ -295,7 +297,7 @@ class Config: def setThemeInstance(self, theme: GuiTheme) -> None: """Set the applications theme instance.""" - self._theme = theme + self._themeObj = theme return def setMainWinSize(self, newWidth, newHeight): @@ -351,9 +353,9 @@ class Config: logger.debug("Last path updated: %s" % self._lastPath) return - def setBackupPath(self, backupPath: Path | None): + def setBackupPath(self, backupPath: Path | str): """Set the current backup path.""" - self._backupPath = checkPath(backupPath, None) + self._backupPath = checkPath(backupPath, self._homePath) return def setTextFont(self, family: str | None, pointSize: int = 12): @@ -411,12 +413,12 @@ class Config: return self._lastPath return self._homePath - def backupPath(self) -> Path | None: + def backupPath(self) -> Path: """Return the backup path.""" if isinstance(self._backupPath, Path): if self._backupPath.is_dir(): return self._backupPath - return None + return self._homePath def errorText(self) -> str: """Compile and return error messages from the initialisation of @@ -495,7 +497,7 @@ class Config: else: self.saveConfig() - self._recent.loadCache() + self._recentObj.loadCache() self._checkOptionalPackages() logger.debug("Config initialisation complete") @@ -647,9 +649,6 @@ class Config: # Check Values # ============ - # Check Certain Values for None - self.spellLanguage = self._checkNone(self.spellLanguage) - # If we're using straight quotes, disable auto-replace if self.fmtSQuoteOpen == self.fmtSQuoteClose == "'" and self.doReplaceSQuote: logger.info("Using straight single quotes, so disabling auto-replace") @@ -697,7 +696,7 @@ class Config: "autosaveproject": str(self.autoSaveProj), "autosavedoc": str(self.autoSaveDoc), "emphlabels": str(self.emphLabels), - "backuppath": str(self._backupPath or ""), + "backuppath": str(self._backupPath), "backuponclose": str(self.backupOnClose), "askbeforebackup": str(self.askBeforeBackup), } diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index 88450946..dfe7a737 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -416,14 +416,6 @@ class NWProject(QObject): logger.info("Backing up project") self.mainGui.setStatus(self.tr("Backing up project ...")) - backupPath = CONFIG.backupPath() - if not isinstance(backupPath, Path): - self.mainGui.makeAlert(self.tr( - "Cannot backup project because no valid backup path is set. " - "Please set a valid backup location in Preferences." - ), level=nwAlert.ERROR) - return False - if not self._data.name: self.mainGui.makeAlert(self.tr( "Cannot backup project because no project name is set. " @@ -432,6 +424,7 @@ class NWProject(QObject): return False cleanName = makeFileNameSafe(self._data.name) + backupPath = CONFIG.backupPath() baseDir = backupPath / cleanName try: baseDir.mkdir(exist_ok=True) @@ -444,11 +437,12 @@ class NWProject(QObject): timeStamp = formatTimeStamp(time(), fileSafe=True) archName = baseDir / f"{cleanName} {timeStamp}.zip" if self._storage.zipIt(archName, compression=2): - size = archName.stat().st_size + size = formatInt(archName.stat().st_size) if doNotify: - self.mainGui.makeAlert(self.tr( - "Backup archive file written to: {0} [{1}B]" - ).format(str(archName), formatInt(size))) + self.mainGui.makeAlert( + self.tr("Created a backup of your project of size {0}B.").format(size), + info=self.tr("Path: {0}").format(str(backupPath)) + ) else: self.mainGui.makeAlert(self.tr( "Could not write backup archive." diff --git a/tests/reference/baseConfig_novelwriter.conf b/tests/reference/baseConfig_novelwriter.conf index 50f12524..e1554e94 100644 --- a/tests/reference/baseConfig_novelwriter.conf +++ b/tests/reference/baseConfig_novelwriter.conf @@ -1,5 +1,5 @@ [Meta] -timestamp = 2023-08-02 14:53:36 +timestamp = 2023-08-08 19:01:25 [Main] theme = default @@ -10,7 +10,7 @@ localisation = en_GB hidevscroll = False hidehscroll = False lastnotes = 0x0 -lastpath = /home/vkbo +lastpath = [Sizes] mainwindow = 1200, 650 diff --git a/tests/test_base/test_base_config.py b/tests/test_base/test_base_config.py index 3f0d94c7..524cd8a1 100644 --- a/tests/test_base/test_base_config.py +++ b/tests/test_base/test_base_config.py @@ -111,7 +111,7 @@ def testBaseConfig_InitLoadSave(monkeypatch, fncPath, tstPaths): # Check that we have a default file copyfile(confFile, testFile) - ignore = ("timestamp", "lastnotes", "localisation", "lastpath") + ignore = ("timestamp", "lastnotes", "localisation", "lastpath", "backuppath") assert cmpFiles(testFile, compFile, ignoreStart=ignore) tstConf.errorText() # This clears the error cache diff --git a/tests/test_core/test_core_project.py b/tests/test_core/test_core_project.py index 598f861e..b13b75c4 100644 --- a/tests/test_core/test_core_project.py +++ b/tests/test_core/test_core_project.py @@ -590,10 +590,6 @@ def testCoreProject_Backup(monkeypatch, mockGUI, fncPath, tstPaths): # Invalid Settings # ================ - # Invalid path - CONFIG._backupPath = None - assert theProject.backupProject(doNotify=False) is False - # Missing project name CONFIG._backupPath = tstPaths.tmpDir theProject.data.setName("")