From 7de1dec3d283f1236b87d0c6f41e67dd67ee7544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Bou=C5=A1ka?= Date: Thu, 22 Aug 2024 15:54:51 +0200 Subject: [PATCH] Allow running scripts without configuration file --- src/buvis/buvis/adapters/config/config.py | 64 +++++++++++++++-------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/buvis/buvis/adapters/config/config.py b/src/buvis/buvis/adapters/config/config.py index c6c6b05..4390395 100644 --- a/src/buvis/buvis/adapters/config/config.py +++ b/src/buvis/buvis/adapters/config/config.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os from pathlib import Path import yaml @@ -9,8 +10,11 @@ class ConfigAdapter: """ - Manages configuration settings stored in a YAML file. - Provides functionality to load, access, and modify configuration settings. + Manages global runtime configuration for BUVIS scripts. + Provides functionality to load from YAML file, access, and modify configuration settings. + + If no configuration file path is provided, then it tries to find the path from the `BUVIS_CONFIG_FILE` + environment variable. If the environment variable is not set, it defaults to `~/.config/buvis/config.yaml`. """ def __init__(self: ConfigAdapter, file_path: Path | None = None) -> None: @@ -22,38 +26,56 @@ def __init__(self: ConfigAdapter, file_path: Path | None = None) -> None: :raises FileNotFoundError: If the configuration file does not exist. """ self._config_dict = {} - self.path_config_file = self._determine_config_path(file_path) - self._load_configuration() + existing_file_path = self._determine_config_path(file_path) + + if existing_file_path is not None: + self.path_config_file = existing_file_path + self._load_configuration() - def _determine_config_path(self: ConfigAdapter, file_path: Path | None) -> Path: + def _determine_config_path( + self: ConfigAdapter, + file_path: Path | None, + ) -> Path | None: """ Determines the absolute path to the configuration file. - When no path is provided, the defaults to '~/.config/buvis/config.yaml'. + + This method attempts to resolve the configuration file path based on the provided `file_path`. + If `file_path` is provided and exists, it returns the absolute path. If the file does not exist, + it raises a FileNotFoundError. + + If no `file_path` is provided, the method tries to find the path from the 'BUVIS_CONFIG_FILE' + environment variable. If the environment variable is not set, it defaults to '~/.config/buvis/config.yaml'. + If the resolved path from the environment or default exists, it returns the absolute path. + If it does not exist, it returns None. :param file_path: Optional path provided by the user. :type file_path: Path | None - :return: The absolute path to the configuration file. - :rtype: Path + :return: The absolute path to the configuration file or None if the default path does not exist. + :rtype: Path | None + :raises FileNotFoundError: If the provided `file_path` does not exist. """ - if file_path is None: - file_path = Path.home() / ".config/buvis/config.yaml" - return Path(file_path).absolute() + if file_path is not None: + if file_path.exists(): + return file_path.absolute() + message = f"The configuration file at {file_path} was not found." + raise FileNotFoundError(message) + + # Try getting the path from environment or default to home directory + alternative_file_path = Path( + os.getenv("BUVIS_CONFIG_FILE", Path.home() / ".config/buvis/config.yaml"), + ) + if alternative_file_path.exists(): + return alternative_file_path.absolute() + + return None def _load_configuration(self: ConfigAdapter) -> None: """ Loads the configuration from the YAML file. - - :raises FileNotFoundError: If the configuration file does not exist. """ - if self.path_config_file.exists(): - with self.path_config_file.open("r") as file: - self._config_dict = yaml.safe_load(file) or {} - else: - message = ( - f"The configuration file at {self.path_config_file} was not found." - ) - raise FileNotFoundError(message) + with self.path_config_file.open("r") as file: + self._config_dict = yaml.safe_load(file) or {} def set_configuration_item(self: ConfigAdapter, key: str, value: object) -> None: """