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
|
||||
##
|
||||
|
||||
Reference in New Issue
Block a user