Utilities
Shared building blocks used internally by the loader and backends, and useful directly for advanced use cases (custom backends, custom merge strategies).
Merging
yaconfiglib.utils.merge.MergeMethod
Bases: IntEnum
Built-in merge strategies.
Deep = 2
class-attribute
instance-attribute
Simple = 1
class-attribute
instance-attribute
Substitute = 3
class-attribute
instance-attribute
__call__(a, b, *, memo=None, **options)
Source code in src/yaconfiglib/utils/merge.py
69 70 71 72 73 74 75 76 77 78 | |
yaconfiglib.utils.merge.Merge
Bases: Protocol
Protocol for any callable that merges two objects.
__call__(a, b, *, memo=None, **options)
Source code in src/yaconfiglib/utils/merge.py
52 53 54 55 56 57 58 59 | |
yaconfiglib.utils.merge.is_scalar(obj)
Source code in src/yaconfiglib/utils/merge.py
27 28 | |
yaconfiglib.utils.merge.is_array(obj, mutable=False)
Return True if obj is a sequence but not a mapping.
When mutable is True, also require that the sequence supports item
assignment (i.e. is a :class:~typing.MutableSequence).
Source code in src/yaconfiglib/utils/merge.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
Jinja2 interpolation
yaconfiglib.utils.jinja2.interpolate(data, globals=None, environment=None)
Recursively interpolate Jinja2 templates within data.
- Strings: rendered as Jinja2 templates. A bare
{{ expr }}(no surrounding text) is evaluated as a Python expression so that the return type is preserved (e.g. an integer stays an integer). - Mappings: keys and values are interpolated recursively.
- Sequences: each element is interpolated recursively.
Returns the interpolated object (may differ in type from data for pure-expression strings).
Source code in src/yaconfiglib/utils/jinja2.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
yaconfiglib.utils.jinja2.load_template(source, name=None, filename=None, environment=None, globals=None)
Compile source into a :class:~jinja2.Template.
Source code in src/yaconfiglib/utils/jinja2.py
25 26 27 28 29 30 31 32 33 34 35 | |
yaconfiglib.utils.jinja2.compile(code, environment=None, globals=None)
Return a render callable for code (a Jinja2 template string).
Source code in src/yaconfiglib/utils/jinja2.py
71 72 73 74 75 76 77 78 79 80 81 82 83 | |
yaconfiglib.utils.jinja2.eval(code, environment=None, globals=None)
Return a callable that evaluates code as a Jinja2 expression.
The expression result is captured via a {% do %} statement and
returned from the callable, preserving non-string Python types.
Source code in src/yaconfiglib/utils/jinja2.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
Source discovery
yaconfiglib.utils.source.parse_sources(sources, base_dir=None, encoding=None, memo=None, path_factory=None, recursive=None)
Resolve sources into a flat stream of loadable :class:Path-like objects.
Each item in sources may be:
- A file path (string or
Path) — resolved against base_dir if relative and not a command URI, and glob-expanded if it contains glob magic characters. - A command URI (
exec://,cmd://,sh://, or a+fmtvariant) — passed through unresolved and unexpanded so :class:~yaconfiglib.backends.command.CommandBackendcan run it. - An in-memory document: a string/bytes value starting with the
"#!\n"marker, where the first line (after the marker) is treated as a virtual filename and the remainder as its content. The content is materialized to aMemPath(or a real temp file as a fallback) so downstream backends can read it like any other file. - An open stream (:class:
io.IOBase) — read fully and materialized the same way as an in-memory document. - A nested iterable of any of the above — flattened recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sources
|
Iterable[SourceLike | Iterable[SourceLike]]
|
The sources to resolve, as passed to |
required |
base_dir
|
Path
|
Directory relative file paths are joined against. |
None
|
encoding
|
str
|
Text encoding used when decoding bytes markers/content. |
None
|
memo
|
Iterable[str | Path]
|
Optional set of already-seen path strings, used to detect and skip duplicate sources across recursive calls; mutated in place. Any iterable is accepted and normalized to a set (O(1) membership; the previous list made duplicate detection O(n²)). |
None
|
path_factory
|
type[Path]
|
Constructor used to build a |
None
|
recursive
|
bool
|
Whether glob expansion should recurse into subdirectories. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
Resolved |
Path
|
class: |
Path
|
(glob patterns may yield zero or many). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an item in sources is not a recognized source type. |
Source code in src/yaconfiglib/utils/source.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 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 259 | |
yaconfiglib.utils.source.has_glob_pattern(path)
Check if the given Path contains glob pattern characters.
Source code in src/yaconfiglib/utils/source.py
80 81 82 83 84 85 | |