From 4141e9666f0cfb1c0f1fa1f6456fda6b54a631fc Mon Sep 17 00:00:00 2001 From: Rebecca Breu Date: Fri, 2 Apr 2021 11:58:00 +0200 Subject: [PATCH] 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. --- beeref/__main__.py | 4 ++-- beeref/config.py | 29 ++++++++++++++++++++++++++++- beeref/items.py | 3 ++- beeref/view.py | 4 +++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/beeref/__main__.py b/beeref/__main__.py index eded3e4..ad1d328 100755 --- a/beeref/__main__.py +++ b/beeref/__main__.py @@ -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 diff --git a/beeref/config.py b/beeref/config.py index 1d7cdda..5cb6a11 100644 --- a/beeref/config.py +++ b/beeref/config.py @@ -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) diff --git a/beeref/items.py b/beeref/items.py index 35c1602..0371079 100644 --- a/beeref/items.py +++ b/beeref/items.py @@ -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') diff --git a/beeref/view.py b/beeref/view.py index ec937ea..b6baf59 100644 --- a/beeref/view.py +++ b/beeref/view.py @@ -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')