Move error reporting out of the NWDoc class
This commit is contained in:
+18
-15
@@ -27,11 +27,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
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
|
||||
##
|
||||
|
||||
@@ -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)
|
||||
|
||||
+11
-1
@@ -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)
|
||||
|
||||
|
||||
+6
-1
@@ -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)
|
||||
|
||||
+6
-1
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user