Config¶
Configuration management for slurmkit projects.
Overview¶
The Config class manages configuration from multiple sources with the following priority (highest to lowest):
- CLI arguments
- Environment variables (
SLURMKIT_*) - Config file (
.slurmkit/config.yaml) - Default values
Classes¶
Config
¶
Config(config_path: Optional[Union[str, Path]] = None, project_root: Optional[Union[str, Path]] = None)
Configuration manager for slurmkit.
Loads configuration from multiple sources and provides access to settings. Configuration precedence (highest to lowest): 1. Explicit overrides (passed to methods) 2. Environment variables 3. Project config file 4. Built-in defaults
Attributes:
| Name | Type | Description |
|---|---|---|
config_path |
Path to the loaded config file (if any) |
|
project_root |
Root directory of the project (where .slurmkit/ lives) |
|
hostname |
Current machine's hostname (for cluster identification) |
Example
config = Config() config.get("jobs_dir") '.jobs/' config.get("slurm_defaults.partition") 'compute'
Initialize configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
Optional[Union[str, Path]]
|
Explicit path to config file. If None, searches for .slurmkit/config.yaml in project_root or current directory. |
None
|
project_root
|
Optional[Union[str, Path]]
|
Project root directory. If None, uses current directory. |
None
|
Source code in src/slurmkit/config.py
get
¶
Get a configuration value by dot-separated key path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Dot-separated key path (e.g., "slurm_defaults.partition") |
required |
default
|
Any
|
Default value if key not found. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Configuration value or default. |
Example
config.get("jobs_dir") '.jobs/' config.get("slurm_defaults.partition") 'compute' config.get("nonexistent", "fallback") 'fallback'
Source code in src/slurmkit/config.py
get_path
¶
Get a configuration value as a Path, resolved relative to project root.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Dot-separated key path. |
required |
default
|
Any
|
Default value if key not found. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Path]
|
Resolved Path or None. |
Source code in src/slurmkit/config.py
get_output_patterns
¶
Get output file patterns in priority order.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of output file pattern strings. |
Source code in src/slurmkit/config.py
get_slurm_defaults
¶
Get default SLURM arguments.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary of default SLURM arguments. |
as_dict
¶
Return the full configuration as a dictionary.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Complete configuration dictionary. |
save
¶
Save current configuration to a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Optional[Union[str, Path]]
|
Path to save to. Defaults to self.config_path. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path where config was saved. |
Source code in src/slurmkit/config.py
Functions¶
get_config
¶
get_config(config_path: Optional[Union[str, Path]] = None, project_root: Optional[Union[str, Path]] = None, reload: bool = False) -> Config
Get the global configuration instance.
This function provides a singleton-like access to configuration. On first call (or when reload=True), it creates a new Config instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
Optional[Union[str, Path]]
|
Explicit path to config file. |
None
|
project_root
|
Optional[Union[str, Path]]
|
Project root directory. |
None
|
reload
|
bool
|
Force reload of configuration. |
False
|
Returns:
| Type | Description |
|---|---|
Config
|
Global Config instance. |
Example
config = get_config() config.get("jobs_dir") '.jobs/'
Source code in src/slurmkit/config.py
init_config
¶
init_config(project_root: Optional[Union[str, Path]] = None, overwrite: bool = False, **kwargs: Any) -> Path
Initialize a new project configuration file.
Creates .slurmkit/config.yaml with default values, optionally customized with provided kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
Optional[Union[str, Path]]
|
Project root directory. Defaults to current directory. |
None
|
overwrite
|
bool
|
If True, overwrite existing config file. |
False
|
**kwargs
|
Any
|
Configuration values to set (e.g., jobs_dir="my_jobs/"). |
{}
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to created config file. |
Raises:
| Type | Description |
|---|---|
FileExistsError
|
If config file exists and overwrite=False. |
Example
init_config(jobs_dir="experiments/", slurm_defaults={"partition": "gpu"}) PosixPath('.slurmkit/config.yaml')
Source code in src/slurmkit/config.py
Usage Example¶
from slurmkit import Config, get_config
# Get the global config instance
config = get_config()
# Access configuration values
jobs_dir = config.get("jobs_dir")
partition = config.get("slurm_defaults.partition")
# Get as Path object
jobs_path = config.get_path("jobs_dir")
# Get all SLURM defaults
slurm_defaults = config.get_slurm_defaults()