yaml_loaders
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| SupportsRead configz.yaml_tools.yaml_loaders |
🛈 DocStrings¶
YAML handling utilities with enhanced loading and dumping capabilities.
get_env_constructor
¶
Construct a YAML tag that references environment variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
Loader
|
YAML loader instance |
required |
node
|
Node
|
Current YAML node being processed |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The resolved environment variable value or default value |
Raises:
| Type | Description |
|---|---|
ConstructorError
|
If node is neither scalar nor sequence |
Source code in src/configz/yaml_tools/yaml_loaders.py
get_include_constructor
¶
get_include_constructor(
fs: JoinablePathLike | AbstractFileSystem | None = None, **kwargs: Any
) -> Constructor
Create a YAML include (!include) constructor with fsspec filesystem support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fs
|
JoinablePathLike | AbstractFileSystem | None
|
Filesystem specification (path or fsspec filesystem object) |
None
|
kwargs
|
Any
|
Additional arguments for the Constructor |
{}
|
Returns:
| Type | Description |
|---|---|
Constructor
|
Configured YAML include constructor |
Source code in src/configz/yaml_tools/yaml_loaders.py
get_jinja_constructor
¶
get_jinja_constructor(
env: Environment | None, resolve_strings: bool = True, resolve_dict_keys: bool = False
) -> Callable[[Loader, Node], Any]
Create a constructor that resolves strings using a MiniJinja environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment | None
|
MiniJinja environment to use for template resolution |
required |
resolve_strings
|
bool
|
Whether to resolve string values |
True
|
resolve_dict_keys
|
bool
|
Whether to resolve dictionary keys |
False
|
Returns:
| Type | Description |
|---|---|
Callable[[Loader, Node], Any]
|
Constructor function for YAML loader |
Source code in src/configz/yaml_tools/yaml_loaders.py
get_loader
¶
get_loader(
base_loader_cls: LoaderType,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
enable_include: bool = True,
enable_env: bool = True,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
) -> LoaderType
Construct an enhanced YAML loader with optional support for !env and !include tags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_loader_cls
|
LoaderType
|
Base loader class to extend |
required |
include_base_path
|
JoinablePathLike | AbstractFileSystem | None
|
Base path for !include tag resolution. If None, use cwd. |
None
|
enable_include
|
bool
|
Whether to enable !include tag support |
True
|
enable_env
|
bool
|
Whether to enable !ENV tag support |
True
|
resolve_strings
|
bool
|
Whether to resolve strings using MiniJinja |
False
|
resolve_dict_keys
|
bool
|
Whether to resolve dictionary keys using MiniJinja |
False
|
variables
|
dict[str, Any] | None
|
An optional dictionary to resolving !var tags |
None
|
jinja_env
|
Environment | None
|
Optional MiniJinja environment for template resolution |
None
|
Returns:
| Type | Description |
|---|---|
LoaderType
|
Enhanced loader class |
Source code in src/configz/yaml_tools/yaml_loaders.py
get_safe_loader
¶
Create a SafeLoader with dummy constructors for common tags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_loader_cls
|
LoaderType
|
Base loader class to extend |
required |
Returns:
| Type | Description |
|---|---|
LoaderType
|
Enhanced safe loader class |
Source code in src/configz/yaml_tools/yaml_loaders.py
load_yaml
¶
load_yaml(
text: YAMLInput,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool | JoinablePathLike = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: None = None,
enable_env: bool = True,
) -> Any
load_yaml(
text: YAMLInput,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool | JoinablePathLike = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: type[T],
enable_env: bool = True,
) -> T
load_yaml(
text: YAMLInput,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool | JoinablePathLike = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: type[T] | None = None,
enable_env: bool = True,
) -> T | Any
load_yaml(
text: YAMLInput,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool | JoinablePathLike = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: type[T] | None = None,
enable_env: bool = True,
) -> Any | T
Load a YAML string with specified safety mode and include path support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
YAMLInput
|
The YAML content to load |
required |
mode
|
LoaderStr | LoaderType
|
YAML loader safety mode ('unsafe', 'full', or 'safe') Custom YAML loader classes are also accepted. |
'unsafe'
|
include_base_path
|
JoinablePathLike | AbstractFileSystem | None
|
Base path for resolving !include directives |
None
|
resolve_strings
|
bool
|
Whether to resolve MiniJinja template strings |
False
|
resolve_dict_keys
|
bool
|
Whether to resolve MiniJinja templates in dictionary keys |
False
|
resolve_inherit
|
bool | JoinablePathLike
|
Whether to resolve INHERIT directives If True, then requires IO object with name attribute for text If str or PathLike, its interpreted as the base path |
False
|
jinja_env
|
Environment | None
|
Optional MiniJinja environment for template resolution |
None
|
variables
|
dict[str, Any] | None
|
An optional dictionary to resolving !var tags |
None
|
verify_type
|
type[T] | None
|
Type to verify and cast the output to (supports TypedDict) |
None
|
enable_env
|
bool
|
Whether to enable the !ENV tag |
True
|
Returns:
| Type | Description |
|---|---|
Any | T
|
The parsed YAML data, typed according to verify_type if provided |
Example
# Simple YAML loading
data = load_yaml("key: value")
# With TypedDict verification
from typing import TypedDict
class Config(TypedDict):
host: str
port: int
config = load_yaml('''
host: localhost
port: 8080
''', verify_type=Config)
# With MiniJinja template resolution
from minijinja import Environment
env = Environment(globals={"name": "World"})
data = load_yaml(
"message: Hello {{ name }}!",
resolve_strings=True,
jinja_env=env
)
Source code in src/configz/yaml_tools/yaml_loaders.py
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 | |
load_yaml_file
¶
load_yaml_file(
path: JoinablePathLike,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: None = None,
enable_env: bool = True,
inherit_from: list[str] | str | None = None,
) -> Any
load_yaml_file(
path: JoinablePathLike,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: type[T],
enable_env: bool = True,
inherit_from: list[str] | str | None = None,
) -> T
load_yaml_file(
path: JoinablePathLike,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: type[T] | None = None,
enable_env: bool = True,
inherit_from: list[str] | str | None = None,
) -> T | Any
load_yaml_file(
path: JoinablePathLike,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: type[T] | None = None,
enable_env: bool = True,
inherit_from: list[str] | str | None = None,
) -> Any | T
Load a YAML file with specified options.
Use verify_type to get proper output type for LSPs and runtime validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
JoinablePathLike
|
Path to the YAML file to load |
required |
mode
|
LoaderStr | LoaderType
|
YAML loader safety mode ('unsafe', 'full', or 'safe'). Custom YAML loader classes are also accepted. |
'unsafe'
|
include_base_path
|
JoinablePathLike | AbstractFileSystem | None
|
Base path for resolving !include directives |
None
|
resolve_inherit
|
bool
|
Whether to resolve INHERIT directives |
False
|
resolve_strings
|
bool
|
Whether to resolve MiniJinja template strings |
False
|
resolve_dict_keys
|
bool
|
Whether to resolve MiniJinja templates in dictionary keys |
False
|
jinja_env
|
Environment | None
|
Optional MiniJinja environment for template resolution |
None
|
variables
|
dict[str, Any] | None
|
An optional dictionary to resolving !var tags |
None
|
storage_options
|
dict[str, Any] | None
|
Additional keywords to pass to fsspec backend |
None
|
verify_type
|
type[T] | None
|
Type to verify and cast the output to (supports TypedDict) |
None
|
enable_env
|
bool
|
Whether to enable the !ENV tag |
True
|
inherit_from
|
list[str] | str | None
|
Additional paths to inherit from. These form the base layer, meaning explicit INHERIT directives in the file take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
Any | T
|
The parsed YAML data |
Example
from typing import TypedDict
class Config(TypedDict):
database: str
port: int
debug: bool
# Load YAML file with inheritance and TypedDict verification
config = load_yaml_file(
"config.yml",
resolve_inherit=True, # Resolve INHERIT directives
include_base_path="configs/", # Base path for includes
verify_type=Config # Verify against TypedDict
)
Source code in src/configz/yaml_tools/yaml_loaders.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 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 618 619 620 621 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 | |