Settings¶
Configuration management for Brainsmith projects with hierarchical loading and type-safe validation using Pydantic.
Supports loading from CLI arguments, environment variables (BSMITH_* prefix), project config files (brainsmith.yaml), and built-in defaults.
load_config ¶
Load configuration with hierarchical priority.
Priority order (highest to lowest): 1. CLI arguments (passed as kwargs) 2. Environment variables (BSMITH_* prefix) 3. Project config file (brainsmith.yaml) 4. Built-in defaults
Special handling: - BSMITH_LOG_LEVEL env var overrides logging.level (shorthand for BSMITH_LOGGING__LEVEL)
Path Resolution: - Absolute paths: Used as-is - Relative CLI paths: Resolve to current working directory - Relative paths (YAML/env/defaults): Resolve to project directory
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_file |
Path | None
|
Path to project config file (for non-standard locations) |
None
|
**cli_overrides |
CLI argument overrides |
{}
|
Returns:
| Type | Description |
|---|---|
SystemConfig
|
SystemConfig object |
Source code in brainsmith/settings/loader.py
Example:
from brainsmith.settings import load_config
# Load with default project config
config = load_config()
# Load with custom project file
config = load_config(project_file="custom.yaml")
# Load with CLI overrides
config = load_config(
build_dir="./custom-build",
vivado_path="/tools/Xilinx/Vivado/2024.2"
)
get_config
cached
¶
Get cached configuration instance.
Environment must be sourced first
source .brainsmith/env.sh # or: direnv allow
Example:
from brainsmith.settings import get_config
# Get cached config instance
config = get_config()
# Access configuration values
print(config.build_dir)
print(config.vivado_path)
print(config.logging.level)
get_default_config ¶
Get a configuration instance with only default values (no files or env vars).
Source code in brainsmith/settings/loader.py
Example:
from brainsmith.settings import get_default_config
# Get default config without loading from files/env
default_config = get_default_config()
SystemConfig ¶
Bases: BaseSettings
Configuration schema with hierarchical priority.
Priority order (highest to lowest): 1. CLI arguments (passed to constructor) 2. Environment variables (BSMITH_* prefix) 3. Project config (brainsmith.yaml) 4. Built-in defaults
bsmith_dir
cached
property
¶
Brainsmith repository root containing pyproject.toml.
This is the parent of the brainsmith package directory.
generate_activation_script ¶
Generate bash activation script from current configuration.
The script can be sourced multiple times safely: - Cleans up old Xilinx/brainsmith paths before adding new ones - Sources Xilinx settings64.sh files for complete tool environment
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path |
Path
|
Where to write the activation script |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to generated script |
Example
config = get_config() config.generate_activation_script(Path("~/.brainsmith/env.sh"))
User runs: source ~/.brainsmith/env.sh¶
Source code in brainsmith/settings/schema.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
generate_direnv_file ¶
Generate .envrc file for direnv integration.
Creates a direnv configuration that: - Watches brainsmith.yaml for changes - Auto-regenerates environment when config changes - Sources .brainsmith/env.sh for all environment variables - Activates virtualenv automatically
User must run 'direnv allow' to trust the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path |
Path
|
Where to write the .envrc file (typically project root) |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to generated .envrc file |
Example
config = get_config() config.generate_direnv_file(Path(".envrc"))
User runs: direnv allow¶
Source code in brainsmith/settings/schema.py
model_post_init ¶
Resolve all paths to absolute.
At this point: - CLI paths are already absolute (resolved to CWD in load_config) - YAML/env/default paths may be relative (need resolution to project_dir)
Resolution steps: 1. Detect project_dir (where config file is, or CWD) 2. Resolve user-facing paths (relative → project_dir) 3. Force internal paths (deps_dir → bsmith_dir) 4. Set defaults for unset paths 5. Validate everything is sane 6. Check for deprecated configuration
Source code in brainsmith/settings/schema.py
settings_customise_sources
classmethod
¶
settings_customise_sources(settings_cls: type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> tuple[PydanticBaseSettingsSource, ...]
Customize settings sources.
Priority order (first source wins): 1. Init settings (CLI/constructor args) - paths already resolved to CWD 2. Environment variables (BSMITH_*) - paths stay relative, resolved in model_post_init 3. YAML files (custom source) - paths stay relative, resolved in model_post_init 4. Field defaults (built into pydantic)
Path Resolution: - CLI paths are resolved to CWD in load_config() before reaching here - All other paths stay relative and are resolved in model_post_init()
Source code in brainsmith/settings/schema.py
validate_component_sources
classmethod
¶
Validate component sources and warn about reserved source names.
Reserved source names: - Core namespace ('brainsmith'): Internal components loaded via direct import - Entry points (e.g., 'finn'): Discovered via pip package entry points
Users can configure filesystem-based sources (project, user, custom) but cannot override reserved names.
Source code in brainsmith/settings/schema.py
Example:
from brainsmith.settings import SystemConfig
# Create config with custom values
config = SystemConfig(
build_dir="./build",
vivado_path="/tools/Xilinx/Vivado/2024.2"
)
# Access nested configuration
print(config.logging.level)
print(config.netron_port)
EnvironmentExporter ¶
Export configuration as environment variables for shell scripts.
Generates environment variable dictionaries for: - FINN (FINN_ROOT, FINN_BUILD_DIR, etc.) - Xilinx tools (VIVADO_PATH, XILINX_VIVADO, etc.) - Visualization tools (NETRON_PORT) - BSMITH_* variables (for YAML ${var} expansion in blueprints)
Example
config = SystemConfig() exporter = EnvironmentExporter(config) env_dict = exporter.to_external_dict() print(env_dict['FINN_ROOT'])
Initialize with system configuration.
Source code in brainsmith/settings/env_export.py
to_all_dict ¶
Generate dict of all environment variables.
Includes both external tool variables and internal BSMITH_* variables.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict of all environment variables |
Source code in brainsmith/settings/env_export.py
to_env_dict ¶
Generate complete environment for shell script generation.
Includes PATH, LD_LIBRARY_PATH, and all configuration variables. Used by generate_activation_script() and generate_direnv_file().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_internal |
bool
|
Include internal BSMITH_* variables (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict of environment variable names to string values |
Source code in brainsmith/settings/env_export.py
to_external_dict ¶
Generate dict of external tool environment variables.
Returns variables for external tools (FINN, Xilinx, etc.). Excludes internal BSMITH_* variables.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict of environment variable names to string values |
Source code in brainsmith/settings/env_export.py
Example:
from brainsmith.settings import get_config, EnvironmentExporter
config = get_config()
exporter = EnvironmentExporter(config)
# Export environment variables for external tools
env_dict = exporter.to_external_dict()
print(env_dict['FINN_ROOT'])
print(env_dict['VIVADO_PATH'])
See Also¶
- Getting Started - Installation and project setup
- GitHub - Issues and questions