Clean up variables and formatting

This commit is contained in:
Veronica Berglyd Olsen
2024-01-28 00:28:01 +01:00
parent 97b791511f
commit 8e4e6903b1
41 changed files with 1241 additions and 1248 deletions
+2 -6
View File
@@ -235,16 +235,12 @@ class BuildSettings:
def getInt(self, key: str) -> int:
"""Type safe value access for integers."""
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None))[1])
if isinstance(value, (int, float)):
return int(value)
return 0
return int(value) if isinstance(value, (int, float)) else 0
def getFloat(self, key: str) -> float:
"""Type safe value access for floats."""
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None))[1])
if isinstance(value, (int, float)):
return float(value)
return 0.0
return float(value) if isinstance(value, (int, float)) else 0.0
##
# Setters
+19 -22
View File
@@ -279,28 +279,25 @@ class DocDuplicator:
"""Run through a list of items, duplicate them, and copy the
text content if they are documents.
"""
if not items:
return
nHandle = items[0]
hMap: dict[str, str | None] = {t: None for t in items}
for tHandle in items:
newItem = self._project.tree.duplicate(tHandle)
if newItem is None:
return
hMap[tHandle] = newItem.itemHandle
if newItem.itemParent in hMap:
newItem.setParent(hMap[newItem.itemParent])
self._project.tree.updateItemData(newItem.itemHandle)
if newItem.isFileType():
oldDoc = self._project.storage.getDocument(tHandle)
newDoc = self._project.storage.getDocument(newItem.itemHandle)
if newDoc.fileExists():
if items:
nHandle = items[0]
hMap: dict[str, str | None] = {t: None for t in items}
for tHandle in items:
newItem = self._project.tree.duplicate(tHandle)
if newItem is None:
return
newDoc.writeDocument(oldDoc.readDocument() or "")
yield newItem.itemHandle, nHandle
nHandle = None
hMap[tHandle] = newItem.itemHandle
if newItem.itemParent in hMap:
newItem.setParent(hMap[newItem.itemParent])
self._project.tree.updateItemData(newItem.itemHandle)
if newItem.isFileType():
oldDoc = self._project.storage.getDocument(tHandle)
newDoc = self._project.storage.getDocument(newItem.itemHandle)
if newDoc.fileExists():
return
newDoc.writeDocument(oldDoc.readDocument() or "")
yield newItem.itemHandle, nHandle
nHandle = None
return
# END Class DocDuplicator
@@ -313,7 +310,7 @@ class ProjectBuilder:
def __init__(self) -> None:
self._path = None
self.tr = partial(QCoreApplication.translate, "NWProject")
self.tr = partial(QCoreApplication.translate, "ProjectBuilder")
return
@property
+2 -2
View File
@@ -248,8 +248,8 @@ class NWBuildDocument:
def _setupBuild(self, bldObj: Tokenizer) -> dict:
"""Configure the build object."""
# Get Settings
textFont = self._build.getStr("format.textFont")
textSize = self._build.getInt("format.textSize")
textFont = self._build.getStr("format.textFont")
textSize = self._build.getInt("format.textSize")
fontFamily = textFont or CONFIG.textFont
bldFont = QFont(fontFamily, textSize)
+6 -6
View File
@@ -52,7 +52,7 @@ class NWDocument:
def __init__(self, project: NWProject, tHandle: str | None) -> None:
self._project = project
self._project = project
self._item = None # The currently open item
self._handle = None # The handle of the currently open item
@@ -284,12 +284,12 @@ class NWDocument:
"""Parse the document meta tag and return the name, parent,
class and layout meta values.
"""
theName = self._docMeta.get("name", "")
theParent = self._docMeta.get("parent", None)
theClass = self._docMeta.get("class", None)
theLayout = self._docMeta.get("layout", None)
name = self._docMeta.get("name", "")
parent = self._docMeta.get("parent", None)
itemClass = self._docMeta.get("class", None)
itemLayout = self._docMeta.get("layout", None)
return theName, theParent, theClass, theLayout
return name, parent, itemClass, itemLayout
def getError(self) -> str:
"""Return the last recorded exception."""
+4 -4
View File
@@ -122,8 +122,8 @@ class NWIndex:
for nwItem in self._project.tree:
if nwItem.isFileType():
tHandle = nwItem.itemHandle
theDoc = self._project.storage.getDocument(tHandle)
self.scanText(tHandle, theDoc.readDocument() or "", blockSignal=True)
doc = self._project.storage.getDocument(tHandle)
self.scanText(tHandle, doc.readDocument() or "", blockSignal=True)
self._indexBroken = False
SHARED.indexSignalProxy({"event": "buildIndex"})
return
@@ -148,8 +148,8 @@ class NWIndex:
"""
if tHandle and self._project.tree.checkType(tHandle, nwItemType.FILE):
logger.debug("Re-indexing item '%s'", tHandle)
theDoc = self._project.storage.getDocument(tHandle)
self.scanText(tHandle, theDoc.readDocument() or "")
doc = self._project.storage.getDocument(tHandle)
self.scanText(tHandle, doc.readDocument() or "")
return True
return False
+4 -4
View File
@@ -194,12 +194,12 @@ class Tokenizer(ABC):
##
@property
def theResult(self) -> str:
def result(self) -> str:
"""The result of the build process."""
return self._result
@property
def theMarkdown(self) -> list:
def allMarkdown(self) -> list:
"""The combined novelWriter Markdown text."""
return self._allMarkdown
@@ -358,8 +358,8 @@ class Tokenizer(ABC):
return True
def setText(self, tHandle: str, text: str | None = None) -> bool:
"""Set the text for the tokenizer from a handle. If theText is
not set, load it from the file.
"""Set the text for the tokenizer from a handle. If text is not
set, load it from the file.
"""
self._nwItem = self._project.tree[tHandle]
if self._nwItem is None:
+4 -4
View File
@@ -682,12 +682,12 @@ class ToOdt(Tokenizer):
return parName
oStyle.setParentStyleName(parName)
theID = oStyle.getID()
if theID in self._autoPara:
return self._autoPara[theID][0]
pID = oStyle.getID()
if pID in self._autoPara:
return self._autoPara[pID][0]
newName = "P%d" % (len(self._autoPara) + 1)
self._autoPara[theID] = (newName, oStyle)
self._autoPara[pID] = (newName, oStyle)
return newName
+1 -1
View File
@@ -500,7 +500,7 @@ class NWTree:
##
def _setTreeChanged(self, state: bool) -> None:
"""Set the changed flag to theState, and if being set to True,
"""Set the changed flag to state, and if being set to True,
propagate that state change to the parent NWProject class.
"""
self._changed = state