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