Make some minor improvements to the main init file

This commit is contained in:
Veronica Berglyd Olsen
2025-01-20 22:19:24 +01:00
parent 8e4127fbb1
commit b4672759f8
2 changed files with 40 additions and 32 deletions
+34 -24
View File
@@ -42,7 +42,7 @@ if TYPE_CHECKING: # pragma: no cover
# ============ # ============
__package__ = "novelwriter" __package__ = "novelwriter"
__copyright__ = "Copyright 20182024, Veronica Berglyd Olsen" __copyright__ = "Copyright 2018-2025 Veronica Berglyd Olsen"
__license__ = "GPLv3" __license__ = "GPLv3"
__author__ = "Veronica Berglyd Olsen" __author__ = "Veronica Berglyd Olsen"
__maintainer__ = "Veronica Berglyd Olsen" __maintainer__ = "Veronica Berglyd Olsen"
@@ -65,20 +65,20 @@ CONFIG = Config()
SHARED = SharedData() SHARED = SharedData()
# ANSI Colours # ANSI Colours
C_RED = "\033[91m" RED = "\033[91m"
C_GREEN = "\033[92m" GREEN = "\033[92m"
C_YELLOW = "\033[93m" YELLOW = "\033[93m"
C_BLUE = "\033[94m" BLUE = "\033[94m"
C_WHITE = "\033[97m" WHITE = "\033[97m"
C_END = "\033[0m" END = "\033[0m"
# Log Formats # Log Format Components
L_TIME = "[{asctime:}]" TIME = "[{asctime:}]"
L_FILE = "{filename:>18}" FILE = "{filename:>18}"
L_LINE = "{lineno:<4d}" LINE = "{lineno:<4d}"
L_LVLP = "{levelname:8}" LVLP = "{levelname:8}"
L_LVLC = "{levelname:17}" LVLC = "{levelname:17}"
L_TEXT = "{message:}" TEXT = "{message:}"
def main(sysArgs: list | None = None) -> GuiMain | None: def main(sysArgs: list | None = None) -> GuiMain | None:
@@ -104,11 +104,19 @@ def main(sysArgs: list | None = None) -> GuiMain | None:
f"novelWriter {__version__} ({__date__})\n" f"novelWriter {__version__} ({__date__})\n"
f"{__copyright__}\n" f"{__copyright__}\n"
"\n" "\n"
"This program is free software: you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation, either version 3 of the License, or\n"
"(at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n" "This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public Licence for more details.\n" "GNU General Public Licence for more details.\n"
"\n" "\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program. If not, see <https://www.gnu.org/licenses/>.\n"
"\n"
"Usage:\n" "Usage:\n"
" -h, --help Print this message.\n" " -h, --help Print this message.\n"
" -v, --version Print program version and exit.\n" " -v, --version Print program version and exit.\n"
@@ -167,21 +175,21 @@ def main(sysArgs: list | None = None) -> GuiMain | None:
if fmtFlags & 0b01: if fmtFlags & 0b01:
# This will overwrite the default level names, and also ensure that # This will overwrite the default level names, and also ensure that
# they can be converted back to integer levels # they can be converted back to integer levels
logging.addLevelName(logging.DEBUG, f"{C_BLUE}DEBUG{C_END}") logging.addLevelName(logging.DEBUG, f"{BLUE}DEBUG{END}")
logging.addLevelName(logging.INFO, f"{C_GREEN}INFO{C_END}") logging.addLevelName(logging.INFO, f"{GREEN}INFO{END}")
logging.addLevelName(logging.WARNING, f"{C_YELLOW}WARNING{C_END}") logging.addLevelName(logging.WARNING, f"{YELLOW}WARNING{END}")
logging.addLevelName(logging.ERROR, f"{C_RED}ERROR{C_END}") logging.addLevelName(logging.ERROR, f"{RED}ERROR{END}")
logging.addLevelName(logging.CRITICAL, f"{C_RED}CRITICAL{C_END}") logging.addLevelName(logging.CRITICAL, f"{RED}CRITICAL{END}")
# Determine Log Format # Determine Log Format
if fmtFlags == 0b00: if fmtFlags == 0b00:
logFmt = f"{L_LVLP} {L_TEXT}" logFmt = f"{LVLP} {TEXT}"
elif fmtFlags == 0b01: elif fmtFlags == 0b01:
logFmt = f"{L_LVLC} {L_TEXT}" logFmt = f"{LVLC} {TEXT}"
elif fmtFlags == 0b10: elif fmtFlags == 0b10:
logFmt = f"{L_TIME} {L_FILE}:{L_LINE} {L_LVLP} {L_TEXT}" logFmt = f"{TIME} {FILE}:{LINE} {LVLP} {TEXT}"
elif fmtFlags == 0b11: elif fmtFlags == 0b11:
logFmt = f"{L_TIME} {C_BLUE}{L_FILE}{C_END}:{C_WHITE}{L_LINE}{C_END} {L_LVLC} {L_TEXT}" logFmt = f"{TIME} {BLUE}{FILE}{END}:{WHITE}{LINE}{END} {LVLC} {TEXT}"
# Setup Logging # Setup Logging
pkgLogger = logging.getLogger(__package__) pkgLogger = logging.getLogger(__package__)
@@ -272,7 +280,9 @@ def main(sysArgs: list | None = None) -> GuiMain | None:
def _createApp(style: str) -> QApplication: def _createApp(style: str) -> QApplication:
"""Create the app.""" """Create the app. This is done in a function to make it easier to
block app creation during testing.
"""
app = QApplication([CONFIG.appName, (f"-style={style}")]) app = QApplication([CONFIG.appName, (f"-style={style}")])
app.setApplicationName(CONFIG.appName) app.setApplicationName(CONFIG.appName)
app.setApplicationVersion(__version__) app.setApplicationVersion(__version__)
+6 -8
View File
@@ -30,8 +30,8 @@ import pytest
from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QApplication
from novelwriter import ( from novelwriter import (
C_BLUE, C_END, C_WHITE, CONFIG, L_FILE, L_LINE, L_LVLC, L_LVLP, L_TEXT, BLUE, CONFIG, END, FILE, LINE, LVLC, LVLP, TEXT, TIME, WHITE, _createApp,
L_TIME, _createApp, logger, main logger, main
) )
from tests.tools import clearLogHandlers from tests.tools import clearLogHandlers
@@ -115,16 +115,14 @@ def testBaseInit_Options(monkeypatch, fncPath):
main(["--info", "--color", f"--config={fncPath}", f"--data={fncPath}"]) main(["--info", "--color", f"--config={fncPath}", f"--data={fncPath}"])
assert ex.value.code == 0 assert ex.value.code == 0
assert logger.getEffectiveLevel() == logging.INFO assert logger.getEffectiveLevel() == logging.INFO
assert getFormat() == f"{L_LVLC} {L_TEXT}" assert getFormat() == f"{LVLC} {TEXT}"
clearLogHandlers() clearLogHandlers()
with pytest.raises(SystemExit) as ex: with pytest.raises(SystemExit) as ex:
main(["--debug", "--color", f"--config={fncPath}", f"--data={fncPath}"]) main(["--debug", "--color", f"--config={fncPath}", f"--data={fncPath}"])
assert ex.value.code == 0 assert ex.value.code == 0
assert logger.getEffectiveLevel() == logging.DEBUG assert logger.getEffectiveLevel() == logging.DEBUG
assert getFormat() == ( assert getFormat() == f"{TIME} {BLUE}{FILE}{END}:{WHITE}{LINE}{END} {LVLC} {TEXT}"
f"{L_TIME} {C_BLUE}{L_FILE}{C_END}:{C_WHITE}{L_LINE}{C_END} {L_LVLC} {L_TEXT}"
)
# Log Levels wo/Color # Log Levels wo/Color
clearLogHandlers() clearLogHandlers()
@@ -132,14 +130,14 @@ def testBaseInit_Options(monkeypatch, fncPath):
main(["--info", f"--config={fncPath}", f"--data={fncPath}"]) main(["--info", f"--config={fncPath}", f"--data={fncPath}"])
assert ex.value.code == 0 assert ex.value.code == 0
assert logger.getEffectiveLevel() == logging.INFO assert logger.getEffectiveLevel() == logging.INFO
assert getFormat() == f"{L_LVLP} {L_TEXT}" assert getFormat() == f"{LVLP} {TEXT}"
clearLogHandlers() clearLogHandlers()
with pytest.raises(SystemExit) as ex: with pytest.raises(SystemExit) as ex:
main(["--debug", f"--config={fncPath}", f"--data={fncPath}"]) main(["--debug", f"--config={fncPath}", f"--data={fncPath}"])
assert ex.value.code == 0 assert ex.value.code == 0
assert logger.getEffectiveLevel() == logging.DEBUG assert logger.getEffectiveLevel() == logging.DEBUG
assert getFormat() == f"{L_TIME} {L_FILE}:{L_LINE} {L_LVLP} {L_TEXT}" assert getFormat() == f"{TIME} {FILE}:{LINE} {LVLP} {TEXT}"
# Help and Version # Help and Version
with pytest.raises(SystemExit) as ex: with pytest.raises(SystemExit) as ex: