Loader
The core orchestrator plus the standard-library-style module functions.
yaconfiglib.loader.ConfigLoader(base_dir='', *, encoding=None, path_factory=None, loader_factory=None, recursive=None, key_factory=None, log_level=LogLevel.Warning, interpolate=None, merge=ConfigLoaderMergeMethod.Simple, merge_options=None, ignore_error=False, inject_env=False, strict=False, allow_commands=True, sandbox=False)
Bases: ConfigBackend
Orchestrates loading, merging, and interpolating configuration sources.
A ConfigLoader is the main entrypoint for hiera-like configuration
loading: given one or more sources (file paths, glob patterns, command
URIs, in-memory strings, or nested lists thereof), it resolves each
source to the appropriate :class:~yaconfiglib.backends.base.ConfigBackend,
parses it, and merges the results together in order using a configurable
:class:ConfigLoaderMergeMethod/:class:~yaconfiglib.utils.merge.Merge
strategy. Optionally, the merged result is interpolated with Jinja2
(see :attr:interpolate) and wrapped in a :class:DotAccessibleDict
for config.some.nested.key style access.
Most users can use the module-level :func:load/:func:loads helpers,
which construct a ConfigLoader for a single call. Construct a
ConfigLoader instance directly when you need to reuse the same
settings (base directory, merge strategy, interpolation options) across
multiple loads.
Configure a reusable loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
str | Path
|
Directory relative-path sources are resolved
against. Accepts a string or |
''
|
encoding
|
str
|
Default text encoding for reading sources. |
None
|
path_factory
|
Callable[[str], Path]
|
Callable used to build a |
None
|
loader_factory
|
type[ConfigBackend]
|
Callable |
None
|
recursive
|
bool
|
Whether glob sources should recurse into subdirectories by default. |
None
|
key_factory
|
Callable[[Path, object], str]
|
Callable |
None
|
log_level
|
int | LogLevel
|
Logging verbosity for this loader's module logger. |
Warning
|
interpolate
|
bool
|
If True, run Jinja2 interpolation over the merged
result after loading (see :func: |
None
|
merge
|
ConfigLoaderMergeMethod | Merge
|
The merge strategy applied between successive sources —
a :class: |
Simple
|
merge_options
|
dict[str]
|
Extra keyword options forwarded to the merge
callable on every call (e.g. |
None
|
ignore_error
|
_IgnoreError | bool
|
Either a bool (ignore/re-raise all load errors
uniformly) or a predicate |
False
|
inject_env
|
bool
|
If True and interpolate is set, expose
|
False
|
strict
|
bool
|
If True, undefined Jinja2 variables raise during interpolation instead of rendering as empty. |
False
|
allow_commands
|
bool
|
If False, loading a command source ( |
True
|
sandbox
|
bool
|
If True, interpolation runs in Jinja2's
|
False
|
Source code in src/yaconfiglib/loader.py
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
allow_commands = bool(allow_commands)
instance-attribute
base_dir
property
writable
encoding = encoding or self.DEFAULT_ENCODING
instance-attribute
ignore_error = ignore_error if callable(ignore_error) else (lambda error, *args, **kwargs: bool(ignore_error))
instance-attribute
inject_env = bool(inject_env)
instance-attribute
interpolate = False if interpolate is None else bool(interpolate)
instance-attribute
key_factory = key_factory or (lambda path, value: path.stem)
instance-attribute
loader_factory = loader_factory or (lambda path: ConfigBackend.get_class_by_path(path)())
instance-attribute
merge = merge if isinstance(merge, Merge) else ConfigLoaderMergeMethod(merge)
instance-attribute
merge_options = {} if merge_options is None else merge_options
instance-attribute
path_factory = path_factory or self.DEFAULT_PATH_FACTORY
instance-attribute
recursive = False if recursive is None else recursive
instance-attribute
sandbox = bool(sandbox)
instance-attribute
strict = bool(strict)
instance-attribute
load(*pathname, recursive=None, encoding=None, loader=None, transform=None, default=None, key_factory=None, flatten=False, interpolate=None, merge=None, merge_options=None, allow_commands=None, sandbox=None, **reader_args)
Load, merge, and (optionally) interpolate one or more configuration sources.
Each item in pathname is resolved via
:func:~yaconfiglib.utils.source.parse_sources (expanding globs,
nested lists, in-memory #!-marked strings, streams, and command
URIs), parsed with the backend selected for it, and merged into the
running result in order using merge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*pathname
|
SourceLike
|
One or more sources — file paths, glob patterns,
command URIs ( |
()
|
recursive
|
bool
|
Overrides the instance's recursive for glob expansion during this call. |
None
|
encoding
|
str
|
Overrides the instance's encoding for this call. |
None
|
loader
|
str
|
Backend name, backend instance, or callable selecting the backend for every source loaded in this call, overriding per-source auto-detection. |
None
|
transform
|
str
|
A Jinja2 expression string evaluated against each
loaded document (as |
None
|
default
|
object
|
Initial value merged against, used when pathname yields no sources. |
None
|
key_factory
|
str | Callable[[Path], str]
|
Overrides the instance's key_factory for this call. |
None
|
flatten
|
bool
|
If True, the final merged result (expected to be a mapping-of-mappings or sequence-of-sequences) is flattened one level — useful when each source contributes items to a shared top-level collection instead of being keyed by itself. |
False
|
interpolate
|
bool
|
Overrides the instance's interpolate for this call. |
None
|
merge
|
ConfigLoaderMergeMethod | Merge
|
Overrides the instance's merge strategy for this call. |
None
|
merge_options
|
dict[str]
|
Overrides the instance's merge_options for this call. |
None
|
**reader_args
|
object
|
Additional keyword arguments forwarded to each
backend's |
{}
|
Returns:
| Type | Description |
|---|---|
object
|
The merged (and possibly interpolated) result. Dict results |
object
|
are wrapped in :class: |
Source code in src/yaconfiglib/loader.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 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 | |
load_all(*pathname, encoding=None, interpolate=None, **reader_args)
Yield each source's parsed (and optionally interpolated) document individually, without merging.
Unlike :meth:load, which merges every source into a single
result, this generator yields one value per resolved source —
useful when sources represent independent documents rather than
layers of the same configuration (e.g. iterating a directory of
unrelated config files).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*pathname
|
Path | Sequence[Path]
|
Sources to resolve, same semantics as :meth: |
()
|
encoding
|
str
|
Overrides the instance's encoding for this call. |
None
|
interpolate
|
bool
|
Overrides the instance's interpolate for this call; applied independently to each yielded document. |
None
|
**reader_args
|
object
|
Additional keyword arguments forwarded to each
backend's |
{}
|
Yields:
| Type | Description |
|---|---|
object
|
Each source's parsed document, with dict results wrapped in |
object
|
class: |
Source code in src/yaconfiglib/loader.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | |
load_as(model_cls, *pathname, **kwargs)
Load configuration sources and instantiate as model_cls.
Supports Pydantic models (if installed) or dataclasses. If neither matches, falls back to passing kwargs/dict unpacking to the constructor.
Source code in src/yaconfiglib/loader.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | |
yaconfiglib.loader.DotAccessibleDict
Bases: dict
Dictionary subclass supporting dot-notation queries and attribute access.
__getattr__(name)
Source code in src/yaconfiglib/loader.py
623 624 625 626 627 628 629 630 631 | |
__setattr__(name, value)
Source code in src/yaconfiglib/loader.py
633 634 | |
get(key, default=None, dig=True)
Support dot-notation traversal, e.g., get("database.credentials.user", dig=True).
Source code in src/yaconfiglib/loader.py
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 | |
yaconfiglib.loader.ConfigLoaderMergeMethod
Bases: _ConfigLoaderMergeMethod, MergeMethod, Protocol
Module-level functions
yaconfiglib.loader.load(fp, **kwargs)
Load configuration from a file pointer or file path.
Source code in src/yaconfiglib/loader.py
669 670 671 672 673 674 675 | |
yaconfiglib.loader.loads(s, **kwargs)
Load configuration from a string or bytes in memory.
Source code in src/yaconfiglib/loader.py
678 679 680 681 682 683 684 685 686 687 688 689 690 | |
yaconfiglib.loader.dump(obj, fp, **kwargs)
Dump configuration object to a file pointer or file path.
Source code in src/yaconfiglib/loader.py
693 694 695 696 697 698 699 700 | |
yaconfiglib.loader.dumps(obj, **kwargs)
Dump configuration object to string (delegates to YamlConfig dumper by default).
Source code in src/yaconfiglib/loader.py
703 704 705 706 707 | |