Also updated the documen class with new project variables

This commit is contained in:
Veronica K. B. Olsen
2020-05-29 23:31:38 +02:00
parent 421c40c288
commit 5d2f6d33b5
4 changed files with 51 additions and 50 deletions
+4 -6
View File
@@ -14,12 +14,10 @@ docs/source/_*
__pycache__ __pycache__
# Sample Project # Sample Project
sample/**/cache sample/cache
sample/**/wordlist.txt sample/meta
sample/**/sessionInfo.log sample/*.bak
sample/**/*.bak sample/*.lock
sample/**/*.json
sample/**/*.lock
# PyTest # PyTest
tests/temp tests/temp
+19 -22
View File
@@ -91,18 +91,17 @@ class NWDoc():
if self.theItem.parHandle == self.theProject.projTree.trashRoot(): if self.theItem.parHandle == self.theProject.projTree.trashRoot():
self.docEditable = False self.docEditable = False
docDir = "content"
docFile = self.docHandle+".nwd" docFile = self.docHandle+".nwd"
self.fileLoc = path.join(docDir, docFile) logger.debug("Opening document %s" % docFile)
logger.debug("Opening document %s" % self.fileLoc)
dataDir = path.join(self.theProject.projPath, docDir) docPath = path.join(self.theProject.projContent, docFile)
docPath = path.join(dataDir, docFile) self.fileLoc = docPath
theText = "" theText = ""
self.docMeta = "" self.docMeta = ""
if path.isfile(docPath): if path.isfile(docPath):
try: try:
with open(docPath,mode="r",encoding="utf8") as inFile: with open(docPath, mode="r", encoding="utf8") as inFile:
fstLine = inFile.readline() fstLine = inFile.readline()
if fstLine.startswith("%%~ "): if fstLine.startswith("%%~ "):
# This is the meta line # This is the meta line
@@ -112,7 +111,7 @@ class NWDoc():
theText += inFile.read() theText += inFile.read()
except Exception as e: except Exception as e:
self.makeAlert(["Failed to open document file.",str(e)], nwAlert.ERROR) self.makeAlert(["Failed to open document file.", str(e)], nwAlert.ERROR)
# Note: Document must be cleared in case of an io error, # Note: Document must be cleared in case of an io error,
# or else the auto-save or save will try to overwrite it # or else the auto-save or save will try to overwrite it
# with an empty file. Return None to alert the caller. # with an empty file. Return None to alert the caller.
@@ -138,25 +137,23 @@ class NWDoc():
if self.docHandle is None or not self.docEditable: if self.docHandle is None or not self.docEditable:
return False return False
docDir = "content" self.theProject.ensureFolderStructure()
docFile = self.docHandle+".nwd" docFile = self.docHandle+".nwd"
logger.debug("Saving document %s" % path.join(docDir, docFile)) logger.debug("Saving document %s" % docFile)
dataPath = path.join(self.theProject.projPath, docDir)
docPath = path.join(dataPath, docFile) docPath = path.join(self.theProject.projContent, docFile)
if not path.isdir(dataPath): docTemp = path.join(self.theProject.projContent, docFile+"~")
mkdir(dataPath)
logger.debug("Created folder %s" % dataPath)
itemPath = self.theProject.projTree.getItemPath(self.docHandle) itemPath = self.theProject.projTree.getItemPath(self.docHandle)
docMeta = "%%~ "+":".join(itemPath)+":"+self.theItem.itemName+"\n" docMeta = "%%~ "+":".join(itemPath)+":"+self.theItem.itemName+"\n"
docTemp = path.join(dataPath, docFile+"~")
try: try:
with open(docTemp,mode="w",encoding="utf8") as outFile: with open(docTemp, mode="w", encoding="utf8") as outFile:
outFile.write(docMeta) outFile.write(docMeta)
outFile.write(docText) outFile.write(docText)
except Exception as e: except Exception as e:
self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR) self.makeAlert(["Could not save document.", str(e)], nwAlert.ERROR)
return False return False
# If we're here, the file was successfully saved, so we can # If we're here, the file was successfully saved, so we can
@@ -173,13 +170,12 @@ class NWDoc():
"""Permanently delete a document source file and its backups """Permanently delete a document source file and its backups
from the project data folder. from the project data folder.
""" """
docDir = "content"
docFile = self.docHandle+".nwd" docFile = self.docHandle+".nwd"
dataPath = path.join(self.theProject.projPath, docDir)
chkList = [] chkList = []
chkList.append(path.join(dataPath, docFile)) chkList.append(path.join(self.theProject.projContent, docFile))
chkList.append(path.join(dataPath, docFile+"~")) chkList.append(path.join(self.theProject.projContent, docFile+"~"))
chkList.append(path.join(dataPath, docFile[:-3]+"bak"))
for chkFile in chkList: for chkFile in chkList:
if path.isfile(chkFile): if path.isfile(chkFile):
try: try:
@@ -188,6 +184,7 @@ class NWDoc():
except Exception as e: except Exception as e:
self.makeAlert(["Could not delete document file.",str(e)], nwAlert.ERROR) self.makeAlert(["Could not delete document file.",str(e)], nwAlert.ERROR)
return False return False
return True return True
## ##
+25 -19
View File
@@ -252,17 +252,10 @@ class NWProject():
# Standard Folders and Files # Standard Folders and Files
# ========================== # ==========================
self.projMeta = path.join(self.projPath, "meta") if not self.ensureFolderStructure():
self.projCache = path.join(self.projPath, "cache") return False
self.projContent = path.join(self.projPath, "content")
self.projDict = path.join(self.projMeta, nwFiles.PROJ_DICT)
if not self._checkFolder(self.projMeta): self.projDict = path.join(self.projMeta, nwFiles.PROJ_DICT)
return False
if not self._checkFolder(self.projCache):
return False
if not self._checkFolder(self.projContent):
return False
# Check for Old Legacy Data # Check for Old Legacy Data
# ========================= # =========================
@@ -473,15 +466,8 @@ class NWProject():
) )
return False return False
self.projMeta = path.join(self.projPath, "meta")
self.projContent = path.join(self.projPath, "content")
saveTime = time() saveTime = time()
if not self.ensureFolderStructure():
if not self._checkFolder(self.projPath):
return False
if not self._checkFolder(self.projMeta):
return False
if not self._checkFolder(self.projContent):
return False return False
logger.debug("Saving project: %s" % self.projPath) logger.debug("Saving project: %s" % self.projPath)
@@ -582,6 +568,26 @@ class NWProject():
self.lockedBy = None self.lockedBy = None
return True return True
def ensureFolderStructure(self):
"""Ensure that all necessary folders exist in the project
folder.
"""
if self.projPath is None or self.projPath == "":
return False
self.projMeta = path.join(self.projPath, "meta")
self.projCache = path.join(self.projPath, "cache")
self.projContent = path.join(self.projPath, "content")
if not self._checkFolder(self.projMeta):
return False
if not self._checkFolder(self.projCache):
return False
if not self._checkFolder(self.projContent):
return False
return True
## ##
# Backup Project # Backup Project
## ##
@@ -1058,7 +1064,7 @@ class NWProject():
def _appendSessionStats(self): def _appendSessionStats(self):
"""Append session statistics to the sessions log file. """Append session statistics to the sessions log file.
""" """
if self.projMeta is None: if not self.ensureFolderStructure():
return False return False
sessionFile = path.join(self.projMeta, nwFiles.SESS_INFO) sessionFile = path.join(self.projMeta, nwFiles.SESS_INFO)
+3 -3
View File
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="0.7.0rc1" hexVersion="0x000700c1" fileVersion="1.1" saveCount="195" autoCount="29" timeStamp="2020-05-29 23:02:40"> <novelWriterXML appVersion="0.7.0rc1" hexVersion="0x000700c1" fileVersion="1.1" saveCount="197" autoCount="29" timeStamp="2020-05-29 23:27:14">
<project> <project>
<name>Sample Project</name> <name>Sample Project</name>
<title>Sample Project</title> <title>Sample Project</title>
@@ -11,7 +11,7 @@
<spellCheck>True</spellCheck> <spellCheck>True</spellCheck>
<autoOutline>True</autoOutline> <autoOutline>True</autoOutline>
<lastEdited>636b6aa9b697b</lastEdited> <lastEdited>636b6aa9b697b</lastEdited>
<lastViewed>ba8a28a246524</lastViewed> <lastViewed>b3e74dbc1f584</lastViewed>
<lastWordCount>914</lastWordCount> <lastWordCount>914</lastWordCount>
<autoReplace> <autoReplace>
<A>B</A> <A>B</A>
@@ -71,7 +71,7 @@
<status>New</status> <status>New</status>
<exported>True</exported> <exported>True</exported>
<layout>PAGE</layout> <layout>PAGE</layout>
<charCount>208</charCount> <charCount>210</charCount>
<wordCount>40</wordCount> <wordCount>40</wordCount>
<paraCount>2</paraCount> <paraCount>2</paraCount>
<cursorPos>213</cursorPos> <cursorPos>213</cursorPos>