Add project variables for character counts

This commit is contained in:
Veronica Berglyd Olsen
2025-04-29 22:00:53 +02:00
parent 8de6fd25e2
commit bd4a658bd9
8 changed files with 104 additions and 57 deletions
+6 -6
View File
@@ -367,7 +367,7 @@ class NWProject:
# Often, the index needs to be rebuilt when updating format
self._index.rebuild()
self.updateWordCounts()
self.updateCounts()
self._session.startSession()
self.setProjectChanged(False)
self._valid = True
@@ -397,7 +397,7 @@ class NWProject:
else:
self._data.incSaveCount()
self.updateWordCounts()
self.updateCounts()
self.countStatus()
xmlWriter = self._storage.getXmlWriter()
@@ -515,10 +515,10 @@ class NWProject:
# Class Methods
##
def updateWordCounts(self) -> None:
"""Update the total word count values."""
novel, notes = self._tree.sumWords()
self._data.setCurrCounts(novel=novel, notes=notes)
def updateCounts(self) -> None:
"""Update the total word and character count values."""
wNovel, wNotes, cNovel, cNotes = self._tree.sumWords()
self._data.setCurrCounts(wNovel=wNovel, wNotes=wNotes, cNovel=cNovel, cNotes=cNotes)
return
def countStatus(self) -> None:
+42 -24
View File
@@ -66,8 +66,8 @@ class NWProjectData:
self._spellLang = None
# Project Dictionaries
self._initCounts = [0, 0]
self._currCounts = [0, 0]
self._initCounts = [0, 0, 0, 0]
self._currCounts = [0, 0, 0, 0]
self._lastHandle: dict[str, str | None] = {
"editor": None,
"viewer": None,
@@ -148,18 +148,18 @@ class NWProjectData:
return self._spellLang
@property
def initCounts(self) -> tuple[int, int]:
"""Return the initial count of words for novel and note
documents.
def initCounts(self) -> tuple[int, int, int, int]:
"""Return the initial count of words and characters for novel
and note documents.
"""
return self._initCounts[0], self._initCounts[1]
return self._initCounts[0], self._initCounts[1], self._initCounts[2], self._initCounts[3]
@property
def currCounts(self) -> tuple[int, int]:
"""Return the current count of words for novel and note
documents.
def currCounts(self) -> tuple[int, int, int, int]:
"""Return the current count of words and characters for novel
and note documents.
"""
return self._currCounts[0], self._currCounts[1]
return self._currCounts[0], self._currCounts[1], self._currCounts[2], self._currCounts[3]
@property
def lastHandle(self) -> dict[str, str | None]:
@@ -301,22 +301,40 @@ class NWProjectData:
self._project.setProjectChanged(True)
return
def setInitCounts(self, novel: Any = None, notes: Any = None) -> None:
"""Set the word count totals for novel and note files."""
if novel is not None:
self._initCounts[0] = checkInt(novel, 0)
self._currCounts[0] = checkInt(novel, 0)
if notes is not None:
self._initCounts[1] = checkInt(notes, 0)
self._currCounts[1] = checkInt(notes, 0)
def setInitCounts(
self, wNovel: Any = None, wNotes: Any = None, cNovel: Any = None, cNotes: Any = None
) -> None:
"""Set the count totals for novel and note files."""
if wNovel is not None:
count = checkInt(wNovel, 0)
self._initCounts[0] = count
self._currCounts[0] = count
if wNotes is not None:
count = checkInt(wNotes, 0)
self._initCounts[1] = count
self._currCounts[1] = count
if cNovel is not None:
count = checkInt(cNovel, 0)
self._initCounts[2] = count
self._currCounts[2] = count
if cNotes is not None:
count = checkInt(cNotes, 0)
self._initCounts[3] = count
self._currCounts[3] = count
return
def setCurrCounts(self, novel: Any = None, notes: Any = None) -> None:
"""Set the word count totals for novel and note files."""
if novel is not None:
self._currCounts[0] = checkInt(novel, 0)
if notes is not None:
self._currCounts[1] = checkInt(notes, 0)
def setCurrCounts(
self, wNovel: Any = None, wNotes: Any = None, cNovel: Any = None, cNotes: Any = None
) -> None:
"""Set the count totals for novel and note files."""
if wNovel is not None:
self._currCounts[0] = checkInt(wNovel, 0)
if wNotes is not None:
self._currCounts[1] = checkInt(wNotes, 0)
if cNovel is not None:
self._currCounts[2] = checkInt(cNovel, 0)
if cNotes is not None:
self._currCounts[3] = checkInt(cNotes, 0)
return
def setAutoReplace(self, value: dict) -> None:
+17 -7
View File
@@ -46,7 +46,7 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
FILE_VERSION = "1.5" # The current project file format version
FILE_REVISION = "4" # The current project file format revision
FILE_REVISION = "5" # The current project file format revision
HEX_VERSION = 0x0105
NUM_VERSION = {
@@ -109,6 +109,8 @@ class ProjectXMLReader:
Rev 3: Added TEMPLATE class. 2.3.
Rev 4: Added shape attribute to status and importance entry
nodes. 2.5.
Rev 5: Added novelChars and notesChars attributes to content
node. 2.7 RC 1.
"""
def __init__(self, path: str | Path) -> None:
@@ -286,9 +288,9 @@ class ProjectXMLReader:
elif xItem.tag == "spellLang": # Changed to spellChecking in 1.5
data.setSpellLang(xItem.text)
elif xItem.tag == "novelWordCount": # Moved to content attribute in 1.5
data.setInitCounts(novel=xItem.text)
data.setInitCounts(wNovel=xItem.text)
elif xItem.tag == "notesWordCount": # Moved to content attribute in 1.5
data.setInitCounts(notes=xItem.text)
data.setInitCounts(wNotes=xItem.text)
return
@@ -298,8 +300,13 @@ class ProjectXMLReader:
"""Parse the content section of the XML file."""
logger.debug("Parsing <content> section")
data.setInitCounts(novel=xSection.attrib.get("novelWords", None)) # Moved in 1.5
data.setInitCounts(notes=xSection.attrib.get("notesWords", None)) # Moved in 1.5
# Moved in 1.5
data.setInitCounts(
wNovel=xSection.attrib.get("novelWords", None),
wNotes=xSection.attrib.get("notesWords", None),
cNovel=xSection.attrib.get("novelChars", None),
cNotes=xSection.attrib.get("notesChars", None),
)
for xItem in xSection:
if xItem.tag != "item":
@@ -527,10 +534,13 @@ class ProjectXMLWriter:
self._packSingleValue(xImport, "entry", label, attrib=attrib)
# Save Tree Content
counts = data.currCounts
contAttr = {
"items": str(len(content)),
"novelWords": str(data.currCounts[0]),
"notesWords": str(data.currCounts[1]),
"novelWords": str(counts[0]),
"notesWords": str(counts[1]),
"novelChars": str(counts[2]),
"notesChars": str(counts[3]),
}
xContent = ET.SubElement(xRoot, "content", attrib=contAttr)
+2 -2
View File
@@ -79,8 +79,8 @@ class NWSessionLog:
return False
now = time()
iNovel, iNotes = self._project.data.initCounts
cNovel, cNotes = self._project.data.currCounts
iNovel, iNotes, _, _ = self._project.data.initCounts
cNovel, cNotes, _, _ = self._project.data.currCounts
iTotal = iNovel + iNotes
wDiff = cNovel + cNotes - iTotal
sTime = now - self._start
+9 -5
View File
@@ -410,16 +410,20 @@ class NWTree:
return True
def sumWords(self) -> tuple[int, int]:
"""Loop over all entries and add up the word counts."""
noteWords = 0
def sumWords(self) -> tuple[int, int, int, int]:
"""Loop over all entries and add up the word and char counts."""
novelWords = 0
notesWords = 0
novelChars = 0
notesChars = 0
for item in self._items.values():
if item.itemLayout == nwItemLayout.NOTE:
noteWords += item.wordCount
notesWords += item.wordCount
notesChars += item.charCount
elif item.itemLayout == nwItemLayout.DOCUMENT:
novelWords += item.wordCount
return novelWords, noteWords
novelChars += item.charCount
return novelWords, notesWords, novelChars, notesChars
##
# Tree Item Methods