!import
tag
Constructor
ImportConstructor
dataclass
Custom PyYAML constructor for the !import
tag, which loads an entire file into the current
document.
As a Constructor, it can be called with a yaml.Loader
and a yaml.Node
to attempt to
construct a given node tagged as !import
into a Python object. In any valid use of the tag,
this node should always be a scalar string, e.g.:
To standardize the parsing of the tag's argument, the Constructor uses an
ImportSpec
dataclass to hold the path to the file to be imported, as a relative path.
Methods:
Name | Description |
---|---|
__call__ |
Construct a node tagged as |
load |
Using a specified loader type, load the contents of the file specified in the
|
Source code in yaml_extras/yaml_import.py
__call__(loader, node)
Using the specified loader, attempt to construct a node tagged as !import
into a Python
object.
For any valid use of the tag, the node should always be a scalar string in the form of a relative file path.
Heavily inspired by @tanbro's pyyaml-include library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loader
|
Loader
|
YAML loader |
required |
node
|
Node
|
|
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
Result of loading the file's contents using the specified loader. |
Source code in yaml_extras/yaml_import.py
load(loader_type, import_spec)
Utility function to load the contents of the file specified in the ImportSpec
dataclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loader_type
|
Type[Loader]
|
YAML loader type |
required |
import_spec
|
ImportSpec
|
Dataclass containing the path to the file to be imported. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
Result of loading the file's contents using the specified loader type. |
Source code in yaml_extras/yaml_import.py
Utility dataclass
ImportSpec
dataclass
Small utility dataclass for typing the parsed argument to the !import
tag. E.g.,
Shall be parsed as,
Attributes:
Name | Type | Description |
---|---|---|
path |
Path
|
Relative path to the file to be imported |
Methods:
Name | Description |
---|---|
from_str |
Parse a string into an |
Source code in yaml_extras/yaml_import.py
from_str(path_str)
classmethod
Parse a string into an ImportSpec
dataclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_str
|
str
|
Relative path to the file to be imported |
required |
Returns:
Name | Type | Description |
---|---|---|
ImportSpec |
ImportSpec
|
Dataclass containing the path to the file to be imported. |