Drop the total word count value in the XML and rewrite how counts are handled

This commit is contained in:
Veronica Berglyd Olsen
2022-11-01 12:21:16 +01:00
parent 6fd7107675
commit 0cfaf4f021
12 changed files with 59 additions and 82 deletions
+34 -34
View File
@@ -544,7 +544,7 @@ class NWProject(QObject):
# Update recent projects
self.mainConf.updateRecentCache(
self.projPath, self._data.name, self._data.getLastCount("total"), time()
self.projPath, self._data.name, sum(self._data.initCounts), time()
)
self.mainConf.saveRecentCache()
@@ -611,7 +611,7 @@ class NWProject(QObject):
# Update recent projects
self.mainConf.updateRecentCache(
self.projPath, self._data.name, self._data.getCurrCount("total"), saveTime
self.projPath, self._data.name, sum(self._data.currCounts), saveTime
)
self.mainConf.saveRecentCache()
@@ -949,10 +949,8 @@ class NWProject(QObject):
def updateWordCounts(self):
"""Update the total word count values.
"""
wcNovel, wcNotes = self._projTree.sumWords()
self._data.setCurrCount(wcNovel, "novel")
self._data.setCurrCount(wcNotes, "notes")
self._data.setCurrCount(wcNovel + wcNotes, "total")
novel, notes = self._projTree.sumWords()
self._data.setCurrCounts(novel=novel, notes=notes)
return
def countStatus(self):
@@ -1211,9 +1209,10 @@ class NWProject(QObject):
isFile = os.path.isfile(sessionFile)
nowTime = time()
lastCount = self._data.getLastCount("total")
currCount = self._data.getCurrCount("total")
sessDiff = currCount - lastCount
iNovel, iNotes = self._data.initCounts
cNovel, cNotes = self._data.currCounts
iTotal = iNovel + iNotes
sessDiff = cNovel + cNotes - iTotal
sessTime = nowTime - self._projOpened
logger.info("The session lasted %d sec and added %d words", int(sessTime), sessDiff)
@@ -1225,8 +1224,8 @@ class NWProject(QObject):
with open(sessionFile, mode="a+", encoding="utf-8") as outFile:
if not isFile:
# It's a new file, so add a header
if lastCount > 0:
outFile.write("# Offset %d\n" % lastCount)
if iTotal > 0:
outFile.write("# Offset %d\n" % iTotal)
outFile.write("# %-17s %-19s %8s %8s %8s\n" % (
"Start Time", "End Time", "Novel", "Notes", "Idle"
))
@@ -1234,8 +1233,8 @@ class NWProject(QObject):
outFile.write("%-19s %-19s %8d %8d %8d\n" % (
formatTimeStamp(self._projOpened),
formatTimeStamp(nowTime),
self._data.getCurrCount("novel"),
self._data.getCurrCount("notes"),
cNovel,
cNotes,
int(idleTime),
))
@@ -1341,8 +1340,8 @@ class NWProjectData:
self._spellLang = None
# Project Dictionaries
self._lastCount: dict[str, int] = {}
self._currCount: dict[str, int] = {}
self._initCounts = [0, 0]
self._currCounts = [0, 0]
self._lastHandle: dict[str, str | None] = {
"editor": None,
"viewer": None,
@@ -1407,6 +1406,14 @@ class NWProjectData:
def spellLang(self):
return self._spellLang
@property
def initCounts(self):
return tuple(self._initCounts)
@property
def currCounts(self):
return tuple(self._currCounts)
@property
def lastHandle(self):
return self._lastHandle
@@ -1461,16 +1468,6 @@ class NWProjectData:
"""
return self._lastHandle.get(component, None)
def getLastCount(self, type):
"""Retrieve the last word count for a given type.
"""
return self._lastCount.get(type, 0)
def getCurrCount(self, type):
"""Retrieve the current word count for a given type.
"""
return self._currCount.get(type, 0)
def getTitleFormat(self, kind):
"""Retrieve the title format string for a given kind of header.
"""
@@ -1580,19 +1577,22 @@ class NWProjectData:
self.theProject.setProjectChanged(True)
return
def setLastCount(self, value, type):
"""Set the word counts from last session.
def setInitCounts(self, novel=None, notes=None):
"""Set the worc count totals for novel and note files.
"""
self._lastCount[type] = checkInt(value, 0)
self.theProject.setProjectChanged(True)
if novel is not None:
self._initCounts[0] = checkInt(novel, 0)
if notes is not None:
self._initCounts[1] = checkInt(notes, 0)
return
def setCurrCount(self, value, type):
"""Set the current word counts.
def setCurrCounts(self, novel=None, notes=None):
"""Set the worc count totals for novel and note files.
"""
if value != self._currCount.get(type, 0):
self._currCount[type] = checkInt(value, 0)
self.theProject.setProjectChanged(True)
if novel is not None:
self._currCounts[0] = checkInt(novel, 0)
if notes is not None:
self._currCounts[1] = checkInt(notes, 0)
return
def setAutoReplace(self, value):
+5 -19
View File
@@ -266,12 +266,10 @@ class ProjectXMLReader:
projData.setSpellCheck(xItem.text)
elif xItem.tag == "spellLang":
projData.setSpellLang(xItem.text)
elif xItem.tag == "totalWordCount":
projData.setLastCount(xItem.text, "total")
elif xItem.tag == "novelWordCount":
projData.setLastCount(xItem.text, "novel")
projData.setInitCounts(novel=xItem.text)
elif xItem.tag == "notesWordCount":
projData.setLastCount(xItem.text, "notes")
projData.setInitCounts(notes=xItem.text)
elif xItem.tag == "status":
self._parseStatusImport(xItem, projData.itemStatus)
elif xItem.tag in ("import", "importance"):
@@ -289,18 +287,7 @@ class ProjectXMLReader:
else: # Pre 1.4 format
projData.setTitleFormat(self._parseDictTagText(xItem))
else:
if self._version < 0x0104:
# Convert some deprecated fields
if xItem.tag == "lastEdited": # Discontinued in 1.4
projData.setLastHandle(xItem.text, "editor")
elif xItem.tag == "lastViewed": # Discontinued in 1.4
projData.setLastHandle(xItem.text, "viewer")
elif xItem.tag == "lastWordCount": # Renamed in 1.4
projData.setLastCount(xItem.text, "total")
else:
logger.warning("Ignored <root/settings/%s> in xml", xItem.tag)
else:
logger.warning("Ignored <root/settings/%s> in xml", xItem.tag)
logger.warning("Ignored <root/settings/%s> in xml", xItem.tag)
return True
@@ -495,9 +482,8 @@ class ProjectXMLWriter:
self._packSingleValue(xSettings, "language", projData.language)
self._packSingleValue(xSettings, "spellCheck", projData.spellCheck)
self._packSingleValue(xSettings, "spellLang", projData.spellLang)
self._packSingleValue(xSettings, "totalWordCount", projData.getCurrCount("total"))
self._packSingleValue(xSettings, "novelWordCount", projData.getCurrCount("novel"))
self._packSingleValue(xSettings, "notesWordCount", projData.getCurrCount("notes"))
self._packSingleValue(xSettings, "novelWordCount", projData.currCounts[0])
self._packSingleValue(xSettings, "notesWordCount", projData.currCounts[1])
self._packDictKeyValue(xSettings, "lastHandle", projData.lastHandle)
self._packDictKeyValue(xSettings, "autoReplace", projData.autoReplace)
self._packDictKeyValue(xSettings, "titleFormat", projData.titleFormat)
+6 -6
View File
@@ -1559,13 +1559,13 @@ class GuiMain(QMainWindow):
self.theProject.updateWordCounts()
if self.mainConf.incNotesWCount:
currWords = self.theProject.data.getCurrCount("total")
diffWords = currWords - self.theProject.data.getLastCount("total")
iTotal = sum(self.theProject.data.initCounts)
cTotal = sum(self.theProject.data.currCounts)
self.mainStatus.setProjectStats(cTotal, cTotal - iTotal)
else:
currWords = self.theProject.data.getCurrCount("novel")
diffWords = currWords - self.theProject.data.getLastCount("novel")
self.mainStatus.setProjectStats(currWords, diffWords)
iNovel, _ = self.theProject.data.initCounts
cNovel, _ = self.theProject.data.currCounts
self.mainStatus.setProjectStats(cNovel, cNovel - iNovel)
return
+3 -4
View File
@@ -1,20 +1,19 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-10-31 22:33:20">
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-11-01 12:20:53">
<project>
<name>Sample Project</name>
<title>Sample Project</title>
<author>Jane Smith</author>
<author>Jay Doh</author>
<saveCount>1407</saveCount>
<saveCount>1409</saveCount>
<autoCount>236</autoCount>
<editTime>69424</editTime>
<editTime>69427</editTime>
</project>
<settings>
<doBackup>False</doBackup>
<language>en_GB</language>
<spellCheck>True</spellCheck>
<spellLang>None</spellLang>
<totalWordCount>1363</totalWordCount>
<novelWordCount>954</novelWordCount>
<notesWordCount>409</notesWordCount>
<lastHandle>
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-10-31 23:08:06">
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-11-01 11:53:22">
<project>
<name>Test Custom</name>
<title>Test Novel</title>
@@ -14,7 +14,6 @@
<language>None</language>
<spellCheck>False</spellCheck>
<spellLang>None</spellLang>
<totalWordCount>0</totalWordCount>
<novelWordCount>0</novelWordCount>
<notesWordCount>0</notesWordCount>
<lastHandle>
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-10-31 23:08:06">
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-11-01 11:53:22">
<project>
<name>Test Custom</name>
<title>Test Novel</title>
@@ -14,7 +14,6 @@
<language>None</language>
<spellCheck>False</spellCheck>
<spellLang>None</spellLang>
<totalWordCount>0</totalWordCount>
<novelWordCount>0</novelWordCount>
<notesWordCount>0</notesWordCount>
<lastHandle>
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-10-31 23:06:52">
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-11-01 11:53:22">
<project>
<name>New Project</name>
<title>New Novel</title>
@@ -13,7 +13,6 @@
<language>None</language>
<spellCheck>False</spellCheck>
<spellLang>None</spellLang>
<totalWordCount>13</totalWordCount>
<novelWordCount>10</novelWordCount>
<notesWordCount>3</notesWordCount>
<lastHandle>
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-10-31 23:08:06">
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-11-01 11:53:22">
<project>
<name>New Project</name>
<title>None</title>
@@ -12,7 +12,6 @@
<language>None</language>
<spellCheck>False</spellCheck>
<spellLang>None</spellLang>
<totalWordCount>0</totalWordCount>
<novelWordCount>0</novelWordCount>
<notesWordCount>0</notesWordCount>
<lastHandle>
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-10-31 23:08:06">
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-11-01 11:53:22">
<project>
<name>New Project</name>
<title>New Novel</title>
@@ -13,7 +13,6 @@
<language>None</language>
<spellCheck>False</spellCheck>
<spellLang>None</spellLang>
<totalWordCount>9</totalWordCount>
<novelWordCount>9</novelWordCount>
<notesWordCount>0</notesWordCount>
<lastHandle>
@@ -1,19 +1,18 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-10-31 22:53:34">
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-11-01 12:01:37">
<project>
<name>New Project</name>
<title>New Novel</title>
<author>Jane Doe</author>
<saveCount>4</saveCount>
<autoCount>2</autoCount>
<editTime>4</editTime>
<editTime>3</editTime>
</project>
<settings>
<doBackup>True</doBackup>
<language>None</language>
<spellCheck>True</spellCheck>
<spellLang>None</spellLang>
<totalWordCount>163</totalWordCount>
<novelWordCount>136</novelWordCount>
<notesWordCount>27</notesWordCount>
<lastHandle>
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-10-31 22:20:53">
<novelWriterXML appVersion="2.0-rc1" hexVersion="0x020000c1" fileVersion="1.4" timeStamp="2022-11-01 11:53:30">
<project>
<name>New Project</name>
<title>New Novel</title>
@@ -13,7 +13,6 @@
<language>None</language>
<spellCheck>False</spellCheck>
<spellLang>None</spellLang>
<totalWordCount>9</totalWordCount>
<novelWordCount>9</novelWordCount>
<notesWordCount>0</notesWordCount>
<lastHandle>
+3 -4
View File
@@ -1003,8 +1003,8 @@ def testCoreProject_Methods(monkeypatch, mockGUI, tmpDir, fncDir, mockRnd):
assert theProject.tree.handles() == oldOrder
# Session stats
theProject.data.setCurrCount(200, "total")
theProject.data.setLastCount(100, "total")
theProject._data._initCounts = [50, 50]
theProject._data._currCounts = [100, 100]
with monkeypatch.context() as mp:
mp.setattr("os.path.isdir", lambda *a, **k: False)
assert not theProject._appendSessionStats(idleTime=0)
@@ -1019,8 +1019,7 @@ def testCoreProject_Methods(monkeypatch, mockGUI, tmpDir, fncDir, mockRnd):
statsFile = os.path.join(theProject.projMeta, nwFiles.SESS_STATS)
theProject._projOpened = 1600002000
theProject._data.setCurrCount(200, "novel")
theProject._data.setCurrCount(100, "notes")
theProject._data._currCounts = [200, 100]
with monkeypatch.context() as mp:
mp.setattr("novelwriter.core.project.time", lambda: 1600005600)