rhiza_tools.config

Configuration management for rhiza-tools.

This module provides configuration loading and access for rhiza-tools. It reads configuration from a TOML file and provides convenient access to tool-specific settings.

Example:

Load configuration from default path::

from rhiza_tools.config import load_config

config = load_config()
bumpversion_config = config.bumpversion

Load configuration from custom path::

from pathlib import Path
from rhiza_tools.config import load_config

config = load_config(Path("custom/.cfg.toml"))
value = config.get("custom_key", "default_value")
  1"""Configuration management for rhiza-tools.
  2
  3This module provides configuration loading and access for rhiza-tools.
  4It reads configuration from a TOML file and provides convenient access
  5to tool-specific settings.
  6
  7Example:
  8    Load configuration from default path::
  9
 10        from rhiza_tools.config import load_config
 11
 12        config = load_config()
 13        bumpversion_config = config.bumpversion
 14
 15    Load configuration from custom path::
 16
 17        from pathlib import Path
 18        from rhiza_tools.config import load_config
 19
 20        config = load_config(Path("custom/.cfg.toml"))
 21        value = config.get("custom_key", "default_value")
 22"""
 23
 24from pathlib import Path
 25from typing import Any
 26
 27import tomlkit
 28from loguru import logger
 29
 30from rhiza_tools import console
 31
 32CONFIG_FILENAME = ".rhiza/.cfg.toml"
 33
 34
 35class RhizaConfig:
 36    """Rhiza tools configuration.
 37
 38    Manages loading and accessing configuration from a TOML file. Provides
 39    convenient access to tool-specific configuration sections.
 40
 41    Attributes:
 42        config_path: Path to the configuration TOML file.
 43
 44    Example:
 45        Basic usage::
 46
 47            config = RhizaConfig()
 48            bumpversion = config.bumpversion
 49            custom_value = config.get("my_key", "default")
 50
 51        With custom path::
 52
 53            config = RhizaConfig(Path("custom/.cfg.toml"))
 54            config.load()
 55    """
 56
 57    def __init__(self, config_path: Path | None = None):
 58        """Initialize RhizaConfig.
 59
 60        Args:
 61            config_path: Path to the configuration file. If None, uses the
 62                default path defined in CONFIG_FILENAME.
 63        """
 64        self.config_path = config_path or Path(CONFIG_FILENAME)
 65        self._data: dict[str, Any] = {}
 66        self.load()
 67
 68    def load(self) -> None:
 69        """Load configuration from file.
 70
 71        Reads and parses the TOML configuration file. If the file doesn't exist,
 72        the configuration will be empty. Logs errors if parsing fails.
 73
 74        Raises:
 75            Exception: If the configuration file exists but cannot be parsed.
 76
 77        Example:
 78            config = RhizaConfig(Path("custom/.cfg.toml"))
 79            config.load()
 80        """
 81        if not self.config_path.exists():
 82            logger.debug(f"Configuration file {self.config_path} not found.")
 83            return
 84
 85        try:
 86            with open(self.config_path) as f:
 87                self._data = tomlkit.parse(f.read())
 88        except Exception as e:
 89            console.error(f"Failed to parse configuration file {self.config_path}: {e}")
 90            raise
 91
 92    @property
 93    def bumpversion(self) -> dict[str, Any]:
 94        """Get bumpversion configuration.
 95
 96        Returns:
 97            Dictionary containing bumpversion-specific configuration from the
 98            [tool.bumpversion] section of the configuration file.
 99
100        Example:
101            config = RhizaConfig()
102            bv_config = config.bumpversion
103            print(bv_config.get("current_version"))
104        """
105        result: dict[str, Any] = self._data.get("tool", {}).get("bumpversion", {})
106        return result
107
108    def get(self, key: str, default: Any = None) -> Any:
109        """Get configuration value.
110
111        Args:
112            key: Configuration key to retrieve.
113            default: Default value to return if key is not found.
114
115        Returns:
116            The configuration value for the given key, or default if not found.
117
118        Example:
119            config = RhizaConfig()
120            value = config.get("custom_setting", "default_value")
121        """
122        return self._data.get(key, default)
123
124
125def load_config(path: Path | None = None) -> RhizaConfig:
126    """Load configuration.
127
128    Convenience function to create and load a RhizaConfig instance.
129
130    Args:
131        path: Path to the configuration file. If None, uses the default path.
132
133    Returns:
134        A RhizaConfig instance with the configuration loaded.
135
136    Example:
137        Load default configuration::
138
139            config = load_config()
140
141        Load from custom path::
142
143            from pathlib import Path
144            config = load_config(Path("custom/.cfg.toml"))
145    """
146    return RhizaConfig(path)
CONFIG_FILENAME = '.rhiza/.cfg.toml'
class RhizaConfig:
 36class RhizaConfig:
 37    """Rhiza tools configuration.
 38
 39    Manages loading and accessing configuration from a TOML file. Provides
 40    convenient access to tool-specific configuration sections.
 41
 42    Attributes:
 43        config_path: Path to the configuration TOML file.
 44
 45    Example:
 46        Basic usage::
 47
 48            config = RhizaConfig()
 49            bumpversion = config.bumpversion
 50            custom_value = config.get("my_key", "default")
 51
 52        With custom path::
 53
 54            config = RhizaConfig(Path("custom/.cfg.toml"))
 55            config.load()
 56    """
 57
 58    def __init__(self, config_path: Path | None = None):
 59        """Initialize RhizaConfig.
 60
 61        Args:
 62            config_path: Path to the configuration file. If None, uses the
 63                default path defined in CONFIG_FILENAME.
 64        """
 65        self.config_path = config_path or Path(CONFIG_FILENAME)
 66        self._data: dict[str, Any] = {}
 67        self.load()
 68
 69    def load(self) -> None:
 70        """Load configuration from file.
 71
 72        Reads and parses the TOML configuration file. If the file doesn't exist,
 73        the configuration will be empty. Logs errors if parsing fails.
 74
 75        Raises:
 76            Exception: If the configuration file exists but cannot be parsed.
 77
 78        Example:
 79            config = RhizaConfig(Path("custom/.cfg.toml"))
 80            config.load()
 81        """
 82        if not self.config_path.exists():
 83            logger.debug(f"Configuration file {self.config_path} not found.")
 84            return
 85
 86        try:
 87            with open(self.config_path) as f:
 88                self._data = tomlkit.parse(f.read())
 89        except Exception as e:
 90            console.error(f"Failed to parse configuration file {self.config_path}: {e}")
 91            raise
 92
 93    @property
 94    def bumpversion(self) -> dict[str, Any]:
 95        """Get bumpversion configuration.
 96
 97        Returns:
 98            Dictionary containing bumpversion-specific configuration from the
 99            [tool.bumpversion] section of the configuration file.
100
101        Example:
102            config = RhizaConfig()
103            bv_config = config.bumpversion
104            print(bv_config.get("current_version"))
105        """
106        result: dict[str, Any] = self._data.get("tool", {}).get("bumpversion", {})
107        return result
108
109    def get(self, key: str, default: Any = None) -> Any:
110        """Get configuration value.
111
112        Args:
113            key: Configuration key to retrieve.
114            default: Default value to return if key is not found.
115
116        Returns:
117            The configuration value for the given key, or default if not found.
118
119        Example:
120            config = RhizaConfig()
121            value = config.get("custom_setting", "default_value")
122        """
123        return self._data.get(key, default)

Rhiza tools configuration.

Manages loading and accessing configuration from a TOML file. Provides convenient access to tool-specific configuration sections.

Attributes:
  • config_path: Path to the configuration TOML file.
Example:

Basic usage::

config = RhizaConfig()
bumpversion = config.bumpversion
custom_value = config.get("my_key", "default")

With custom path::

config = RhizaConfig(Path("custom/.cfg.toml"))
config.load()
RhizaConfig(config_path: pathlib.Path | None = None)
58    def __init__(self, config_path: Path | None = None):
59        """Initialize RhizaConfig.
60
61        Args:
62            config_path: Path to the configuration file. If None, uses the
63                default path defined in CONFIG_FILENAME.
64        """
65        self.config_path = config_path or Path(CONFIG_FILENAME)
66        self._data: dict[str, Any] = {}
67        self.load()

Initialize RhizaConfig.

Arguments:
  • config_path: Path to the configuration file. If None, uses the default path defined in CONFIG_FILENAME.
config_path
def load(self) -> None:
69    def load(self) -> None:
70        """Load configuration from file.
71
72        Reads and parses the TOML configuration file. If the file doesn't exist,
73        the configuration will be empty. Logs errors if parsing fails.
74
75        Raises:
76            Exception: If the configuration file exists but cannot be parsed.
77
78        Example:
79            config = RhizaConfig(Path("custom/.cfg.toml"))
80            config.load()
81        """
82        if not self.config_path.exists():
83            logger.debug(f"Configuration file {self.config_path} not found.")
84            return
85
86        try:
87            with open(self.config_path) as f:
88                self._data = tomlkit.parse(f.read())
89        except Exception as e:
90            console.error(f"Failed to parse configuration file {self.config_path}: {e}")
91            raise

Load configuration from file.

Reads and parses the TOML configuration file. If the file doesn't exist, the configuration will be empty. Logs errors if parsing fails.

Raises:
  • Exception: If the configuration file exists but cannot be parsed.
Example:

config = RhizaConfig(Path("custom/.cfg.toml")) config.load()

bumpversion: dict[str, typing.Any]
 93    @property
 94    def bumpversion(self) -> dict[str, Any]:
 95        """Get bumpversion configuration.
 96
 97        Returns:
 98            Dictionary containing bumpversion-specific configuration from the
 99            [tool.bumpversion] section of the configuration file.
100
101        Example:
102            config = RhizaConfig()
103            bv_config = config.bumpversion
104            print(bv_config.get("current_version"))
105        """
106        result: dict[str, Any] = self._data.get("tool", {}).get("bumpversion", {})
107        return result

Get bumpversion configuration.

Returns:

Dictionary containing bumpversion-specific configuration from the [tool.bumpversion] section of the configuration file.

Example:

config = RhizaConfig() bv_config = config.bumpversion print(bv_config.get("current_version"))

def get(self, key: str, default: Any = None) -> Any:
109    def get(self, key: str, default: Any = None) -> Any:
110        """Get configuration value.
111
112        Args:
113            key: Configuration key to retrieve.
114            default: Default value to return if key is not found.
115
116        Returns:
117            The configuration value for the given key, or default if not found.
118
119        Example:
120            config = RhizaConfig()
121            value = config.get("custom_setting", "default_value")
122        """
123        return self._data.get(key, default)

Get configuration value.

Arguments:
  • key: Configuration key to retrieve.
  • default: Default value to return if key is not found.
Returns:

The configuration value for the given key, or default if not found.

Example:

config = RhizaConfig() value = config.get("custom_setting", "default_value")

def load_config(path: pathlib.Path | None = None) -> RhizaConfig:
126def load_config(path: Path | None = None) -> RhizaConfig:
127    """Load configuration.
128
129    Convenience function to create and load a RhizaConfig instance.
130
131    Args:
132        path: Path to the configuration file. If None, uses the default path.
133
134    Returns:
135        A RhizaConfig instance with the configuration loaded.
136
137    Example:
138        Load default configuration::
139
140            config = load_config()
141
142        Load from custom path::
143
144            from pathlib import Path
145            config = load_config(Path("custom/.cfg.toml"))
146    """
147    return RhizaConfig(path)

Load configuration.

Convenience function to create and load a RhizaConfig instance.

Arguments:
  • path: Path to the configuration file. If None, uses the default path.
Returns:

A RhizaConfig instance with the configuration loaded.

Example:

Load default configuration::

config = load_config()

Load from custom path::

from pathlib import Path
config = load_config(Path("custom/.cfg.toml"))