Make NWDoc class non-reusable
This commit is contained in:
+14
-24
@@ -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))
|
||||
|
||||
+2
-2
@@ -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)
|
||||
|
||||
|
||||
+10
-19
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user