CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-autohooks

Library for managing git hooks using pyproject.toml configuration with an extensible plugin system

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

configuration.mddocs/

Configuration Management

Configuration system for reading and writing autohooks settings in pyproject.toml with support for different dependency modes and plugin management. This API provides comprehensive configuration handling for both internal use and plugin development.

Capabilities

Configuration Loading

Load and parse autohooks configuration from pyproject.toml files with support for various input formats and validation.

class AutohooksConfig:
    """
    Main autohooks configuration class providing access to settings and plugin configuration.
    """
    
    def __init__(self, *, settings: Optional[AutohooksSettings] = None, config: Optional[Config] = None) -> None:
        """
        Create AutohooksConfig instance.
        
        Args:
            settings: Optional AutohooksSettings instance
            config: Optional Config instance for raw configuration access
        """
    
    @staticmethod
    def from_dict(config_dict: Dict[str, Any]) -> 'AutohooksConfig':
        """
        Create a new AutohooksConfig from a dictionary.
        
        Args:
            config_data: A dictionary containing the config data
            
        Returns:
            A new AutohooksConfig
        """
    
    @staticmethod
    def from_string(content: str) -> 'AutohooksConfig':
        """
        Load an AutohooksConfig from a string.
        
        Args:
            content: The content of the config
            
        Returns:
            A new AutohooksConfig
        """
    
    @staticmethod  
    def from_toml(toml_file: Path) -> 'AutohooksConfig':
        """
        Load an AutohooksConfig from a TOML file.
        
        Args:
            toml_file: Path for the toml file to load
            
        Returns:
            A new AutohooksConfig
        """

def load_config_from_pyproject_toml(pyproject_toml: Optional[Path] = None) -> AutohooksConfig:
    """
    Load an AutohooksConfig from a pyproject.toml file.
    
    If no path to the pyproject.toml file is passed the path will be determined
    from the current working directory and the project.
    
    Args:
        pyproject_toml: Path to the pyproject.toml file
        
    Returns:
        A new AutohooksConfig
    """

Usage Examples:

from autohooks.config import load_config_from_pyproject_toml, AutohooksConfig
from pathlib import Path

# Load from default pyproject.toml location
config = load_config_from_pyproject_toml()

# Load from specific file
config = load_config_from_pyproject_toml(Path("custom/pyproject.toml"))

# Load from string content
toml_content = """
[tool.autohooks]
mode = "poetry"
pre-commit = ["autohooks.plugins.black", "autohooks.plugins.ruff"]
"""
config = AutohooksConfig.from_string(toml_content)

# Load from dictionary
config_dict = {"tool": {"autohooks": {"mode": "poetry", "pre-commit": ["black"]}}}
config = AutohooksConfig.from_dict(config_dict)

Configuration Access

Access autohooks-specific settings and plugin configuration with validation and default handling.

class AutohooksConfig:
    def get_config(self) -> Config:
        """
        Get raw configuration object for advanced access.
        
        Returns:
            Config instance providing key-based access to all settings
        """
    
    def has_autohooks_config(self) -> bool:
        """
        Check if autohooks configuration section exists.
        
        Returns:
            True if [tool.autohooks] section exists, False otherwise
        """
    
    def get_pre_commit_script_names(self) -> List[str]:
        """
        Get list of configured pre-commit plugin names.
        
        Returns:
            List of plugin names from pre-commit setting
        """
    
    def get_mode(self) -> Mode:
        """
        Get configured dependency management mode.
        
        Returns:
            Mode enum value for dependency management
        """

Usage Examples:

from autohooks.config import load_config_from_pyproject_toml

def precommit(config, report_progress, **kwargs):
    # Access autohooks configuration
    autohooks_config = load_config_from_pyproject_toml()
    
    # Check if autohooks is configured
    if not autohooks_config.has_autohooks_config():
        error("Autohooks not configured in pyproject.toml")
        return 1
    
    # Get plugin list
    plugins = autohooks_config.get_pre_commit_script_names()
    info(f"Configured plugins: {', '.join(plugins)}")
    
    # Get dependency mode
    mode = autohooks_config.get_mode()
    info(f"Using dependency mode: {mode}")
    
    # Access raw configuration for plugin-specific settings
    raw_config = autohooks_config.get_config()
    plugin_config = raw_config.get("tool", "my-plugin")
    
    return 0

Settings Management

Structured settings management with support for different dependency modes and plugin configuration.

class AutohooksSettings:
    """
    Autohooks configuration settings dataclass.
    """
    mode: Mode = Mode.UNDEFINED
    pre_commit: Iterable[str] = field(default_factory=list)
    
    def write(self, filename: Path) -> None:
        """
        Write the current AutohooksSettings to a TOML file.
        
        If the TOML file already exists only the [tool.autohooks] section is overridden.
        
        Args:
            filename: Path to pyproject.toml file to write/update
        """

class Mode(Enum):
    """
    Dependency management modes for hook execution.
    """
    PIPENV = 1
    PYTHONPATH = 2  
    POETRY = 3
    PIPENV_MULTILINE = 4
    POETRY_MULTILINE = 5
    UNDEFINED = -1
    UNKNOWN = -2
    
    def get_effective_mode(self):
        """
        Get the effective mode for execution, handling multiline variants.
        
        Returns:
            Effective Mode for hook execution
        """
    
    @staticmethod
    def from_string(modestring: Optional[str]) -> 'Mode':
        """
        Convert string to Mode enum.
        
        Args:
            modestring: String representation of mode
            
        Returns:
            Mode enum value
        """

Usage Examples:

from autohooks.settings import AutohooksSettings, Mode
from pathlib import Path

# Create new settings
settings = AutohooksSettings(
    mode=Mode.POETRY,
    pre_commit=["autohooks.plugins.black", "autohooks.plugins.ruff"]
)

# Write settings to pyproject.toml
pyproject_path = Path("pyproject.toml")
settings.write(pyproject_path)

# Work with modes
mode = Mode.from_string("poetry")
effective_mode = mode.get_effective_mode()

# Check mode types
if mode == Mode.POETRY:
    print("Using Poetry for dependency management")
elif mode == Mode.PIPENV:
    print("Using Pipenv for dependency management")
elif mode == Mode.PYTHONPATH:
    print("Using system Python path")

Configuration Constants

Constants for configuration section identification and validation.

AUTOHOOKS_SECTION = "tool.autohooks"

Usage Examples:

from autohooks.config import AUTOHOOKS_SECTION
import tomlkit

# Check for autohooks section in TOML
def has_autohooks_section(toml_content: str) -> bool:
    doc = tomlkit.loads(toml_content)
    return AUTOHOOKS_SECTION in doc

# Add autohooks section to existing TOML
def add_autohooks_section(toml_path: Path):
    doc = tomlkit.loads(toml_path.read_text())
    if AUTOHOOKS_SECTION not in doc:
        doc[AUTOHOOKS_SECTION] = {"mode": "poetry", "pre-commit": []}
        toml_path.write_text(tomlkit.dumps(doc))

Configuration File Format

The autohooks configuration is stored in pyproject.toml under the [tool.autohooks] section:

[tool.autohooks]
mode = "poetry"  # or "pipenv", "pythonpath"
pre-commit = [
    "autohooks.plugins.black",
    "autohooks.plugins.ruff", 
    "autohooks.plugins.mypy"
]

# Plugin-specific configuration
[tool.autohooks.plugins.black]
line-length = 88

[tool.autohooks.plugins.ruff]
line-length = 88
target-version = "py39"

Types

from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path  
from typing import Any, Dict, Iterable, List, Optional

Install with Tessl CLI

npx tessl i tessl/pypi-autohooks

docs

cli.md

configuration.md

git-operations.md

hook-management.md

index.md

path-utilities.md

plugin-api.md

tile.json