Improvements to saving meta data, and added counting of words, etc

This commit is contained in:
Veronica K. B. Olsen
2019-04-19 23:42:30 +02:00
parent 99257b82cd
commit f68a64503e
6 changed files with 177 additions and 82 deletions
+36 -9
View File
@@ -13,8 +13,9 @@
import logging
import nw
from os import path, mkdir
from lxml import etree, html
from os import path, mkdir, rename, unlink
from nw.tools.analyse import TextAnalysis
logger = logging.getLogger(__name__)
@@ -27,35 +28,61 @@ class NWDoc():
self.mainConf = nw.CONFIG
self.theProject = theProject
self.docHandle = None
self.theItem = None
return
def openDocument(self, tHandle):
self.docHandle = tHandle
self.theItem = self.theProject.getItem(tHandle)
docDir, docFile = self._assemblePath(self.FILE_MN)
logger.debug("Opening document %s" % path.join(docDir,docFile))
dataDir = path.join(self.theProject.projPath, docDir)
docPath = path.join(dataDir, docFile)
if path.isfile(docPath):
with open(docPath,mode="r") as inFile:
return inFile.read()
else:
logger.debug("The requested document does not exist.")
return ""
return None
def saveDocument(self, docHtml):
def saveDocument(self, docText):
if self.docHandle is None:
return False
docDir, docFile = self._assemblePath(self.FILE_MN)
logger.debug("Saving document %s" % path.join(docDir,docFile))
dataDir = path.join(self.theProject.projPath, docDir)
docPath = path.join(dataDir, docFile)
if not path.isdir(dataDir):
mkdir(dataDir)
logger.debug("Created folder %s" % dataDir)
dataPath = path.join(self.theProject.projPath, docDir)
docPath = path.join(dataPath, docFile)
if not path.isdir(dataPath):
mkdir(dataPath)
logger.debug("Created folder %s" % dataPath)
docTemp = path.join(dataPath,docFile[:-3]+"tmp")
docBack = path.join(dataPath,docFile[:-3]+"bak")
if path.isfile(docTemp): unlink(docTemp)
if path.isfile(docBack): rename(docBack,docTemp)
if path.isfile(docPath): rename(docPath,docBack)
with open(docPath,mode="w") as outFile:
outFile.write(docHtml)
outFile.write(docText)
if path.isfile(docTemp): unlink(docTemp)
docAna = TextAnalysis(docText,"en_GB")
wC, sC, pC = docAna.getStats()
self.theItem.setWordCount(wC)
self.theItem.setSentCount(sC)
self.theItem.setParaCount(pC)
return True
##