Skip to content

Commit

Permalink
Allow running scripts without configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
tbouska committed Aug 22, 2024
1 parent 9fc7d66 commit 7de1dec
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions src/buvis/buvis/adapters/config/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from pathlib import Path

import yaml
Expand All @@ -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:
Expand All @@ -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:
"""
Expand Down

0 comments on commit 7de1dec

Please sign in to comment.