From 0bc0e6929000d5dd1ec0244a51bbd881d9e3df55 Mon Sep 17 00:00:00 2001 From: "Staiger, Christine" Date: Fri, 29 Nov 2024 10:08:23 +0100 Subject: [PATCH] add version string to log and welcome --- ibridgesgui/__main__.py | 11 ++++++++++- ibridgesgui/config.py | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ibridgesgui/__main__.py b/ibridgesgui/__main__.py index 7dea5c0b..a215d57d 100755 --- a/ibridgesgui/__main__.py +++ b/ibridgesgui/__main__.py @@ -21,6 +21,11 @@ from ibridgesgui.ui_files.MainMenu import Ui_MainWindow from ibridgesgui.welcome import Welcome +try: # Python < 3.10 (backport) + from importlib_metadata import version # type: ignore +except ImportError: + from importlib.metadata import version # type: ignore [assignment] + # Global constants THIS_APPLICATION = "ibridges-gui" @@ -123,8 +128,12 @@ def setup_tabs(self): def welcome_tab(self): """Create first tab.""" + try: + release = version("ibridgesgui") + except Exception: + release = "" welcome = Welcome() - self.tab_widget.addTab(welcome, "iBridges") + self.tab_widget.addTab(welcome, f"iBridges {release}") def init_info_tab(self): """Create info.""" diff --git a/ibridgesgui/config.py b/ibridgesgui/config.py index 4dd9e871..66972c56 100644 --- a/ibridgesgui/config.py +++ b/ibridgesgui/config.py @@ -20,6 +20,11 @@ ) from irods.session import iRODSSession +try: # Python < 3.10 (backport) + from importlib_metadata import version # type: ignore +except ImportError: + from importlib.metadata import version # type: ignore [assignment] + LOG_LEVEL = { "fulldebug": logging.DEBUG - 5, "debug": logging.DEBUG, @@ -33,6 +38,7 @@ CONFIG_FILE = CONFIG_DIR.joinpath("ibridges_gui.json") IRODSA = Path.home() / ".irods" / ".irodsA" + def ensure_log_config_location(): """Ensure the location for logs and config files.""" CONFIG_DIR.mkdir(parents=True, mode=0o700, exist_ok=True) @@ -43,6 +49,7 @@ def ensure_irods_location(): irods_loc = Path("~/.irods").expanduser() irods_loc.mkdir(mode=0o700, exist_ok=True) + # logging functions def init_logger(app_name: str, log_level: str) -> logging.Logger: """Create a logger for the app. @@ -61,13 +68,19 @@ def init_logger(app_name: str, log_level: str) -> logging.Logger: file_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.setLevel(LOG_LEVEL.get(log_level, LOG_LEVEL["info"])) + try: + release = version("ibridgesgui") + except Exception: + release = "" # Logger greeting when app is started with open(logfile, "a", encoding="utf-8") as logfd: logfd.write("\n\n") underscores = f"{'_' * 50}\n" logfd.write(underscores * 2) - logfd.write(f"\t Starting iBridges-GUI \n\t{datetime.datetime.now().isoformat()}\n") + logfd.write( + f"\t Starting iBridges-GUI {release}\n\t{datetime.datetime.now().isoformat()}\n" + ) logfd.write(underscores * 2) return logger @@ -131,6 +144,7 @@ def _get_config() -> Union[None, dict]: print(f"CANNOT START APP: {CONFIG_FILE} incorrectly formatted.") sys.exit(1) + def save_current_settings(env_path_name: Path): """Store the environment with the currently scrambled password in irodsA.""" with open(IRODSA, "r", encoding="utf-8") as f: @@ -142,6 +156,7 @@ def save_current_settings(env_path_name: Path): config["settings"][str(env_path_name)] = pw _save_config(config) + def get_prev_settings(): """Extract the settings from the configuration.""" config = _get_config() @@ -149,6 +164,7 @@ def get_prev_settings(): return {} return config.get("settings", {}) + # irods config functions @@ -177,7 +193,7 @@ def is_session_from_config(session: Session) -> Union[Session, None]: return False -def check_irods_config(ienv: Union[Path, dict], include_network = True) -> str: +def check_irods_config(ienv: Union[Path, dict], include_network=True) -> str: """Check whether an iRODS configuration file is correct. Parameters