Make checking for unknown commandline args configurable in the config module.

Introducing commandline args lead to errors when passing arguments to pytest, which were in turn passed to the code and causing errors because they were not recognised. This change only runs the check for known arguments from the main() routine while other imports won't cause a check.
This commit is contained in:
Rebecca Breu 2021-04-02 11:58:00 +02:00
parent 5be88649f6
commit 4141e9666f
4 changed files with 35 additions and 5 deletions

View file

@ -22,10 +22,9 @@ import sys
from PyQt6 import QtCore, QtGui, QtWidgets
from beeref.config import commandline_args
from beeref.config import CommandlineArgs
from beeref.view import BeeGraphicsView
logger = logging.getLogger('BeeRef')
@ -69,6 +68,7 @@ def handle_sigint(signum, frame):
def main():
commandline_args = CommandlineArgs(with_check=True)
logging.basicConfig(level=getattr(logging, commandline_args.loglevel))
app = QtWidgets.QApplication(sys.argv)
bee = BeeRefMainWindow(app) # NOQA:F841

View file

@ -34,4 +34,31 @@ parser.add_argument(
action='store_true',
help='draw debug shapes for bounding rects and interactable areas')
commandline_args = parser.parse_args()
class CommandlineArgs():
"""Wrapper around argument parsing.
Checking for unknown arugments is configurable so that it can be
deliberately enabled from the main() function while ignored for
other imports. This is a singleton so that arguments are only
parsed once.
"""
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self, with_check=False):
if with_check:
self._args = parser.parse_args()
else:
self._args = parser.parse_known_args()[0]
def __getattribute__(self, name):
if name == '_args':
return super().__getattribute__(name)
else:
return getattr(self._args, name)

View file

@ -24,9 +24,10 @@ from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QGraphicsItem
from beeref import commands
from beeref.config import commandline_args
from beeref.config import CommandlineArgs
commandline_args = CommandlineArgs()
logger = logging.getLogger('BeeRef')

View file

@ -19,12 +19,14 @@ from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import Qt
from beeref import commands
from beeref.config import commandline_args
from beeref.config import CommandlineArgs
from beeref import fileio
from beeref.gui import BeeProgressDialog, WelcomeOverlay
from beeref.items import BeePixmapItem
from beeref.scene import BeeGraphicsScene
commandline_args = CommandlineArgs()
logger = logging.getLogger('BeeRef')