Fix linting errors in the main code

This commit is contained in:
Veronica Berglyd Olsen
2025-04-07 17:18:43 +02:00
parent dab48d43a5
commit 8ed3bd889d
66 changed files with 434 additions and 344 deletions
+11 -7
View File
@@ -29,21 +29,25 @@ import re
import xml.etree.ElementTree as ET
from datetime import datetime
from pathlib import Path
from typing import NamedTuple
from typing import TYPE_CHECKING, NamedTuple
from zipfile import ZIP_DEFLATED, ZipFile
from PyQt6.QtCore import QMargins, QSize
from PyQt6.QtGui import QColor
from novelwriter import __version__
from novelwriter.common import firstFloat, xmlElement, xmlSubElem
from novelwriter.constants import nwHeadFmt, nwStyles
from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
from novelwriter.formats.tokenizer import Tokenizer
from novelwriter.types import QtHexRgb
if TYPE_CHECKING:
from pathlib import Path
from PyQt6.QtGui import QColor
from novelwriter.core.project import NWProject
logger = logging.getLogger(__name__)
# RegEx
@@ -1052,9 +1056,9 @@ class ToDocX(Tokenizer):
class DocXParagraph:
__slots__ = (
"_content", "_style", "_textAlign",
"_topMargin", "_bottomMargin", "_leftMargin", "_rightMargin",
"_indentFirst", "_breakBefore", "_breakAfter", "_footnoteRef",
"_bottomMargin", "_breakAfter", "_breakBefore", "_content",
"_footnoteRef", "_indentFirst", "_leftMargin", "_rightMargin",
"_style", "_textAlign", "_topMargin",
)
def __init__(self) -> None:
+7 -5
View File
@@ -26,16 +26,20 @@ from __future__ import annotations
import json
import logging
from pathlib import Path
from time import time
from typing import TYPE_CHECKING
from novelwriter.common import formatTimeStamp
from novelwriter.constants import nwHtmlUnicode, nwStyles
from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape
from novelwriter.formats.tokenizer import Tokenizer
from novelwriter.types import FONT_STYLE, FONT_WEIGHTS, QtHexRgb
if TYPE_CHECKING:
from pathlib import Path
from novelwriter.core.project import NWProject
logger = logging.getLogger(__name__)
# Each opener tag, with the id of its corresponding closer and tag format
@@ -311,10 +315,8 @@ class ToHtml(Tokenizer):
def replaceTabs(self, nSpaces: int = 8, spaceChar: str = " ") -> None:
"""Replace tabs with spaces in the html."""
pages = []
tabSpace = spaceChar*nSpaces
for aLine in self._pages:
pages.append(aLine.replace("\t", tabSpace))
pages = [aLine.replace("\t", tabSpace) for aLine in self._pages]
self._pages = pages
return
+12 -11
View File
@@ -28,8 +28,7 @@ import logging
import re
from abc import ABC, abstractmethod
from pathlib import Path
from typing import NamedTuple
from typing import TYPE_CHECKING, NamedTuple
from PyQt6.QtCore import QLocale
from PyQt6.QtGui import QColor, QFont
@@ -40,7 +39,6 @@ from novelwriter.constants import (
nwHeadFmt, nwKeyWords, nwLabels, nwShortcode, nwStats, nwStyles, nwUnicode,
trConst
)
from novelwriter.core.project import NWProject
from novelwriter.enum import nwComment, nwItemLayout
from novelwriter.formats.shared import (
BlockFmt, BlockTyp, T_Block, T_Formats, T_Note, TextDocumentTheme, TextFmt
@@ -48,6 +46,11 @@ from novelwriter.formats.shared import (
from novelwriter.text.comments import processComment
from novelwriter.text.patterns import REGEX_PATTERNS, DialogParser
if TYPE_CHECKING:
from pathlib import Path
from novelwriter.core.project import NWProject
logger = logging.getLogger(__name__)
@@ -72,7 +75,7 @@ HEADINGS = [
BlockTyp.TITLE, BlockTyp.PART, BlockTyp.HEAD1,
BlockTyp.HEAD2, BlockTyp.HEAD3, BlockTyp.HEAD4,
]
SKIP_INDENT = HEADINGS + [BlockTyp.SEP, BlockTyp.SKIP]
SKIP_INDENT = [*HEADINGS, BlockTyp.SEP, BlockTyp.SKIP]
B_EMPTY: T_Block = (BlockTyp.EMPTY, "", "", [], BlockFmt.NONE)
@@ -651,7 +654,7 @@ class Tokenizer(ABC):
tText = aLine[2:].strip()
tType = BlockTyp.HEAD1 if isPlain else BlockTyp.TITLE
sHide = self._hidePart if isPlain else False
if not (isPlain or isNovel and sHide):
if not (isPlain or (isNovel and sHide)):
tStyle |= self._titleStyle
if isNovel:
tType = BlockTyp.PART if isPlain else BlockTyp.TITLE
@@ -1124,12 +1127,10 @@ class Tokenizer(ABC):
temp.append((res.end(0), 0, TextFmt.HRF_E, ""))
# Match Shortcodes
for res in REGEX_PATTERNS.shortcodePlain.finditer(text):
temp.append((
res.start(1), res.end(1),
self._shortCodeFmt.get(res.group(1).lower(), 0),
"",
))
temp.extend(
(res.start(1), res.end(1), self._shortCodeFmt.get(res.group(1).lower(), 0), "")
for res in REGEX_PATTERNS.shortcodePlain.finditer(text)
)
# Match Shortcode w/Values
tHandle = self._handle or ""
+6 -2
View File
@@ -25,13 +25,17 @@ from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING
from novelwriter.constants import nwUnicode
from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
from novelwriter.formats.tokenizer import Tokenizer
if TYPE_CHECKING:
from pathlib import Path
from novelwriter.core.project import NWProject
logger = logging.getLogger(__name__)
+22 -18
View File
@@ -29,10 +29,9 @@ from __future__ import annotations
import logging
import xml.etree.ElementTree as ET
from collections.abc import Sequence
from datetime import datetime
from hashlib import sha256
from pathlib import Path
from typing import TYPE_CHECKING, Final
from zipfile import ZIP_DEFLATED, ZipFile
from PyQt6.QtGui import QColor, QFont
@@ -40,11 +39,16 @@ from PyQt6.QtGui import QColor, QFont
from novelwriter import __version__
from novelwriter.common import xmlElement, xmlIndent, xmlSubElem
from novelwriter.constants import nwHeadFmt, nwStyles
from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, TextFmt, stripEscape
from novelwriter.formats.tokenizer import Tokenizer
from novelwriter.types import FONT_STYLE, QtHexRgb
if TYPE_CHECKING:
from collections.abc import Sequence
from pathlib import Path
from novelwriter.core.project import NWProject
logger = logging.getLogger(__name__)
# Main XML NameSpaces
@@ -1000,11 +1004,11 @@ class ODTParagraphStyle:
exporter. Only the used settings are exposed here to keep the class
minimal and fast.
"""
VALID_ALIGN = ["start", "center", "end", "justify", "inside", "outside", "left", "right"]
VALID_BREAK = ["auto", "column", "page", "even-page", "odd-page", "inherit"]
VALID_LEVEL = ["1", "2", "3", "4"]
VALID_CLASS = ["text", "chapter", "extra"]
VALID_WEIGHT = ["normal", "bold"] + FONT_WEIGHT_NUM
VALID_ALIGN: Final[list[str]] = ["start", "center", "end", "justify", "left", "right"]
VALID_BREAK: Final[list[str]] = ["auto", "page", "even-page", "odd-page", "inherit"]
VALID_LEVEL: Final[list[str]] = ["1", "2", "3", "4"]
VALID_CLASS: Final[list[str]] = ["text", "chapter", "extra"]
VALID_WEIGHT: Final[list[str]] = ["normal", "bold", *FONT_WEIGHT_NUM]
def __init__(self, name: str) -> None:
@@ -1207,9 +1211,9 @@ class ODTParagraphStyle:
def getID(self) -> str:
"""Generate a unique ID from the settings."""
return sha256((
f"Paragraph:Main:{str(self._mAttr)}:"
f"Paragraph:Para:{str(self._pAttr)}:"
f"Paragraph:Text:{str(self._tAttr)}:"
f"Paragraph:Main:{self._mAttr!s}:"
f"Paragraph:Para:{self._pAttr!s}:"
f"Paragraph:Text:{self._tAttr!s}:"
).encode()).hexdigest()
def packXML(self, xParent: ET.Element) -> None:
@@ -1237,13 +1241,13 @@ class ODTTextStyle:
Only the used settings are exposed here to keep the class minimal
and fast.
"""
VALID_WEIGHT = ["normal", "bold"] + FONT_WEIGHT_NUM
VALID_STYLE = ["normal", "italic", "oblique"]
VALID_POS = ["super", "sub"]
VALID_LSTYLE = ["none", "solid"]
VALID_LTYPE = ["single", "double"]
VALID_LWIDTH = ["auto"]
VALID_LCOL = ["font-color"]
VALID_WEIGHT: Final[list[str]] = ["normal", "bold", *FONT_WEIGHT_NUM]
VALID_STYLE: Final[list[str]] = ["normal", "italic", "oblique"]
VALID_POS: Final[list[str]] = ["super", "sub"]
VALID_LSTYLE: Final[list[str]] = ["none", "solid"]
VALID_LTYPE: Final[list[str]] = ["single", "double"]
VALID_LWIDTH: Final[list[str]] = ["auto"]
VALID_LCOL: Final[list[str]] = ["font-color"]
def __init__(self, name: str) -> None:
self._name = name
+6 -2
View File
@@ -25,7 +25,7 @@ from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING
from PyQt6.QtCore import QMarginsF, QSizeF
from PyQt6.QtGui import (
@@ -36,7 +36,6 @@ from PyQt6.QtPrintSupport import QPrinter
from novelwriter import __version__
from novelwriter.constants import nwStyles, nwUnicode
from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
from novelwriter.formats.tokenizer import HEADINGS, Tokenizer
from novelwriter.types import (
@@ -45,6 +44,11 @@ from novelwriter.types import (
QtPropLineHeight, QtTransparent, QtVAlignNormal, QtVAlignSub, QtVAlignSuper
)
if TYPE_CHECKING:
from pathlib import Path
from novelwriter.core.project import NWProject
logger = logging.getLogger(__name__)
T_TextStyle = tuple[QTextBlockFormat, QTextCharFormat]
+6 -2
View File
@@ -26,13 +26,17 @@ from __future__ import annotations
import json
import logging
from pathlib import Path
from time import time
from typing import TYPE_CHECKING
from novelwriter.common import formatTimeStamp
from novelwriter.core.project import NWProject
from novelwriter.formats.tokenizer import Tokenizer
if TYPE_CHECKING:
from pathlib import Path
from novelwriter.core.project import NWProject
logger = logging.getLogger(__name__)