From 0eb09a2136040c860e753a104c7aed7460bfba6d Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 25 Apr 2021 23:21:51 +0200 Subject: [PATCH] Move error reporting out of the NWDoc class --- nw/core/document.py | 33 ++++++++++++++++++--------------- nw/dialogs/docmerge.py | 10 +++++++++- nw/dialogs/docsplit.py | 12 +++++++++++- nw/gui/doceditor.py | 7 ++++++- nw/gui/projtree.py | 7 ++++++- 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/nw/core/document.py b/nw/core/document.py index 4607ebe0..44f94203 100644 --- a/nw/core/document.py +++ b/nw/core/document.py @@ -27,11 +27,7 @@ along with this program. If not, see . import logging import os -from functools import partial - -from PyQt5.QtCore import QCoreApplication - -from nw.enum import nwAlert, nwItemLayout, nwItemClass +from nw.enum import nwItemLayout, nwItemClass from nw.common import isHandle logger = logging.getLogger(__name__) @@ -41,17 +37,13 @@ class NWDoc(): def __init__(self, theProject, theHandle): self.theProject = theProject - self.theParent = theProject.theParent # Internal Variables self._docHandle = theHandle self._theItem = self.theProject.projTree[theHandle] self._fileLoc = None self._docMeta = {} - - # Internal Mapping - self.makeAlert = self.theParent.makeAlert - self.tr = partial(QCoreApplication.translate, "NWDoc") + self._docError = "" return @@ -74,10 +66,13 @@ class NWDoc(): on disk, return an empty string. If something went wrong, return None. """ + self._docError = "" if not isHandle(self._docHandle): + self._docError = "No document handle set." return None if self._theItem is None and not isOrphan: + self._docError = "Unknown novelWriter document." return None docFile = self._docHandle+".nwd" @@ -105,12 +100,13 @@ class NWDoc(): theText += inFile.read() except Exception as e: - self.makeAlert([self.tr("Failed to open document file."), str(e)], nwAlert.ERROR) + self._docError = str(e) # Note: Document must be cleared in case of an io error, # or else the auto-save or save will try to overwrite it # with an empty file. Return None to alert the caller. self.clearDocument() return None + else: # The document file does not exist, so we assume it's a new # document and initialise an empty text string. @@ -123,7 +119,9 @@ class NWDoc(): """Write the document. The file is saved via a temp file in case of save failure. Returns True if successful, False if not. """ + self._docError = "" if not isHandle(self._docHandle): + self._docError = "No document handle set." return False self.theProject.ensureFolderStructure() @@ -149,7 +147,7 @@ class NWDoc(): outFile.write(docMeta) outFile.write(docText) except Exception as e: - self.makeAlert([self.tr("Could not save document."), str(e)], nwAlert.ERROR) + self._docError = str(e) return False # If we're here, the file was successfully saved, so we can @@ -164,7 +162,9 @@ class NWDoc(): """Permanently delete a document source file and related files from the project data folder. """ + self._docError = "" if not isHandle(self._docHandle): + self._docError = "No document handle set." return False docFile = self._docHandle+".nwd" @@ -179,9 +179,7 @@ class NWDoc(): os.unlink(chkFile) logger.debug("Deleted: %s" % chkFile) except Exception as e: - self.makeAlert( - [self.tr("Could not delete document file."), str(e)], nwAlert.ERROR - ) + self._docError = str(e) return False return True @@ -211,6 +209,11 @@ class NWDoc(): return theName, theParent, theClass, theLayout + def getError(self): + """Return the last recorded exception. + """ + return self._docError + ## # Internal Functions ## diff --git a/nw/dialogs/docmerge.py b/nw/dialogs/docmerge.py index ede75adc..b873575a 100644 --- a/nw/dialogs/docmerge.py +++ b/nw/dialogs/docmerge.py @@ -111,6 +111,11 @@ class GuiDocMerge(QDialog): for tHandle in finalOrder: inDoc = NWDoc(self.theProject, tHandle) docText = inDoc.readDocument().rstrip("\n") + docErr = inDoc.getError() + if docText is None and docErr: + self.makeAlert( + [self.tr("Failed to open document file."), docErr], nwAlert.ERROR + ) if docText: theText += docText+"\n\n" @@ -132,7 +137,10 @@ class GuiDocMerge(QDialog): newItem.setStatus(srcItem.itemStatus) outDoc = NWDoc(self.theProject, nHandle) - outDoc.writeDocument(theText) + if not outDoc.writeDocument(theText): + self.theParent.makeAlert( + [self.tr("Could not save document."), outDoc.getError()], nwAlert.ERROR + ) self.theParent.treeView.revealNewTreeItem(nHandle) self.theParent.openDocument(nHandle, doScroll=True) diff --git a/nw/dialogs/docsplit.py b/nw/dialogs/docsplit.py index 2c8b1271..d284056f 100644 --- a/nw/dialogs/docsplit.py +++ b/nw/dialogs/docsplit.py @@ -129,6 +129,13 @@ class GuiDocSplit(QDialog): inDoc = NWDoc(self.theProject, self.sourceItem) theText = inDoc.readDocument() + + docErr = inDoc.getError() + if theText is None and docErr: + self.theParent.makeAlert( + [self.tr("Failed to open document file."), docErr], nwAlert.ERROR + ) + if theText is None: theText = "" @@ -219,7 +226,10 @@ class GuiDocSplit(QDialog): theText = theText.rstrip("\n") + "\n\n" outDoc = NWDoc(self.theProject, nHandle) - outDoc.writeDocument(theText) + if not outDoc.writeDocument(theText): + self.theParent.makeAlert( + [self.tr("Could not save document."), outDoc.getError()], nwAlert.ERROR + ) self.theParent.treeView.revealNewTreeItem(nHandle) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 73c6c41e..3a9fe5c4 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -446,7 +446,12 @@ class GuiDocEditor(QTextEdit): theItem.setParaCount(self.paraCount) self.saveCursorPosition() - self.nwDocument.writeDocument(docText) + if not self.nwDocument.writeDocument(docText): + self.theParent.makeAlert([ + self.tr("Could not save document."), self.nwDocument.getError() + ], nwAlert.ERROR) + return False + self.setDocumentChanged(False) self.theIndex.scanText(tHandle, docText) diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index 6cd6a580..03f5c0c5 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -528,7 +528,12 @@ class GuiProjectTree(QTreeWidget): self.theParent.closeDocument() delDoc = NWDoc(self.theProject, tHandle) - delDoc.deleteDocument() + if not delDoc.deleteDocument(): + self.makeAlert([ + self.tr("Could not delete document file."), delDoc.getError() + ], nwAlert.ERROR) + return False + self.theIndex.deleteHandle(tHandle) self._deleteTreeItem(tHandle) self._setTreeChanged(True)