Skip to content

core

wrknv.config.core

Core Configuration for wrknv

Main configuration classes using provide.foundation.

Classes

WorkenvConfig

Bases: RuntimeConfig

Main workenv configuration with WRKNV_ environment variable support.

Functions
__attrs_post_init__
__attrs_post_init__() -> None

Initialize helper instances after attrs initialization.

Source code in wrknv/config/core.py
def __attrs_post_init__(self) -> None:
    """Initialize helper instances after attrs initialization."""
    from wrknv.config.display import WorkenvConfigDisplay
    from wrknv.config.persistence import WorkenvConfigPersistence
    from wrknv.config.validation import WorkenvConfigValidator

    self._validator = WorkenvConfigValidator(self)
    self._persistence = WorkenvConfigPersistence(self)
    self._display = WorkenvConfigDisplay(self)
config_exists
config_exists() -> bool

Check if configuration file exists.

Source code in wrknv/config/core.py
def config_exists(self) -> bool:
    """Check if configuration file exists."""
    if self._persistence is None:
        return False
    return self._persistence.config_exists()  # type: ignore[no-any-return]
delete_profile
delete_profile(profile_name: str) -> bool

Delete a profile.

Source code in wrknv/config/core.py
def delete_profile(self, profile_name: str) -> bool:
    """Delete a profile."""
    if profile_name in self.profiles:
        del self.profiles[profile_name]
        self.save_config()
        return True
    return False
edit_config
edit_config() -> None

Open configuration file in editor.

Source code in wrknv/config/core.py
def edit_config(self) -> None:
    """Open configuration file in editor."""
    self._persistence.edit_config()
get_all_tools
get_all_tools() -> dict[str, str | list[str]]

Get all tool versions.

Source code in wrknv/config/core.py
def get_all_tools(self) -> dict[str, str | list[str]]:
    """Get all tool versions."""
    result = {}
    for tool_name, config in self.tools.items():
        version = config.get("version") if isinstance(config, dict) else config
        if version:
            result[tool_name] = version
    return result
get_config_path
get_config_path() -> Path

Get path to configuration file.

Source code in wrknv/config/core.py
def get_config_path(self) -> Path:
    """Get path to configuration file."""
    if self._persistence is None:
        msg = "Persistence handler not initialized"
        raise RuntimeError(msg)
    return self._persistence.get_config_path()  # type: ignore[no-any-return]
get_current_profile
get_current_profile() -> str

Get the current active profile name.

Source code in wrknv/config/core.py
def get_current_profile(self) -> str:
    """Get the current active profile name."""
    # For now, always return "default"
    # TODO: Add support for tracking active profile
    return "default"
get_env_config
get_env_config() -> dict[str, Any]

Get environment configuration for env generation.

Source code in wrknv/config/core.py
def get_env_config(self) -> dict[str, Any]:
    """Get environment configuration for env generation."""
    return self.env
get_profile
get_profile(profile_name: str) -> dict[str, str] | None

Get a configuration profile.

Source code in wrknv/config/core.py
def get_profile(self, profile_name: str) -> dict[str, str] | None:
    """Get a configuration profile."""
    return self.profiles.get(profile_name)
get_setting
get_setting(key: str, default: Any = None) -> Any

Get a configuration setting using dot notation.

Source code in wrknv/config/core.py
def get_setting(self, key: str, default: Any = None) -> Any:
    """Get a configuration setting using dot notation."""
    parts = key.split(".")
    current = self

    for part in parts:
        if hasattr(current, part):
            current = getattr(current, part)
        elif isinstance(current, dict) and part in current:
            current = current[part]
        else:
            return default

    return current
get_tool_version
get_tool_version(tool_name: str) -> str | None

Get version for a specific tool.

Source code in wrknv/config/core.py
def get_tool_version(self, tool_name: str) -> str | None:
    """Get version for a specific tool."""
    tool_config = self.tools.get(tool_name, {})
    if isinstance(tool_config, dict):
        return tool_config.get("version")
    return tool_config if isinstance(tool_config, str) else None
list_profiles
list_profiles() -> list[str]

List all available profiles.

Source code in wrknv/config/core.py
def list_profiles(self) -> list[str]:
    """List all available profiles."""
    return list(self.profiles.keys())
load classmethod
load(config_file: Path | None = None) -> WorkenvConfig

Load configuration from file and environment variables.

Source code in wrknv/config/core.py
@classmethod
def load(cls, config_file: Path | None = None) -> WorkenvConfig:
    """Load configuration from file and environment variables."""
    instance = cls()
    instance.config_path = config_file or instance._find_config_file()
    instance._manager = instance._create_manager()
    instance._load_config()

    # Also load from environment variables with WRKNV_ prefix
    env_config = cls.from_env(prefix="WRKNV")

    # Merge environment config over file config
    if env_config.project_name is not None:
        instance.project_name = env_config.project_name
    if env_config.version is not None:
        instance.version = env_config.version
    if env_config.description is not None:
        instance.description = env_config.description

    # Merge workenv settings from environment
    env_settings = WorkenvSettings.from_env(prefix="WRKNV")
    if env_settings.log_level != "WARNING":
        instance.workenv.log_level = env_settings.log_level
    if not env_settings.auto_install:
        instance.workenv.auto_install = env_settings.auto_install
    if not env_settings.use_cache:
        instance.workenv.use_cache = env_settings.use_cache
    if env_settings.cache_ttl != "7d":
        instance.workenv.cache_ttl = env_settings.cache_ttl
    if env_settings.container_runtime != "docker":
        instance.workenv.container_runtime = env_settings.container_runtime
    if env_settings.container_registry != "ghcr.io":
        instance.workenv.container_registry = env_settings.container_registry

    return instance
profile_exists
profile_exists(profile_name: str) -> bool

Check if a profile exists.

Source code in wrknv/config/core.py
def profile_exists(self, profile_name: str) -> bool:
    """Check if a profile exists."""
    return profile_name in self.profiles
save_config
save_config() -> None

Save configuration to file.

Source code in wrknv/config/core.py
def save_config(self) -> None:
    """Save configuration to file."""
    self._persistence.save_config()
save_profile
save_profile(
    profile_name: str, tools: dict[str, str]
) -> None

Save a profile.

Source code in wrknv/config/core.py
def save_profile(self, profile_name: str, tools: dict[str, str]) -> None:
    """Save a profile."""
    self.profiles[profile_name] = tools
    self.save_config()
set_setting
set_setting(key: str, value: Any) -> None

Set a configuration setting using dot notation.

Source code in wrknv/config/core.py
def set_setting(self, key: str, value: Any) -> None:
    """Set a configuration setting using dot notation."""
    parts = key.split(".")

    # Navigate to the parent
    current = self
    for part in parts[:-1]:
        if hasattr(current, part):
            current = getattr(current, part)
        elif isinstance(current, dict):
            if part not in current:
                current[part] = {}
            current = current[part]
        else:
            raise WorkenvConfigError(f"Cannot set {key}: parent doesn't exist")

    # Set the value
    last_part = parts[-1]
    if hasattr(current, last_part):
        setattr(current, last_part, value)
    elif isinstance(current, dict):
        current[last_part] = value
    else:
        raise WorkenvConfigError(f"Cannot set {key}: invalid target")

    # Save to file
    self.save_config()
show_config
show_config() -> None

Display configuration in a readable format.

Source code in wrknv/config/core.py
def show_config(self) -> None:
    """Display configuration in a readable format."""
    self._display.show_config()
to_dict
to_dict(include_sensitive: bool = True) -> dict[str, Any]

Convert configuration to dictionary.

Source code in wrknv/config/core.py
def to_dict(self, include_sensitive: bool = True) -> dict[str, Any]:
    """Convert configuration to dictionary."""
    if self._persistence is None:
        return {}
    return self._persistence.to_dict()  # type: ignore[no-any-return]
validate_config
validate_config() -> tuple[bool, list[str]]

Validate configuration comprehensively.

Returns:

Type Description
tuple[bool, list[str]]

Tuple of (is_valid, list of error messages)

Source code in wrknv/config/core.py
def validate_config(self) -> tuple[bool, list[str]]:
    """Validate configuration comprehensively.

    Returns:
        Tuple of (is_valid, list of error messages)
    """
    if self._validator is None:
        return True, []
    return self._validator.validate()  # type: ignore[no-any-return]
validate_version
validate_version(tool_name: str, version: str) -> bool

Validate a tool version format.

Parameters:

Name Type Description Default
tool_name str

Name of the tool

required
version str

Version string to validate

required

Returns:

Type Description
bool

True if version is valid, False otherwise

Source code in wrknv/config/core.py
def validate_version(self, tool_name: str, version: str) -> bool:
    """Validate a tool version format.

    Args:
        tool_name: Name of the tool
        version: Version string to validate

    Returns:
        True if version is valid, False otherwise
    """
    import re

    if not version:
        return False

    # Allow "latest" as a special case
    if version.lower() in ["latest", "stable"]:
        return True

    # Check semantic versioning format (X.Y.Z with optional suffixes)
    semver_pattern = r"^\d+\.\d+(\.\d+)?(-[a-zA-Z0-9\.\-]+)?(\+[a-zA-Z0-9\.\-]+)?$"
    if re.match(semver_pattern, version):
        return True

    # Allow version patterns (for matrix testing)
    return bool("*" in version or "~" in version or "^" in version)
write_config
write_config(config_data: dict[str, Any]) -> None

Write configuration data to file.

Source code in wrknv/config/core.py
def write_config(self, config_data: dict[str, Any]) -> None:
    """Write configuration data to file."""
    self._persistence.write_config(config_data)

WorkenvConfigError

Bases: Exception

Raised when there's an error in workenv configuration.

WorkenvSettings

Bases: RuntimeConfig

Workenv-specific settings with WRKNV_ environment variable support.

WorkenvToolConfig

Configuration for a single tool.

Functions