From 972ed25a8cc8e1e2db4ead40b42253721a244f63 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 5 Nov 2022 23:33:50 +0100 Subject: [PATCH] Remove project cache attribute from project class --- novelwriter/core/project.py | 6 ------ novelwriter/core/storage.py | 19 +++++++------------ novelwriter/tools/build.py | 13 ++++++++++--- tests/test_core/test_core_project.py | 8 -------- 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index 891d8dde..886fe1c2 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -85,7 +85,6 @@ class NWProject(QObject): # Class Settings self.projPath = None # The full path to where the currently open project is saved - self.projCache = None # The full path to the project's cache folder self.projContent = None # The full path to the project's content folder self.projDict = None # The spell check dictionary self.projFiles = [] # A list of all files in the content folder on load @@ -254,7 +253,6 @@ class NWProject(QObject): # Project Settings self.projPath = None - self.projCache = None self.projContent = None self.projDict = None self.projFiles = [] @@ -274,7 +272,6 @@ class NWProject(QObject): # ToDo: These should not be set explicitly, and should stay as Path self.projPath = str(self._storage.runtimePath) self.projContent = str(self._storage.contentPath) - self.projCache = str(self._storage.cachePath) logger.info("Opening project: %s", self.projPath) @@ -478,15 +475,12 @@ class NWProject(QObject): if self.projPath is None or self.projPath == "": return False - self.projCache = os.path.join(self.projPath, "cache") self.projContent = os.path.join(self.projPath, "content") if self.projPath == os.path.expanduser("~"): # Don't make a mess in the user's home folder return False - if not self._checkFolder(self.projCache): - return False if not self._checkFolder(self.projContent): return False diff --git a/novelwriter/core/storage.py b/novelwriter/core/storage.py index 485af8f0..05522469 100644 --- a/novelwriter/core/storage.py +++ b/novelwriter/core/storage.py @@ -76,18 +76,6 @@ class NWStorage: return self._runtimePath / "content" return None - @property - def metaPath(self): - if self._runtimePath is not None: - return self._runtimePath / "meta" - return None - - @property - def cachePath(self): - if self._runtimePath is not None: - return self._runtimePath / "cache" - return None - ## # Core Methods ## @@ -172,6 +160,13 @@ class NWStorage: return self._runtimePath / "meta" / fileName return None + def getCacheFile(self, fileName): + """Return the path to a file in the project cache folder. + """ + if self._runtimePath is not None: + return self._runtimePath / "cache" / fileName + return None + def readLockFile(self): """Read the project lock file. """ diff --git a/novelwriter/tools/build.py b/novelwriter/tools/build.py index 8278fb95..78089abf 100644 --- a/novelwriter/tools/build.py +++ b/novelwriter/tools/build.py @@ -29,6 +29,7 @@ import logging import novelwriter from time import time +from pathlib import Path from datetime import datetime from PyQt5.QtGui import ( @@ -1088,9 +1089,12 @@ class GuiBuildNovel(QDialog): def _loadCache(self): """Save the current data to cache. """ - buildCache = os.path.join(self.theProject.projCache, nwFiles.BUILD_CACHE) + buildCache = self.theProject.storage.getCacheFile(nwFiles.BUILD_CACHE) + if not isinstance(buildCache, Path): + return False + dataCount = 0 - if os.path.isfile(buildCache): + if buildCache.exists(): logger.debug("Loading build cache") try: with open(buildCache, mode="r", encoding="utf-8") as inFile: @@ -1115,7 +1119,10 @@ class GuiBuildNovel(QDialog): def _saveCache(self): """Save the current data to cache. """ - buildCache = os.path.join(self.theProject.projCache, nwFiles.BUILD_CACHE) + buildCache = self.theProject.storage.getCacheFile(nwFiles.BUILD_CACHE) + if not isinstance(buildCache, Path): + return False + logger.debug("Saving build cache") try: with open(buildCache, mode="w+", encoding="utf-8") as outFile: diff --git a/tests/test_core/test_core_project.py b/tests/test_core/test_core_project.py index 4404fdc3..8371a748 100644 --- a/tests/test_core/test_core_project.py +++ b/tests/test_core/test_core_project.py @@ -287,12 +287,6 @@ def testCoreProject_Helpers(monkeypatch, fncDir, mockGUI): mp.setattr("os.path.expanduser", lambda *a, **k: fncDir) assert theProject.ensureFolderStructure() is False - # Create a file to block cache folder - cacheDir = os.path.join(fncDir, "cache") - writeFile(cacheDir, "stuff") - assert theProject.ensureFolderStructure() is False - os.unlink(cacheDir) - # Create a file to block content folder contentDir = os.path.join(fncDir, "content") writeFile(contentDir, "stuff") @@ -301,8 +295,6 @@ def testCoreProject_Helpers(monkeypatch, fncDir, mockGUI): # Now, do it right assert theProject.ensureFolderStructure() is True - # assert os.path.isdir(metaDir) - assert os.path.isdir(cacheDir) assert os.path.isdir(contentDir) # END Test testCoreProject_Helpers