Config Module API Reference¶
This section documents the internals of the config
module in Bijux CLI.
bijux_cli.services.config ¶
Provides a robust, file-based configuration management service.
This module defines the Config
class, a concrete implementation of the ConfigProtocol
. It is responsible for loading, accessing, and persisting key-value configuration settings from .env
files.
Key features include
- Atomic Writes: Changes are written to a temporary file before being atomically moved into place to prevent data corruption.
- Cross-Process Safety: On POSIX systems,
fcntl.flock
is used with retries to handle concurrent access from multiple CLI processes. - Key Normalization: Configuration keys are handled case-insensitively and the
BIJUXCLI_
prefix is optional. - Security Checks: Includes validation to prevent operating on device files or traversing symbolic link loops.
Config ¶
Bases: ConfigProtocol
A robust configuration handler for .env
files.
This service manages loading, saving, and persisting configuration values, featuring atomic writes and key normalization. Keys are stored internally in lowercase and without the BIJUXCLI_
prefix.
Attributes:
-
_di
(Any
) –The dependency injection container.
-
_log
(ObservabilityProtocol
) –The logging service.
-
_data
(dict[str, str]
) –The in-memory dictionary of configuration data.
-
_path
(Path | None
) –The path to the configuration file being managed.
Initializes the Config service and attempts to autoload configuration.
Parameters:
-
dependency_injector
(Any
) –The DI container for resolving dependencies.
Source code in src/bijux_cli/services/config.py
all ¶
Returns all configuration key-value pairs.
Returns:
-
dict[str, str]
–dict[str, str]: A dictionary of all configuration data.
clear ¶
Deletes all configuration entries and removes the config file.
Raises:
-
CommandError
–If the config file cannot be deleted due to a lock or other filesystem error.
Source code in src/bijux_cli/services/config.py
delete ¶
Deletes a configuration key and persists the change.
Parameters:
-
key
(str
) –The key to delete (case-insensitive,
BIJUXCLI_
prefix optional).
Raises:
-
CommandError
–If the key does not exist or the change cannot be persisted.
Source code in src/bijux_cli/services/config.py
export ¶
Exports the configuration to a file or standard output.
Parameters:
-
path
(str | Path
) –The destination file path, or "-" for stdout.
-
out_format
(str | None
, default:None
) –The output format ('env', 'json', 'yaml'). If None, the format is auto-detected from the file extension.
Raises:
-
CommandError
–If the format is unsupported or the export fails.
Source code in src/bijux_cli/services/config.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
|
get ¶
Retrieves a configuration value by key.
The key is normalized (lowercase, BIJUXCLI_
prefix removed), and the environment is checked first before consulting the in-memory store.
Parameters:
-
key
(str
) –The key to retrieve.
-
default
(Any
, default:None
) –The value to return if the key is not found.
Returns:
-
Any
(Any
) –The value associated with the key, or the default value.
Raises:
-
CommandError
–If the key is not found and no default is provided.
Source code in src/bijux_cli/services/config.py
list_keys ¶
Returns a list of all configuration keys.
Returns:
-
list[str]
–list[str]: A list of all keys in the configuration.
load ¶
Loads configuration from a .env
file.
This method reads a specified .env
file, parsing KEY=VALUE
pairs. It handles comments, validates syntax, and normalizes keys. If no path is given, it uses the default path from .env
or environment.
Parameters:
-
path
(str | Path | None
, default:None
) –Path to the
.env
file. If None, uses the default path from the environment or project structure.
Raises:
-
FileNotFoundError
–If a specified config file does not exist.
-
ValueError
–If a line is malformed or contains non-ASCII characters.
-
CommandError
–If the file is binary or another parsing error occurs.
Source code in src/bijux_cli/services/config.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
reload ¶
Reloads configuration from the last-loaded file path.
Raises:
-
CommandError
–If no file path has been previously loaded.
Source code in src/bijux_cli/services/config.py
save ¶
Persists the current in-memory configuration to its source file.
Source code in src/bijux_cli/services/config.py
set ¶
Sets a single configuration key-value pair and persists it.
Parameters:
-
key
(str
) –The key to set (case-insensitive,
BIJUXCLI_
prefix optional). -
value
(Any
) –The value to associate with the key.
Returns:
-
None
(None
) –
Raises:
-
CommandError
–If the configuration cannot be persisted.
Source code in src/bijux_cli/services/config.py
set_many ¶
Sets multiple key-value pairs and persists them to the config file.
Parameters:
-
items
(dict[str, Any]
) –A dictionary of key-value pairs to set.
Source code in src/bijux_cli/services/config.py
unset ¶
Removes a configuration key (alias for delete
).
Parameters:
-
key
(str
) –The key to remove.