Rename open and save document to read and write in NWDoc

This commit is contained in:
Veronica K. B. Olsen
2021-04-25 17:10:12 +02:00
parent 1e48c6dcfe
commit d473615ccf
11 changed files with 67 additions and 59 deletions
+4 -4
View File
@@ -68,8 +68,8 @@ class NWDoc():
self._docMeta = {}
return
def openDocument(self, tHandle, showStatus=True, isOrphan=False):
"""Open a document from handle, capturing potential file system
def readDocument(self, tHandle, showStatus=True, 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
None.
@@ -134,8 +134,8 @@ class NWDoc():
return theText
def saveDocument(self, docText):
"""Save the document. The file is saved via a temp file in case
def writeDocument(self, docText):
"""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:
+1 -1
View File
@@ -117,7 +117,7 @@ class NWIndex():
return False
theDoc = NWDoc(self.theProject, self.theParent)
theText = theDoc.openDocument(tHandle, showStatus=False)
theText = theDoc.readDocument(tHandle, showStatus=False)
if theText:
self.scanText(tHandle, theText)
+15 -15
View File
@@ -284,16 +284,16 @@ class NWProject():
self.projTree.setFileItemLayout(xHandle[5], nwItemLayout.TITLE)
self.projTree.setFileItemLayout(xHandle[7], nwItemLayout.CHAPTER)
aDoc.openDocument(xHandle[5], showStatus=False)
aDoc.saveDocument(titlePage)
aDoc.readDocument(xHandle[5], showStatus=False)
aDoc.writeDocument(titlePage)
aDoc.clearDocument()
aDoc.openDocument(xHandle[7], showStatus=False)
aDoc.saveDocument("## %s\n\n" % self.tr("New Chapter"))
aDoc.readDocument(xHandle[7], showStatus=False)
aDoc.writeDocument("## %s\n\n" % self.tr("New Chapter"))
aDoc.clearDocument()
aDoc.openDocument(xHandle[8], showStatus=False)
aDoc.saveDocument("### %s\n\n" % self.tr("New Scene"))
aDoc.readDocument(xHandle[8], showStatus=False)
aDoc.writeDocument("### %s\n\n" % self.tr("New Scene"))
aDoc.clearDocument()
elif popCustom:
@@ -311,8 +311,8 @@ class NWProject():
tHandle = self.newFile(self.tr("Title Page"), nwItemClass.NOVEL, nHandle)
self.projTree.setFileItemLayout(tHandle, nwItemLayout.TITLE)
aDoc.openDocument(tHandle, showStatus=False)
aDoc.saveDocument(titlePage)
aDoc.readDocument(tHandle, showStatus=False)
aDoc.writeDocument(titlePage)
aDoc.clearDocument()
# Create chapters and scenes
@@ -331,8 +331,8 @@ class NWProject():
cHandle = self.newFile(chTitle, nwItemClass.NOVEL, pHandle)
self.projTree.setFileItemLayout(cHandle, nwItemLayout.CHAPTER)
aDoc.openDocument(cHandle, showStatus=False)
aDoc.saveDocument("## %s\n\n" % chTitle)
aDoc.readDocument(cHandle, showStatus=False)
aDoc.writeDocument("## %s\n\n" % chTitle)
aDoc.clearDocument()
# Create chapter scenes
@@ -341,8 +341,8 @@ class NWProject():
scTitle = self.tr("Scene {0}").format(f"{ch+1:d}.{sc+1:d}")
sHandle = self.newFile(scTitle, nwItemClass.NOVEL, pHandle)
aDoc.openDocument(sHandle, showStatus=False)
aDoc.saveDocument("### %s\n\n" % scTitle)
aDoc.readDocument(sHandle, showStatus=False)
aDoc.writeDocument("### %s\n\n" % scTitle)
aDoc.clearDocument()
# Create scenes (no chapters)
@@ -351,8 +351,8 @@ class NWProject():
scTitle = self.tr("Scene {0}").format(f"{sc+1:d}")
sHandle = self.newFile(scTitle, nwItemClass.NOVEL, nHandle)
aDoc.openDocument(sHandle, showStatus=False)
aDoc.saveDocument("### %s\n\n" % scTitle)
aDoc.readDocument(sHandle, showStatus=False)
aDoc.writeDocument("### %s\n\n" % scTitle)
aDoc.clearDocument()
# Finalise
@@ -1404,7 +1404,7 @@ class NWProject():
oParent = None
oClass = None
oLayout = None
if aDoc.openDocument(oHandle, showStatus=False, isOrphan=True) is not None:
if aDoc.readDocument(oHandle, showStatus=False, isOrphan=True) is not None:
oName, oParent, oClass, oLayout = aDoc.getMeta()
if oName:
+5 -2
View File
@@ -278,13 +278,16 @@ class Tokenizer():
if self.theItem is None:
return False
self.theText = ""
if theText is not None:
# If the text is set, just use that
self.theText = theText
else:
# Otherwise, load it from file
theDocument = NWDoc(self.theProject, self.theParent)
self.theText = theDocument.openDocument(theHandle)
theDoc = NWDoc(self.theProject, self.theParent)
theText = theDoc.readDocument(theHandle)
if theText:
self.theText = theText
docSize = len(self.theText)
if docSize > nwConst.MAX_DOCSIZE: