Skip to content

Commit 2bd1047

Browse files
authored
Minor refactoring and linting (#2536)
2 parents 98cdd4e + 3d55b49 commit 2bd1047

12 files changed

Lines changed: 51 additions & 53 deletions

File tree

.github/workflows/test_mac.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212

1313
jobs:
1414
testMac:
15-
runs-on: macos-13
15+
runs-on: macos-latest
1616
steps:
1717
- name: Python Setup
1818
uses: actions/setup-python@v5

novelWriter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import sys
88

99
try:
10-
import PyQt6.QtCore
11-
import PyQt6.QtGui
10+
import PyQt6.QtCore # noqa: F401
11+
import PyQt6.QtGui # noqa: F401
1212
import PyQt6.QtWidgets # noqa: F401
1313
except Exception:
1414
print("ERROR: Failed to load dependency PyQt6")

novelwriter/core/status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def fromRaw(self, data: list[str]) -> StatusEntry | None:
183183
icon = NWStatus.createIcon(self._height, color, shape)
184184
return StatusEntry(simplified(data[2]), color, theme, shape, icon)
185185
except Exception:
186-
logger.error("Could not parse entry %s", str(data))
186+
logger.error("Could not parse entry %s", data)
187187
return None
188188

189189
def refreshIcons(self) -> None:

novelwriter/core/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __getitem__(self, tHandle: str | None) -> NWItem | None:
8989
"""
9090
if tHandle and tHandle in self._items:
9191
return self._items[tHandle]
92-
logger.error("No tree item with handle '%s'", str(tHandle))
92+
logger.error("No tree item with handle '%s'", tHandle)
9393
return None
9494

9595
def __contains__(self, tHandle: str) -> bool:

novelwriter/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def exceptionHandler(exType: type, exValue: BaseException, exTrace: TracebackTyp
171171

172172
from PyQt6.QtWidgets import QApplication
173173

174-
logger.critical("%s: %s", exType.__name__, str(exValue))
174+
logger.critical("%s: %s", exType.__name__, exValue)
175175
print_tb(exTrace)
176176

177177
try:

novelwriter/gui/doceditor.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
QtAlignCenterTop, QtAlignJustify, QtAlignLeft, QtAlignLeftTop,
7878
QtAlignRight, QtImCursorRectangle, QtKeepAnchor, QtModCtrl, QtModNone,
7979
QtModShift, QtMouseLeft, QtMoveAnchor, QtMoveLeft, QtMoveRight,
80-
QtScrollAlwaysOff, QtScrollAsNeeded, QtTransparent
80+
QtScrollAlwaysOff, QtScrollAsNeeded, QtSelectBlock, QtSelectDocument,
81+
QtSelectWord, QtTransparent
8182
)
8283

8384
logger = logging.getLogger(__name__)
@@ -672,7 +673,7 @@ def toggleSpellCheck(self, state: bool | None) -> None:
672673
self.spellCheckStateChanged.emit(state)
673674
self.spellCheckDocument()
674675

675-
logger.debug("Spell check is set to '%s'", str(state))
676+
logger.debug("Spell check is set to '%s'", state)
676677

677678
def spellCheckDocument(self) -> None:
678679
"""Rerun the highlighter to update spell checking status of the
@@ -731,9 +732,9 @@ def docAction(self, action: nwDocAction) -> bool:
731732
elif action == nwDocAction.D_QUOTE:
732733
self._wrapSelection(CONFIG.fmtDQuoteOpen, CONFIG.fmtDQuoteClose)
733734
elif action == nwDocAction.SEL_ALL:
734-
self._makeSelection(QTextCursor.SelectionType.Document)
735+
self._makeSelection(QtSelectDocument)
735736
elif action == nwDocAction.SEL_PARA:
736-
self._makeSelection(QTextCursor.SelectionType.BlockUnderCursor)
737+
self._makeSelection(QtSelectBlock)
737738
elif action == nwDocAction.BLOCK_H1:
738739
self._formatBlock(nwDocAction.BLOCK_H1)
739740
elif action == nwDocAction.BLOCK_H2:
@@ -785,7 +786,7 @@ def docAction(self, action: nwDocAction) -> bool:
785786
elif action == nwDocAction.SC_SUB:
786787
self._wrapSelection(nwShortcode.SUB_O, nwShortcode.SUB_C)
787788
else:
788-
logger.debug("Unknown or unsupported document action '%s'", str(action))
789+
logger.debug("Unknown or unsupported document action '%s'", action)
789790
self._allowAutoReplace(True)
790791
return False
791792

@@ -1174,13 +1175,9 @@ def _openContextMenu(self, pos: QPoint) -> None:
11741175
action = qtAddAction(ctxMenu, self.tr("Select All"))
11751176
action.triggered.connect(qtLambda(self.docAction, nwDocAction.SEL_ALL))
11761177
action = qtAddAction(ctxMenu, self.tr("Select Word"))
1177-
action.triggered.connect(qtLambda(
1178-
self._makePosSelection, QTextCursor.SelectionType.WordUnderCursor, pos,
1179-
))
1178+
action.triggered.connect(qtLambda(self._makePosSelection, QtSelectWord, pos))
11801179
action = qtAddAction(ctxMenu, self.tr("Select Paragraph"))
1181-
action.triggered.connect(qtLambda(
1182-
self._makePosSelection, QTextCursor.SelectionType.BlockUnderCursor, pos
1183-
))
1180+
action.triggered.connect(qtLambda(self._makePosSelection, QtSelectBlock, pos))
11841181

11851182
# Spell Checking
11861183
if SHARED.project.data.spellCheck:
@@ -1750,7 +1747,7 @@ def _processBlockFormat(
17501747
elif action == nwDocAction.BLOCK_TXT:
17511748
text = temp
17521749
else:
1753-
logger.error("Unknown or unsupported block format requested: '%s'", str(action))
1750+
logger.error("Unknown or unsupported block format requested: '%s'", action)
17541751
return nwDocAction.NO_ACTION, "", 0
17551752

17561753
return action, text, offset
@@ -1760,7 +1757,7 @@ def _formatBlock(self, action: nwDocAction) -> bool:
17601757
cursor = self.textCursor()
17611758
block = cursor.block()
17621759
if not block.isValid():
1763-
logger.debug("Invalid block selected for action '%s'", str(action))
1760+
logger.debug("Invalid block selected for action '%s'", action)
17641761
return False
17651762

17661763
action, text, offset = self._processBlockFormat(action, block.text())
@@ -1770,7 +1767,7 @@ def _formatBlock(self, action: nwDocAction) -> bool:
17701767
pos = cursor.position()
17711768

17721769
cursor.beginEditBlock()
1773-
self._makeSelection(QTextCursor.SelectionType.BlockUnderCursor, cursor)
1770+
self._makeSelection(QtSelectBlock, cursor)
17741771
cursor.insertText(text)
17751772
cursor.endEditBlock()
17761773

@@ -1798,7 +1795,7 @@ def _iterFormatBlocks(self, action: nwDocAction) -> bool:
17981795
if pAction != nwDocAction.NO_ACTION and blockText.strip():
17991796
action = pAction # First block decides further actions
18001797
cursor.setPosition(block.position())
1801-
self._makeSelection(QTextCursor.SelectionType.BlockUnderCursor, cursor)
1798+
self._makeSelection(QtSelectBlock, cursor)
18021799
cursor.insertText(text)
18031800
toggle = False
18041801

@@ -1818,7 +1815,7 @@ def _removeInParLineBreaks(self) -> None:
18181815
"""Strip line breaks within paragraphs in the selected text."""
18191816
cursor = self.textCursor()
18201817
if not cursor.hasSelection():
1821-
cursor.select(QTextCursor.SelectionType.Document)
1818+
cursor.select(QtSelectDocument)
18221819

18231820
rS = 0
18241821
rE = self._qDocument.characterCount()
@@ -2035,10 +2032,10 @@ def _makeSelection(
20352032
cursor.clearSelection()
20362033
cursor.select(mode)
20372034

2038-
if mode == QTextCursor.SelectionType.WordUnderCursor:
2035+
if mode == QtSelectWord:
20392036
cursor = self._autoSelect()
20402037

2041-
elif mode == QTextCursor.SelectionType.BlockUnderCursor:
2038+
elif mode == QtSelectBlock:
20422039
# This selection mode also selects the preceding paragraph
20432040
# separator, which we want to avoid.
20442041
posS = cursor.selectionStart()

novelwriter/gui/docviewer.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
from novelwriter.gui.theme import STYLES_MIN_TOOLBUTTON
5454
from novelwriter.types import (
5555
QtAlignCenterTop, QtKeepAnchor, QtMouseLeft, QtMoveAnchor,
56-
QtScrollAlwaysOff, QtScrollAsNeeded
56+
QtScrollAlwaysOff, QtScrollAsNeeded, QtSelectBlock, QtSelectDocument,
57+
QtSelectWord
5758
)
5859

5960
logger = logging.getLogger(__name__)
@@ -287,11 +288,11 @@ def docAction(self, action: nwDocAction) -> bool:
287288
elif action == nwDocAction.COPY:
288289
self.copy()
289290
elif action == nwDocAction.SEL_ALL:
290-
self._makeSelection(QTextCursor.SelectionType.Document)
291+
self._makeSelection(QtSelectDocument)
291292
elif action == nwDocAction.SEL_PARA:
292-
self._makeSelection(QTextCursor.SelectionType.BlockUnderCursor)
293+
self._makeSelection(QtSelectBlock)
293294
else:
294-
logger.debug("Unknown or unsupported document action '%s'", str(action))
295+
logger.debug("Unknown or unsupported document action '%s'", action)
295296
return False
296297
return True
297298

@@ -400,14 +401,10 @@ def _openContextMenu(self, point: QPoint) -> None:
400401
action.triggered.connect(qtLambda(self.docAction, nwDocAction.SEL_ALL))
401402

402403
action = qtAddAction(ctxMenu, self.tr("Select Word"))
403-
action.triggered.connect(qtLambda(
404-
self._makePosSelection, QTextCursor.SelectionType.WordUnderCursor, point
405-
))
404+
action.triggered.connect(qtLambda(self._makePosSelection, QtSelectWord, point))
406405

407406
action = qtAddAction(ctxMenu, self.tr("Select Paragraph"))
408-
action.triggered.connect(qtLambda(
409-
self._makePosSelection, QTextCursor.SelectionType.BlockUnderCursor, point
410-
))
407+
action.triggered.connect(qtLambda(self._makePosSelection, QtSelectBlock, point))
411408

412409
# Open the context menu
413410
if viewport := self.viewport():
@@ -466,7 +463,7 @@ def _makeSelection(self, selType: QTextCursor.SelectionType) -> None:
466463
cursor.clearSelection()
467464
cursor.select(selType)
468465

469-
if selType == QTextCursor.SelectionType.BlockUnderCursor:
466+
if selType == QtSelectBlock:
470467
# This selection mode also selects the preceding paragraph
471468
# separator, which we want to avoid.
472469
posS = cursor.selectionStart()

novelwriter/gui/outline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def _loadHeaderState(self) -> None:
586586
try:
587587
for name, (hidden, width) in colState.items():
588588
if name not in nwOutline.__members__:
589-
logger.warning("Ignored unknown outline column '%s'", str(name))
589+
logger.warning("Ignored unknown outline column '%s'", name)
590590
continue
591591
tmpOrder.append(nwOutline[name])
592592
tmpHidden[nwOutline[name]] = hidden

novelwriter/types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,14 @@
109109

110110
QtKeepAnchor = QTextCursor.MoveMode.KeepAnchor
111111
QtMoveAnchor = QTextCursor.MoveMode.MoveAnchor
112+
112113
QtMoveLeft = QTextCursor.MoveOperation.Left
113114
QtMoveRight = QTextCursor.MoveOperation.Right
114115

116+
QtSelectWord = QTextCursor.SelectionType.WordUnderCursor
117+
QtSelectBlock = QTextCursor.SelectionType.BlockUnderCursor
118+
QtSelectDocument = QTextCursor.SelectionType.Document
119+
115120
QtImCursorRectangle = Qt.InputMethodQuery.ImCursorRectangle
116121

117122
# Size Policy

tests/test_gui/test_gui_doceditor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
from novelwriter.text.counting import standardCounter
4343
from novelwriter.types import (
4444
QtAlignJustify, QtAlignLeft, QtKeepAnchor, QtModCtrl, QtModNone,
45-
QtMouseLeft, QtMoveAnchor, QtMoveRight, QtScrollAlwaysOff, QtScrollAsNeeded
45+
QtMouseLeft, QtMoveAnchor, QtMoveRight, QtScrollAlwaysOff,
46+
QtScrollAsNeeded, QtSelectDocument, QtSelectWord
4647
)
4748

4849
from tests.mocked import causeOSError
@@ -58,7 +59,7 @@ def getMenuForPos(editor: GuiDocEditor, pos: int, select: bool = False) -> QMenu
5859
cursor = editor.textCursor()
5960
cursor.setPosition(pos)
6061
if select:
61-
cursor.select(QTextCursor.SelectionType.WordUnderCursor)
62+
cursor.select(QtSelectWord)
6263
editor.setTextCursor(cursor)
6364
editor._openContextFromCursor()
6465
for obj in editor.children():
@@ -1239,7 +1240,7 @@ def testGuiEditor_TextManipulation(qtbot, nwGUI, projPath, ipsumText, mockRnd):
12391240
docEditor.setCursorPosition(45)
12401241
assert len(docEditor._selectedBlocks(cursor)) == 0
12411242

1242-
cursor.select(QTextCursor.SelectionType.Document)
1243+
cursor.select(QtSelectDocument)
12431244
assert len(docEditor._selectedBlocks(cursor)) == 15
12441245

12451246
# Remove All
@@ -2093,7 +2094,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum):
20932094

20942095
# Select the Word "est"
20952096
docEditor.setCursorPosition(663)
2096-
docEditor._makeSelection(QTextCursor.SelectionType.WordUnderCursor)
2097+
docEditor._makeSelection(QtSelectWord)
20972098
cursor = docEditor.textCursor()
20982099
assert cursor.selectedText() == "est"
20992100

@@ -2223,7 +2224,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum):
22232224
# Close search and select "est" again
22242225
docSearch.cancelSearch.activate(QAction.ActionEvent.Trigger)
22252226
docEditor.setCursorPosition(663)
2226-
docEditor._makeSelection(QTextCursor.SelectionType.WordUnderCursor)
2227+
docEditor._makeSelection(QtSelectWord)
22272228
cursor = docEditor.textCursor()
22282229
assert cursor.selectedText() == "est"
22292230

0 commit comments

Comments
 (0)