From 7773adfec4ef78df7ff5b001552e33ffd8c00c52 Mon Sep 17 00:00:00 2001 From: YektaY Date: Fri, 25 Oct 2024 09:15:39 -0700 Subject: [PATCH 1/5] added TODO --- src/badger/actions/doctor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/badger/actions/doctor.py b/src/badger/actions/doctor.py index ff7e4ebe..4dc7590f 100644 --- a/src/badger/actions/doctor.py +++ b/src/badger/actions/doctor.py @@ -77,6 +77,7 @@ def check_n_config_paths(): try: print('') _config_path_var(pname) + #TODO potential keyError here except KeyboardInterrupt: pass From ede55ac70f69d595f43b1dbcb38ce0f914dda61f Mon Sep 17 00:00:00 2001 From: YektaY Date: Mon, 4 Nov 2024 17:17:30 -0800 Subject: [PATCH 2/5] added the ability to pass a config filepath to load a config file that is not the default one stored in the user folder --- src/badger/__main__.py | 1 + src/badger/actions/__init__.py | 7 ++++++- src/badger/gui/default/__init__.py | 7 ++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/badger/__main__.py b/src/badger/__main__.py index d7c1873b..8bdf6654 100644 --- a/src/badger/__main__.py +++ b/src/badger/__main__.py @@ -24,6 +24,7 @@ def main(): parser.add_argument('-l', '--log', choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'], default='WARNING', const='WARNING', nargs='?', help='change the log level') + parser.add_argument('-cf', '--config_filepath', type=str, default=None, help='Path to the config file') parser.set_defaults(func=show_info) subparsers = parser.add_subparsers(help='Badger commands help') diff --git a/src/badger/actions/__init__.py b/src/badger/actions/__init__.py index 619cc2fd..e7924ea3 100644 --- a/src/badger/actions/__init__.py +++ b/src/badger/actions/__init__.py @@ -7,12 +7,17 @@ def show_info(args): # Change log level for all existed loggers set_log_level(args.log) + config_path = None + + if args.config_filepath: + config_path = args.config_filepath + if args.gui: if check_n_config_paths(): from ..gui.default import launch_gui - launch_gui() + launch_gui(config_path) return diff --git a/src/badger/gui/default/__init__.py b/src/badger/gui/default/__init__.py index a629b0ae..c192cfca 100644 --- a/src/badger/gui/default/__init__.py +++ b/src/badger/gui/default/__init__.py @@ -48,9 +48,14 @@ def on_timeout(): print('Timeout, resume the operation...') -def launch_gui(): +def launch_gui(config_path=None): app = QApplication(sys.argv) config_singleton = init_settings() + + if config_path is not None: + config_singleton._instance._config = config_singleton.load_or_create_config(config_path) + config_singleton._instance.config_path = config_path + # Set app metainfo app.setApplicationName('Badger') icon_ref = resources.files(__name__) / 'images/icon.png' From cd11412e651f839490ea691df8fa489563e1b760 Mon Sep 17 00:00:00 2001 From: YektaY Date: Mon, 4 Nov 2024 17:31:45 -0800 Subject: [PATCH 3/5] formatting fixes --- src/badger/__main__.py | 8 +++++++- src/badger/actions/__init__.py | 1 - src/badger/actions/doctor.py | 2 +- src/badger/gui/default/__init__.py | 8 +++++--- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/badger/__main__.py b/src/badger/__main__.py index 09315b0d..a2d27c61 100644 --- a/src/badger/__main__.py +++ b/src/badger/__main__.py @@ -31,7 +31,13 @@ def main(): nargs="?", help="change the log level", ) - parser.add_argument('-cf', '--config_filepath', type=str, default=None, help='Path to the config file') + parser.add_argument( + "-cf", + "--config_filepath", + type=str, + default=None, + help="Path to the config file", + ) parser.set_defaults(func=show_info) subparsers = parser.add_subparsers(help="Badger commands help") diff --git a/src/badger/actions/__init__.py b/src/badger/actions/__init__.py index cd1c3f2a..26f2d8f6 100644 --- a/src/badger/actions/__init__.py +++ b/src/badger/actions/__init__.py @@ -11,7 +11,6 @@ def show_info(args): if args.config_filepath: config_path = args.config_filepath - if args.gui: if check_n_config_paths(): diff --git a/src/badger/actions/doctor.py b/src/badger/actions/doctor.py index e882a801..29f55a8c 100644 --- a/src/badger/actions/doctor.py +++ b/src/badger/actions/doctor.py @@ -80,7 +80,7 @@ def check_n_config_paths(): try: print("") _config_path_var(pname) - #TODO potential keyError here + # TODO potential keyError here except KeyboardInterrupt: pass diff --git a/src/badger/gui/default/__init__.py b/src/badger/gui/default/__init__.py index d08a0025..91a547c5 100644 --- a/src/badger/gui/default/__init__.py +++ b/src/badger/gui/default/__init__.py @@ -84,10 +84,12 @@ def launch_gui(config_path=None): app = QApplication(sys.argv) config_singleton = init_settings() - if config_path is not None: - config_singleton._instance._config = config_singleton.load_or_create_config(config_path) + if config_path is not None: + config_singleton._instance._config = config_singleton.load_or_create_config( + config_path + ) config_singleton._instance.config_path = config_path - + # Set app metainfo app.setApplicationName("Badger") icon_ref = resources.files(__name__) / "images/icon.png" From eb6bc7e91ac8e3f1d6a35ab4a9eeaaf7d1c35891 Mon Sep 17 00:00:00 2001 From: YektaY Date: Tue, 5 Nov 2024 11:18:41 -0800 Subject: [PATCH 4/5] raise an error if passed in config file is incorrect --- src/badger/actions/__init__.py | 2 +- src/badger/errors.py | 3 ++- src/badger/gui/default/__init__.py | 2 ++ src/badger/settings.py | 37 ++++++++++++++++++++++-------- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/badger/actions/__init__.py b/src/badger/actions/__init__.py index 26f2d8f6..7de8f7d0 100644 --- a/src/badger/actions/__init__.py +++ b/src/badger/actions/__init__.py @@ -24,7 +24,7 @@ def show_info(args): if check_n_config_paths(): from badger.gui.default import launch_gui - launch_gui() + launch_gui(config_path) return diff --git a/src/badger/errors.py b/src/badger/errors.py index adba60d4..a13fcb66 100644 --- a/src/badger/errors.py +++ b/src/badger/errors.py @@ -90,7 +90,8 @@ class BadgerLogbookError(Exception): class BadgerLoadConfigError(Exception): - pass + def __init__(self, message="Config Error!"): + super().__init__(message) class BadgerRoutineError(Exception): diff --git a/src/badger/gui/default/__init__.py b/src/badger/gui/default/__init__.py index 91a547c5..c33fc289 100644 --- a/src/badger/gui/default/__init__.py +++ b/src/badger/gui/default/__init__.py @@ -85,10 +85,12 @@ def launch_gui(config_path=None): config_singleton = init_settings() if config_path is not None: + config_singleton._instance.user_flag = True config_singleton._instance._config = config_singleton.load_or_create_config( config_path ) config_singleton._instance.config_path = config_path + # Set app metainfo app.setApplicationName("Badger") diff --git a/src/badger/settings.py b/src/badger/settings.py index 1b894d88..1f401645 100644 --- a/src/badger/settings.py +++ b/src/badger/settings.py @@ -6,7 +6,7 @@ from badger.utils import get_datadir from pydantic import BaseModel, Field, ValidationError from typing import Any, Dict, Optional, Union - +from badger.errors import BadgerLoadConfigError class Setting(BaseModel): """ @@ -106,15 +106,16 @@ class BadgerConfig(BaseModel): class ConfigSingleton: _instance = None - def __new__(cls, config_path: str = None): + def __new__(cls, config_path: str = None, user_flag: bool = False): if cls._instance is None: cls._instance = super(ConfigSingleton, cls).__new__(cls) + cls._instance.user_flag = user_flag cls._instance._config = cls.load_or_create_config(config_path) cls._instance.config_path = config_path return cls._instance - @staticmethod - def load_or_create_config(config_path: str) -> BadgerConfig: + @classmethod + def load_or_create_config(cls, config_path: str) -> BadgerConfig: """ Loads the config file from a given yaml file if it exists, otherwise creates an instance of BadgerConfig with default settings. @@ -161,7 +162,12 @@ def load_or_create_config(config_path: str) -> BadgerConfig: except ValidationError as e: print(f"Error validating config file: {e}") raise - else: + else: + if cls._instance.user_flag: + err_msg = f"Error loading config {config_path}: invalid path." + raise BadgerLoadConfigError(err_msg) + + print("YES") return BadgerConfig() @property @@ -348,20 +354,31 @@ def reset_settings(self) -> None: ) -def init_settings() -> ConfigSingleton: +def init_settings(config_arg: bool=None) -> ConfigSingleton: """ Builds and returns an instance of the ConfigSingleton class. + Parameters + ---------- + config_arg: bool + a path to a config file passed through the --config__filepath argument + Returns ------- config_singleton: ConfigSingleton an instance of the ConfigSingleton class """ + user_flag = False + + if config_arg == None: + config_path = get_user_config_folder() + file_name = "config.yaml" + file_path = os.path.join(config_path, file_name) + else: + file_path = config_arg + user_flag = True - config_path = get_user_config_folder() - file_name = "config.yaml" - file_path = os.path.join(config_path, file_name) - config_singleton = ConfigSingleton(file_path) + config_singleton = ConfigSingleton(file_path, user_flag) return config_singleton From cb895cdb7f1c83c7199e87c44fe8bd1b95fa4feb Mon Sep 17 00:00:00 2001 From: YektaY Date: Tue, 5 Nov 2024 16:24:40 -0800 Subject: [PATCH 5/5] made changes to how the passed config file is passed to the singleton which holds the config settings in badger --- src/badger/actions/__init__.py | 2 +- src/badger/actions/doctor.py | 8 ++++++-- src/badger/gui/default/__init__.py | 10 +++------- src/badger/settings.py | 14 +++++++------- src/badger/tests/test_settings.py | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/badger/actions/__init__.py b/src/badger/actions/__init__.py index 7de8f7d0..1478e78c 100644 --- a/src/badger/actions/__init__.py +++ b/src/badger/actions/__init__.py @@ -13,7 +13,7 @@ def show_info(args): config_path = args.config_filepath if args.gui: - if check_n_config_paths(): + if check_n_config_paths(args.config_filepath): from badger.gui.default import launch_gui launch_gui(config_path) diff --git a/src/badger/actions/doctor.py b/src/badger/actions/doctor.py index 29f55a8c..48e3e8fd 100644 --- a/src/badger/actions/doctor.py +++ b/src/badger/actions/doctor.py @@ -5,6 +5,7 @@ def self_check(args): config = init_settings() # Reset Badger + if args.reset: while True: _res = input( @@ -30,8 +31,11 @@ def self_check(args): print("Badger is healthy!") -def check_n_config_paths(): - config = init_settings() +def check_n_config_paths(config_filepath=None): + if config_filepath is not None: + config = init_settings(config_filepath) + else: + config = init_settings() good = True issue_list = [] diff --git a/src/badger/gui/default/__init__.py b/src/badger/gui/default/__init__.py index c33fc289..04332547 100644 --- a/src/badger/gui/default/__init__.py +++ b/src/badger/gui/default/__init__.py @@ -82,15 +82,11 @@ def launch_gui(config_path=None): sys.excepthook = error_handler app = QApplication(sys.argv) - config_singleton = init_settings() if config_path is not None: - config_singleton._instance.user_flag = True - config_singleton._instance._config = config_singleton.load_or_create_config( - config_path - ) - config_singleton._instance.config_path = config_path - + config_singleton = init_settings(config_path) + else: + config_singleton = init_settings() # Set app metainfo app.setApplicationName("Badger") diff --git a/src/badger/settings.py b/src/badger/settings.py index 1f401645..1a77adcd 100644 --- a/src/badger/settings.py +++ b/src/badger/settings.py @@ -8,6 +8,7 @@ from typing import Any, Dict, Optional, Union from badger.errors import BadgerLoadConfigError + class Setting(BaseModel): """ Setting model to store the configuration details. @@ -162,12 +163,11 @@ def load_or_create_config(cls, config_path: str) -> BadgerConfig: except ValidationError as e: print(f"Error validating config file: {e}") raise - else: + else: if cls._instance.user_flag: err_msg = f"Error loading config {config_path}: invalid path." - raise BadgerLoadConfigError(err_msg) + raise BadgerLoadConfigError(err_msg) - print("YES") return BadgerConfig() @property @@ -354,23 +354,23 @@ def reset_settings(self) -> None: ) -def init_settings(config_arg: bool=None) -> ConfigSingleton: +def init_settings(config_arg: bool = None) -> ConfigSingleton: """ Builds and returns an instance of the ConfigSingleton class. Parameters ---------- config_arg: bool - a path to a config file passed through the --config__filepath argument + a path to a config file passed through the --config__filepath argument Returns ------- config_singleton: ConfigSingleton an instance of the ConfigSingleton class """ - user_flag = False + user_flag = False - if config_arg == None: + if config_arg is None: config_path = get_user_config_folder() file_name = "config.yaml" file_path = os.path.join(config_path, file_name) diff --git a/src/badger/tests/test_settings.py b/src/badger/tests/test_settings.py index a6189002..f3f09953 100644 --- a/src/badger/tests/test_settings.py +++ b/src/badger/tests/test_settings.py @@ -109,7 +109,7 @@ def test_init_settings(self): ) as mock_config_cls: config_singleton = init_settings() mock_config_cls.assert_called_once_with( - "/mock/config/folder/config.yaml" + "/mock/config/folder/config.yaml", False ) assert config_singleton == mock_config_singleton