Improve backup feature (#1484)
This commit is contained in:
@@ -209,8 +209,7 @@ def checkIntTuple(value: int, valid: tuple | list | set, default: int) -> int:
|
|||||||
# =============================================================================================== #
|
# =============================================================================================== #
|
||||||
|
|
||||||
def formatInt(value: int) -> str:
|
def formatInt(value: int) -> str:
|
||||||
"""Formats an integer with k, M, G etc.
|
"""Formats an integer with k, M, G etc."""
|
||||||
"""
|
|
||||||
if not isinstance(value, int):
|
if not isinstance(value, int):
|
||||||
return "ERR"
|
return "ERR"
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ from novelwriter.core.sessions import NWSessionLog
|
|||||||
from novelwriter.core.projectxml import ProjectXMLReader, ProjectXMLWriter, XMLReadState
|
from novelwriter.core.projectxml import ProjectXMLReader, ProjectXMLWriter, XMLReadState
|
||||||
from novelwriter.core.projectdata import NWProjectData
|
from novelwriter.core.projectdata import NWProjectData
|
||||||
from novelwriter.common import (
|
from novelwriter.common import (
|
||||||
checkStringNone, formatTimeStamp, hexToInt, makeFileNameSafe, minmax
|
checkStringNone, formatInt, formatTimeStamp, hexToInt, makeFileNameSafe, minmax
|
||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING: # pragma: no cover
|
if TYPE_CHECKING: # pragma: no cover
|
||||||
@@ -446,14 +446,14 @@ class NWProject(QObject):
|
|||||||
), nwAlert.ERROR, exception=exc)
|
), nwAlert.ERROR, exception=exc)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
archName = baseDir / self.tr(
|
timeStamp = formatTimeStamp(time(), fileSafe=True)
|
||||||
"Backup from {0}"
|
archName = baseDir / f"{cleanName} {timeStamp}.zip"
|
||||||
).format(formatTimeStamp(time(), fileSafe=True) + ".zip")
|
|
||||||
if self._storage.zipIt(archName, compression=2):
|
if self._storage.zipIt(archName, compression=2):
|
||||||
|
size = archName.stat().st_size
|
||||||
if doNotify:
|
if doNotify:
|
||||||
self.mainGui.makeAlert(self.tr(
|
self.mainGui.makeAlert(self.tr(
|
||||||
"Backup archive file written to: {0}"
|
"Backup archive file written to: {0} [{1}B]"
|
||||||
).format(str(archName), nwAlert.INFO))
|
).format(str(archName), formatInt(size)), nwAlert.INFO)
|
||||||
else:
|
else:
|
||||||
self.mainGui.makeAlert(self.tr(
|
self.mainGui.makeAlert(self.tr(
|
||||||
"Could not write backup archive."
|
"Could not write backup archive."
|
||||||
|
|||||||
@@ -620,12 +620,12 @@ def testCoreProject_Backup(monkeypatch, mockGUI, fncPath, tstPaths):
|
|||||||
theFiles = list((tstPaths.tmpDir / "Test Minimal").iterdir())
|
theFiles = list((tstPaths.tmpDir / "Test Minimal").iterdir())
|
||||||
assert len(theFiles) in (1, 2) # Sometimes 2 due to clock tick
|
assert len(theFiles) in (1, 2) # Sometimes 2 due to clock tick
|
||||||
|
|
||||||
theZip = theFiles[-1].name
|
theZip = theFiles[-1]
|
||||||
assert theZip[:12] == "Backup from "
|
assert theZip.name.startswith("Test Minimal")
|
||||||
assert theZip[-4:] == ".zip"
|
assert theZip.suffix == ".zip"
|
||||||
|
|
||||||
# Extract the archive
|
# Extract the archive
|
||||||
with ZipFile(tstPaths.tmpDir / "Test Minimal" / theZip, mode="r") as inZip:
|
with ZipFile(tstPaths.tmpDir / "Test Minimal" / theZip.name, mode="r") as inZip:
|
||||||
inZip.extractall(tstPaths.tmpDir / "extract")
|
inZip.extractall(tstPaths.tmpDir / "extract")
|
||||||
|
|
||||||
# Check that the main project file was restored
|
# Check that the main project file was restored
|
||||||
|
|||||||
Reference in New Issue
Block a user