Made some the properties of the NWDoc class internal

This commit is contained in:
Veronica K. B. Olsen
2020-09-18 17:10:33 +02:00
parent d7feece887
commit 86bb1fc9a4
4 changed files with 59 additions and 51 deletions
-2
View File
@@ -4,7 +4,6 @@ from nw.core.document import NWDoc
from nw.core.index import NWIndex
from nw.core.project import NWProject
from nw.core.spellcheck import NWSpellCheck, NWSpellEnchant, NWSpellSimple
from nw.core.tokenizer import Tokenizer
from nw.core.tohtml import ToHtml
from nw.core.tools import countWords, numberToRoman, numberToWord
@@ -15,7 +14,6 @@ __all__ = [
"NWSpellCheck",
"NWSpellEnchant",
"NWSpellSimple",
"Tokenizer",
"ToHtml",
"countWords",
"numberToRoman",
+41 -33
View File
@@ -26,11 +26,9 @@
"""
import logging
import nw
from os import path, rename, unlink
from nw.core.item import NWItem
from nw.constants import nwAlert
from nw.common import isHandle
from nw.constants import nwItemLayout, nwItemClass, nwConst
@@ -41,14 +39,14 @@ class NWDoc():
def __init__(self, theProject, theParent):
self.mainConf = nw.CONFIG
self.theProject = theProject
self.theParent = theParent
self.theItem = None
self.docHandle = None
self.fileLoc = None
self.docMeta = ""
# 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 string of the currently open item
# Internal Mapping
self.makeAlert = self.theParent.makeAlert
@@ -62,10 +60,10 @@ class NWDoc():
def clearDocument(self):
"""Clear the document contents.
"""
self.theItem = None
self.docHandle = None
self.fileLoc = None
self.docMeta = ""
self._theItem = None
self._docHandle = None
self._fileLoc = None
self._docMeta = ""
return
def openDocument(self, tHandle, showStatus=True, isOrphan=False):
@@ -78,31 +76,31 @@ class NWDoc():
# Always clear first, since the object will often be reused.
self.clearDocument()
self.docHandle = tHandle
self._docHandle = tHandle
if not isOrphan:
self.theItem = self.theProject.projTree[tHandle]
self._theItem = self.theProject.projTree[tHandle]
else:
self.theItem = None
self._theItem = None
if self.theItem is None and not isOrphan:
if self._theItem is None and not isOrphan:
self.clearDocument()
return None
docFile = self.docHandle+".nwd"
docFile = self._docHandle+".nwd"
logger.debug("Opening document %s" % docFile)
docPath = path.join(self.theProject.projContent, docFile)
self.fileLoc = docPath
self._fileLoc = docPath
theText = ""
self.docMeta = ""
self._docMeta = ""
if path.isfile(docPath):
try:
with open(docPath, mode="r", encoding="utf8") as inFile:
fstLine = inFile.readline()
if fstLine.startswith("%%~ "):
# This is the meta line
self.docMeta = fstLine[4:].strip()
self._docMeta = fstLine[4:].strip()
else:
theText = fstLine
theText += inFile.read()
@@ -120,10 +118,10 @@ class NWDoc():
logger.debug("The requested document does not exist.")
return ""
logger.verbose("DocMeta: '%s'" % self.docMeta)
logger.verbose("DocMeta: '%s'" % self._docMeta)
if showStatus and not isOrphan:
self.theParent.statusBar.setStatus("Opened Document: %s" % self.theItem.itemName)
self.theParent.statusBar.setStatus("Opened Document: %s" % self._theItem.itemName)
return theText
@@ -131,29 +129,29 @@ class NWDoc():
"""Save the document via temp file in case of save failure, and
in any case keep a backup of the file.
"""
if self.docHandle is None:
if self._docHandle is None:
return False
self.theProject.ensureFolderStructure()
docFile = self.docHandle+".nwd"
docFile = self._docHandle+".nwd"
logger.debug("Saving document %s" % docFile)
docPath = path.join(self.theProject.projContent, docFile)
docTemp = path.join(self.theProject.projContent, docFile+"~")
if isinstance(self.theItem, NWItem):
itemPath = self.theProject.projTree.getItemPath(self.docHandle)
if self._theItem is None:
docMeta = ""
else:
itemPath = self.theProject.projTree.getItemPath(self._docHandle)
docMeta = (
"%%~ {handlepath:s}:{itemclass:s}:{itemlayout:s}:{itemname:s}\n"
).format(
handlepath = ":".join(itemPath),
itemclass = self.theItem.itemClass.name,
itemlayout = self.theItem.itemLayout.name,
itemname = self.theItem.itemName,
itemclass = self._theItem.itemClass.name,
itemlayout = self._theItem.itemLayout.name,
itemname = self._theItem.itemName,
)
else:
docMeta = ""
try:
with open(docTemp, mode="w", encoding="utf8") as outFile:
@@ -169,7 +167,7 @@ class NWDoc():
unlink(docPath)
rename(docTemp, docPath)
self.theParent.statusBar.setStatus("Saved Document: %s" % self.theItem.itemName)
self.theParent.statusBar.setStatus("Saved Document: %s" % self._theItem.itemName)
return True
@@ -201,15 +199,25 @@ class NWDoc():
# Getters
##
def getFileLocation(self):
"""Return the file location of the current file.
"""
return self._fileLoc
def getCurrentItem(self):
"""Return a pointer to the currently open item.
"""
return self._theItem
def getMeta(self):
"""Parses the document meta tag and returns the path and name as
a list and a string.
"""
if len(self.docMeta) < 14:
if len(self._docMeta) < 14:
# Not enough information
return "", [], None, None
theMeta = self.docMeta
theMeta = self._docMeta
# Scan for handles
thePath = []
+17 -15
View File
@@ -270,10 +270,12 @@ class GuiDocEditor(QTextEdit):
afTime = time()
logger.debug("Document highlighted in %.3f milliseconds" % (1000*(afTime-bfTime)))
if tLine is None:
self.setCursorPosition(self.nwDocument.theItem.cursorPos)
theItem = self.nwDocument.getCurrentItem()
if tLine is None and theItem is not None:
self.setCursorPosition(theItem.cursorPos)
else:
self.setCursorLine(tLine)
self.lastEdit = time()
self._runCounter()
self.wcTimer.start()
@@ -308,21 +310,20 @@ class GuiDocEditor(QTextEdit):
"""Save the text currently in the editor to the NWDoc object,
and update the NWItem meta data.
"""
if self.nwDocument.theItem is None:
theItem = self.nwDocument.getCurrentItem()
if theItem is None:
return False
docText = self.getText()
cursPos = self.getCursorPosition()
self.nwDocument.theItem.setCharCount(self.charCount)
self.nwDocument.theItem.setWordCount(self.wordCount)
self.nwDocument.theItem.setParaCount(self.paraCount)
self.nwDocument.theItem.setCursorPos(cursPos)
theItem.setCharCount(self.charCount)
theItem.setWordCount(self.wordCount)
theItem.setParaCount(self.paraCount)
theItem.setCursorPos(cursPos)
self.nwDocument.saveDocument(docText)
self.setDocumentChanged(False)
self.theParent.theIndex.scanText(
self.nwDocument.theItem.itemHandle, docText
)
self.theParent.theIndex.scanText(theItem.itemHandle, docText)
return True
@@ -598,7 +599,7 @@ class GuiDocEditor(QTextEdit):
"Location: {fileLoc:s}"
).format(
handle = self.theHandle,
fileLoc = str(self.nwDocument.fileLoc)
fileLoc = str(self.nwDocument.getFileLocation())
))
return
@@ -873,7 +874,8 @@ class GuiDocEditor(QTextEdit):
def _updateCounts(self):
"""Slot for the word counter's finished signal
"""
if self.theHandle is None or self.nwDocument.theItem is None:
theItem = self.nwDocument.getCurrentItem()
if self.theHandle is None or theItem is None:
return
logger.verbose("Updating word count")
@@ -881,9 +883,9 @@ class GuiDocEditor(QTextEdit):
self.charCount = self.wCounter.charCount
self.wordCount = self.wCounter.wordCount
self.paraCount = self.wCounter.paraCount
self.nwDocument.theItem.setCharCount(self.charCount)
self.nwDocument.theItem.setWordCount(self.wordCount)
self.nwDocument.theItem.setParaCount(self.paraCount)
theItem.setCharCount(self.charCount)
theItem.setWordCount(self.wordCount)
theItem.setParaCount(self.paraCount)
self.theParent.treeView.propagateCount(self.theHandle, self.wordCount)
self.theParent.treeView.projectWordCount()
+1 -1
View File
@@ -316,7 +316,7 @@ def testDocMeta(nwDummy, nwLipsum):
assert theClass == nwItemClass.NOVEL
assert theLayout == nwItemLayout.SCENE
aDoc.docMeta = "too_short"
aDoc._docMeta = "too_short"
theMeta, thePath, theClass, theLayout = aDoc.getMeta()
assert theMeta == ""
assert thePath == []