config¶
Configuration loading and validation.
config ¶
Configuration loading and validation.
This module handles loading the .pre-commit-snippets-config.yaml file and validating its contents. It defines dataclasses for structured configuration and provides a minimal YAML parser with no external dependencies.
SnippetSource dataclass ¶
Configuration for a single snippet source repository.
Attributes:
| Name | Type | Description |
|---|---|---|
repo | str | URL or path to the snippet repository. |
branch | str | Branch or tag to clone. Empty string for default branch. |
subdir | str | Subdirectory within the repo containing snippets. |
ext | str | File extension for snippet files (e.g., ".md"). |
Source code in pre_commit_snippet/config.py
Config dataclass ¶
Main configuration for the snippet hook.
Attributes:
| Name | Type | Description |
|---|---|---|
sources | list[SnippetSource] | List of snippet source repositories. |
target_files | list[str] | List of file paths (relative to repo root) to process. |
Source code in pre_commit_snippet/config.py
primary_source property ¶
Get the primary (first) snippet source.
Returns:
| Type | Description |
|---|---|
SnippetSource | None | The first SnippetSource, or None if no sources are configured. |
load_yaml(path) ¶
Parse the very simple flat YAML used for the config.
Supports only flat key-value pairs and lists (using "- item" syntax). Does not handle nested structures, quoted strings with colons, or multiline values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | Path | Path to the YAML configuration file. | required |
Returns:
| Type | Description |
|---|---|
dict[str, str | list[str]] | A dictionary mapping keys to either string values or lists of strings. |
Source code in pre_commit_snippet/config.py
load_config(config_path) ¶
Load and validate configuration from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path | Path | Path to the configuration file. | required |
Returns:
| Type | Description |
|---|---|
Config | A validated Config object. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError | If the config file doesn't exist. |
ValueError | If required config keys are missing. |