Add copy project feature to project builder class

This commit is contained in:
Veronica Berglyd Olsen
2024-01-31 23:56:09 +01:00
parent 90dda43205
commit b0f1db0aea
2 changed files with 53 additions and 7 deletions
+52 -6
View File
@@ -36,8 +36,8 @@ from functools import partial
from PyQt5.QtCore import QCoreApplication from PyQt5.QtCore import QCoreApplication
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.common import minmax, simplified from novelwriter.common import isHandle, minmax, simplified
from novelwriter.constants import nwItemClass from novelwriter.constants import nwFiles, nwItemClass
from novelwriter.core.item import NWItem from novelwriter.core.item import NWItem
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.core.storage import NWStorageCreate from novelwriter.core.storage import NWStorageCreate
@@ -330,9 +330,8 @@ class ProjectBuilder:
self._path = Path(path).resolve() self._path = Path(path).resolve()
if data.get("sample", False): if data.get("sample", False):
return self._extractSampleProject(self._path) return self._extractSampleProject(self._path)
elif data.get("template"): # pragma: no cover elif data.get("template"):
# Not implemented yet return self._copyProject(self._path, data)
return True
else: else:
return self._buildAndPopulate(self._path, data) return self._buildAndPopulate(self._path, data)
SHARED.error("A project path is required.") SHARED.error("A project path is required.")
@@ -348,7 +347,7 @@ class ProjectBuilder:
status = project.storage.createNewProject(path) status = project.storage.createNewProject(path)
if status == NWStorageCreate.NOT_EMPTY: if status == NWStorageCreate.NOT_EMPTY:
SHARED.error(self.tr( SHARED.error(self.tr(
"A project already exists in that location. " "The target folder is not empty. "
"Please choose another folder." "Please choose another folder."
)) ))
return False return False
@@ -454,6 +453,53 @@ class ProjectBuilder:
return True return True
def _copyProject(self, path: Path, data: dict) -> bool:
"""Copy an existing project content, but not the meta data, and
update new settings.
"""
source = data.get("template")
if not (isinstance(source, Path) and source.is_file()
and source.name == nwFiles.PROJ_FILE):
return False
logger.info("Copying project: %s", source)
if path.exists():
SHARED.error(self.tr(
"The target folder already exists. "
"Please choose another folder."
))
return False
# Begin copying
srcPath = source.parent
dstPath = path.resolve()
srcCont = srcPath / "content"
dstCont = dstPath / "content"
dstPath.mkdir(exist_ok=True)
dstCont.mkdir(exist_ok=True)
shutil.copy2(srcPath / nwFiles.PROJ_FILE, dstPath)
for contFile in srcCont.iterdir():
if contFile.is_file() and contFile.suffix == ".nwd" and isHandle(contFile.stem):
shutil.copy2(contFile, dstCont)
# Open the copied project and update settings
project = NWProject()
project.openProject(dstPath)
project.data.setUuid("") # Creates a fresh uuid
project.data.setName(data.get("name", "None"))
project.data.setAuthor(data.get("author", ""))
project.data.setLanguage(data.get("language", "en_GB"))
project.data.setSpellCheck(True)
project.data.setSpellLang(None)
project.data.setDoBackup(True)
project.data.setSaveCount(0)
project.data.setAutoCount(0)
project.data.setEditTime(0)
project.saveProject()
project.closeProject()
return True
def _extractSampleProject(self, path: Path) -> bool: def _extractSampleProject(self, path: Path) -> bool:
"""Make a copy of the sample project by extracting the """Make a copy of the sample project by extracting the
sample.zip file to the new path. sample.zip file to the new path.
+1 -1
View File
@@ -425,7 +425,7 @@ def testCoreTools_ProjectBuilderWrapper(monkeypatch, caplog, fncPath, mockGUI):
# Creating the project once more should fail # Creating the project once more should fail
caplog.clear() caplog.clear()
assert builder.buildProject({"path": fncPath}) is False assert builder.buildProject({"path": fncPath}) is False
assert "A project already exists" in caplog.text assert "The target folder is not empty." in caplog.text
# END Test testCoreTools_ProjectBuilderWrapper # END Test testCoreTools_ProjectBuilderWrapper