From 470daf0036ef12eca6afdd601928651a4f68cef9 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 25 Apr 2021 23:01:20 +0200 Subject: [PATCH] Make NWDoc class non-reusable --- nw/core/document.py | 38 ++++++++++++++------------------------ nw/core/index.py | 4 ++-- nw/core/project.py | 29 ++++++++++------------------- nw/core/tokenizer.py | 4 ++-- nw/dialogs/docmerge.py | 9 +++++---- nw/dialogs/docsplit.py | 15 ++++++++------- nw/gui/doceditor.py | 8 +++++--- nw/gui/projtree.py | 8 ++++---- 8 files changed, 50 insertions(+), 65 deletions(-) diff --git a/nw/core/document.py b/nw/core/document.py index c645a840..4607ebe0 100644 --- a/nw/core/document.py +++ b/nw/core/document.py @@ -38,16 +38,16 @@ logger = logging.getLogger(__name__) class NWDoc(): - def __init__(self, theProject): + def __init__(self, theProject, theHandle): self.theProject = theProject self.theParent = theProject.theParent # Internal Variables - self._theItem = None # The currently open item - self._docHandle = None # The handle of the currently open item - self._fileLoc = None # The file location of the currently open item - self._docMeta = {} # The meta data of the currently open item + self._docHandle = theHandle + self._theItem = self.theProject.projTree[theHandle] + self._fileLoc = None + self._docMeta = {} # Internal Mapping self.makeAlert = self.theParent.makeAlert @@ -68,26 +68,16 @@ class NWDoc(): self._docMeta = {} return - def readDocument(self, tHandle, isOrphan=False): - """Read a document from handle, capturing potential file system - errors and parse meta data. If the document doesn't exist on - disk, return an empty string. If something went wrong, return + def readDocument(self, isOrphan=False): + """Read a document from set handle, capturing potential file + system errors and parse meta data. If the document doesn't exist + on disk, return an empty string. If something went wrong, return None. """ - if not isHandle(tHandle): + if not isHandle(self._docHandle): return None - # Always clear first, since the object will often be reused. - self.clearDocument() - - self._docHandle = tHandle - if not isOrphan: - self._theItem = self.theProject.projTree[tHandle] - else: - self._theItem = None - if self._theItem is None and not isOrphan: - self.clearDocument() return None docFile = self._docHandle+".nwd" @@ -133,7 +123,7 @@ 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. """ - if self._docHandle is None: + if not isHandle(self._docHandle): return False self.theProject.ensureFolderStructure() @@ -170,14 +160,14 @@ class NWDoc(): return True - def deleteDocument(self, tHandle): + def deleteDocument(self): """Permanently delete a document source file and related files from the project data folder. """ - if not isHandle(tHandle): + if not isHandle(self._docHandle): return False - docFile = tHandle+".nwd" + docFile = self._docHandle+".nwd" chkList = [] chkList.append(os.path.join(self.theProject.projContent, docFile)) diff --git a/nw/core/index.py b/nw/core/index.py index d7b09ec4..16b5f1b8 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -115,8 +115,8 @@ class NWIndex(): if tItem.itemType != nwItemType.FILE: return False - theDoc = NWDoc(self.theProject) - theText = theDoc.readDocument(tHandle) + theDoc = NWDoc(self.theProject, tHandle) + theText = theDoc.readDocument() if theText: self.scanText(tHandle, theText) diff --git a/nw/core/project.py b/nw/core/project.py index e0e978ee..7c95b997 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -265,9 +265,6 @@ class NWProject(): if self.bookAuthors: titlePage = "%s%s %s\n" % (titlePage, self.tr("By"), self.getAuthors()) - # Document object for writing files - aDoc = NWDoc(self) - if popMinimal: # Creating a minimal project with a few root folders and a # single chapter folder with a single file. @@ -284,17 +281,14 @@ class NWProject(): self.projTree.setFileItemLayout(xHandle[5], nwItemLayout.TITLE) self.projTree.setFileItemLayout(xHandle[7], nwItemLayout.CHAPTER) - aDoc.readDocument(xHandle[5]) + aDoc = NWDoc(self, xHandle[5]) aDoc.writeDocument(titlePage) - aDoc.clearDocument() - aDoc.readDocument(xHandle[7]) + aDoc = NWDoc(self, xHandle[7]) aDoc.writeDocument("## %s\n\n" % self.tr("New Chapter")) - aDoc.clearDocument() - aDoc.readDocument(xHandle[8]) + aDoc = NWDoc(self, xHandle[8]) aDoc.writeDocument("### %s\n\n" % self.tr("New Scene")) - aDoc.clearDocument() elif popCustom: # Create a project structure based on selected root folders @@ -311,9 +305,8 @@ class NWProject(): tHandle = self.newFile(self.tr("Title Page"), nwItemClass.NOVEL, nHandle) self.projTree.setFileItemLayout(tHandle, nwItemLayout.TITLE) - aDoc.readDocument(tHandle) + aDoc = NWDoc(self, tHandle) aDoc.writeDocument(titlePage) - aDoc.clearDocument() # Create chapters and scenes numChapters = projData.get("numChapters", 0) @@ -331,9 +324,8 @@ class NWProject(): cHandle = self.newFile(chTitle, nwItemClass.NOVEL, pHandle) self.projTree.setFileItemLayout(cHandle, nwItemLayout.CHAPTER) - aDoc.readDocument(cHandle) + aDoc = NWDoc(self, cHandle) aDoc.writeDocument("## %s\n\n" % chTitle) - aDoc.clearDocument() # Create chapter scenes if numScenes > 0: @@ -341,9 +333,8 @@ class NWProject(): scTitle = self.tr("Scene {0}").format(f"{ch+1:d}.{sc+1:d}") sHandle = self.newFile(scTitle, nwItemClass.NOVEL, pHandle) - aDoc.readDocument(sHandle) + aDoc = NWDoc(self, sHandle) aDoc.writeDocument("### %s\n\n" % scTitle) - aDoc.clearDocument() # Create scenes (no chapters) elif numScenes > 0: @@ -351,9 +342,8 @@ class NWProject(): scTitle = self.tr("Scene {0}").format(f"{sc+1:d}") sHandle = self.newFile(scTitle, nwItemClass.NOVEL, nHandle) - aDoc.readDocument(sHandle) + aDoc = NWDoc(self, sHandle) aDoc.writeDocument("### %s\n\n" % scTitle) - aDoc.clearDocument() # Finalise if popCustom or popMinimal: @@ -1393,7 +1383,6 @@ class NWProject(): return # Handle orphans - aDoc = NWDoc(self) nOrph = 0 noWhere = False oPrefix = self.tr("Recovered") @@ -1404,7 +1393,9 @@ class NWProject(): oParent = None oClass = None oLayout = None - if aDoc.readDocument(oHandle, isOrphan=True) is not None: + + aDoc = NWDoc(self, oHandle) + if aDoc.readDocument(isOrphan=True) is not None: oName, oParent, oClass, oLayout = aDoc.getMeta() if oName: diff --git a/nw/core/tokenizer.py b/nw/core/tokenizer.py index d04ffe47..583eb948 100644 --- a/nw/core/tokenizer.py +++ b/nw/core/tokenizer.py @@ -284,8 +284,8 @@ class Tokenizer(): self.theText = theText else: # Otherwise, load it from file - theDoc = NWDoc(self.theProject) - theText = theDoc.readDocument(theHandle) + theDoc = NWDoc(self.theProject, theHandle) + theText = theDoc.readDocument() if theText: self.theText = theText diff --git a/nw/dialogs/docmerge.py b/nw/dialogs/docmerge.py index 80c0f0e6..ede75adc 100644 --- a/nw/dialogs/docmerge.py +++ b/nw/dialogs/docmerge.py @@ -107,10 +107,10 @@ class GuiDocMerge(QDialog): ) return - theDoc = NWDoc(self.theProject) theText = "" for tHandle in finalOrder: - docText = theDoc.readDocument(tHandle).rstrip("\n") + inDoc = NWDoc(self.theProject, tHandle) + docText = inDoc.readDocument().rstrip("\n") if docText: theText += docText+"\n\n" @@ -131,8 +131,9 @@ class GuiDocMerge(QDialog): newItem = self.theProject.projTree[nHandle] newItem.setStatus(srcItem.itemStatus) - theDoc.readDocument(nHandle) - theDoc.writeDocument(theText) + outDoc = NWDoc(self.theProject, nHandle) + outDoc.writeDocument(theText) + self.theParent.treeView.revealNewTreeItem(nHandle) self.theParent.openDocument(nHandle, doScroll=True) diff --git a/nw/dialogs/docsplit.py b/nw/dialogs/docsplit.py index 1944c875..2c8b1271 100644 --- a/nw/dialogs/docsplit.py +++ b/nw/dialogs/docsplit.py @@ -127,8 +127,8 @@ class GuiDocSplit(QDialog): ) return - theDoc = NWDoc(self.theProject) - theText = theDoc.readDocument(self.sourceItem) + inDoc = NWDoc(self.theProject, self.sourceItem) + theText = inDoc.readDocument() if theText is None: theText = "" @@ -217,9 +217,10 @@ class GuiDocSplit(QDialog): theText = "\n".join(theLines[iStart:iEnd]) theText = theText.rstrip("\n") + "\n\n" - theDoc.readDocument(nHandle) - theDoc.writeDocument(theText) - theDoc.clearDocument() + + outDoc = NWDoc(self.theProject, nHandle) + outDoc.writeDocument(theText) + self.theParent.treeView.revealNewTreeItem(nHandle) self._doClose() @@ -259,8 +260,8 @@ class GuiDocSplit(QDialog): return self.listBox.clear() - theDoc = NWDoc(self.theProject) - theText = theDoc.readDocument(self.sourceItem) + inDoc = NWDoc(self.theProject, self.sourceItem) + theText = inDoc.readDocument() if theText is None: theText = "" diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 759c0053..73c6c41e 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -75,7 +75,7 @@ class GuiDocEditor(QTextEdit): self.theTheme = theParent.theTheme self.theIndex = theParent.theIndex self.theProject = theParent.theProject - self.nwDocument = NWDoc(self.theProject) + self.nwDocument = NWDoc(self.theProject, None) self.docChanged = False # Flag for changed status of document self.spellCheck = False # Flag for spell checking enabled @@ -165,7 +165,7 @@ class GuiDocEditor(QTextEdit): """Clear the current document and reset all document related flags and counters. """ - self.nwDocument.clearDocument() + self.nwDocument = NWDoc(self.theProject, None) self.setReadOnly(True) self.clear() self.wcTimer.stop() @@ -297,7 +297,9 @@ class GuiDocEditor(QTextEdit): document is new (empty string), we set up the editor for editing the file. """ - theDoc = self.nwDocument.readDocument(tHandle) + self.nwDocument = NWDoc(self.theProject, tHandle) + + theDoc = self.nwDocument.readDocument() if theDoc is None: # There was an io error self.clearEditor() diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index 84415f80..6cd6a580 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -286,8 +286,8 @@ class GuiProjectTree(QTreeWidget): return True # This is a new files, so let's add some content - newDoc = NWDoc(self.theProject) - curTxt = newDoc.readDocument(tHandle) + newDoc = NWDoc(self.theProject, tHandle) + curTxt = newDoc.readDocument() if curTxt is None: curTxt = "" @@ -527,8 +527,8 @@ class GuiProjectTree(QTreeWidget): if self.theParent.docEditor.theHandle == tHandle: self.theParent.closeDocument() - theDoc = NWDoc(self.theProject) - theDoc.deleteDocument(tHandle) + delDoc = NWDoc(self.theProject, tHandle) + delDoc.deleteDocument() self.theIndex.deleteHandle(tHandle) self._deleteTreeItem(tHandle) self._setTreeChanged(True)