Sources
anycfg.sources: what a loader reads that is not a file path, and the
registry of <scheme>:// strings. See the Sources guide
for how each kind of source is resolved.
The contract
anycfg.sources.Source
Base class for something a loader reads that is not a file path.
Override :meth:read. Set executes = True for a source that runs a
program: the loader then refuses it unless the policy allows commands,
before :meth:read is called.
executes = False
class-attribute
name
property
A name for messages and for the default merge key.
read(context)
Return what this source holds, under context's policy.
Source code in src/anycfg/sources/_base.py
88 89 90 | |
anycfg.sources.Payload
Bases: NamedTuple
What a source read.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
Any
|
Text or bytes for a format to parse, or the data itself when parsed is true. |
name |
str
|
The source's name, for messages and merge keys. |
identity |
str
|
A stable key for this source, used for include-cycle detection and de-duplication. |
format |
'_ty.Optional[str]'
|
The format name the source itself determined ( |
origin |
Any
|
The location relative includes resolve against, or |
parsed |
bool
|
data is already data, not text. |
trusted_format |
bool
|
format was chosen by the caller, so it may name any
registered format; one a source read from data (a command's
|
encoding |
'_ty.Optional[str]'
|
The codec data is in, and the default for its includes;
|
data
instance-attribute
encoding = None
class-attribute
instance-attribute
format = None
class-attribute
instance-attribute
identity
instance-attribute
name
instance-attribute
origin = None
class-attribute
instance-attribute
parsed = False
class-attribute
instance-attribute
trusted_format = False
class-attribute
instance-attribute
Built-in sources
anycfg.sources.Text(data, *, format=None, name='')
Bases: Source
In-memory content in a named format: Text("a: 1", format="yaml").
data is str or bytes (decoded with the load's encoding). Without
format, the content must name its own: a #!<format> first line (which
is removed) or a format's marker such as YAML's %YAML directive; such a
format is limited to the ones documents may select. Relative includes
inside it resolve against the loader's base_dir. name appears in
messages and is the merge key for collect="hash".
Source code in src/anycfg/sources/_base.py
132 133 134 135 136 137 138 139 140 141 142 143 | |
data = bytes(data) if isinstance(data, bytearray) else data
instance-attribute
format = format
instance-attribute
name
property
__repr__()
Source code in src/anycfg/sources/_base.py
163 164 | |
read(context)
Source code in src/anycfg/sources/_base.py
149 150 151 152 153 154 155 156 157 158 159 160 161 | |
anycfg.sources.Chunks(chunks, *, format=None, name='')
Bases: Source
Content arriving in pieces: any iterable of str or bytes chunks.
Chunks(response.iter_content(), format="json"). The chunks are joined as
they arrive and the read stops once they pass the policy's max_bytes. The
format is format, else the one name's suffix selects. An iterator is read
once, so a source built on one loads once.
Source code in src/anycfg/sources/_base.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
chunks = chunks
instance-attribute
format = format
instance-attribute
name
property
__repr__()
Source code in src/anycfg/sources/_base.py
235 236 | |
read(context)
Source code in src/anycfg/sources/_base.py
225 226 227 228 229 230 231 232 233 | |
anycfg.sources.Data(data, name='')
Bases: Source
Python data as a source: a mapping or a list, merged like a document.
The data is deep-copied when read, so a later merge or interpolation never
reaches back into the caller's object. A Mapping or a list passed
straight to a loader is wrapped in one of these.
Source code in src/anycfg/sources/_base.py
101 102 103 | |
data = data
instance-attribute
name
property
__repr__()
Source code in src/anycfg/sources/_base.py
117 118 | |
read(context)
Source code in src/anycfg/sources/_base.py
109 110 111 112 113 114 115 | |
anycfg.sources.Env(prefix='', *, nested=None, lowercase=False, coerce=False, environ=None)
Bases: Source
Environment variables as a configuration document.
Only variables starting with prefix are read, with the prefix removed.
With nested ("__"), APP_DB__PORT becomes {"DB": {"PORT": ...}};
a variable that is both a value and a parent of nested keys raises, naming
both. Values stay strings — types come from hydration (load(into=...))
— unless coerce is set: True reads each value by the YAML 1.2 core
rules (80 an int, true a bool, null None; yes and 0755
stay text), a callable (text) -> value decides itself.
Keys keep their case unless lowercase is set. On Windows the prefix is
matched case-insensitively, as the environment is.
environ replaces os.environ, for tests and for a captured snapshot.
Source code in src/anycfg/sources/_env.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
coerce = coerce
instance-attribute
environ = environ
instance-attribute
lowercase = lowercase
instance-attribute
name
property
nested = nested
instance-attribute
prefix = prefix
instance-attribute
__repr__()
Source code in src/anycfg/sources/_env.py
135 136 | |
read(context)
Source code in src/anycfg/sources/_env.py
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 | |
anycfg.sources.Command(command, *, format=None, timeout=_UNSET, encoding=None)
Bases: Source
Run a command and parse its standard output.
command is either a scheme string — cmd://..., exec://... or
sh://..., optionally naming the output format as cmd+json://... —
or a bare command line. Both run through the shell. Use
:meth:script for a script file, which runs through its interpreter
without a shell.
The output format comes from, in order: format, the +fmt of the
scheme, a #!<format> first line of the output (which is then dropped),
and for a script the suffix before its own (secrets.json.sh); then the
loader's own format=. Output with no format raises: content is never
sniffed. Includes inside the output are read with the same encoding.
The command runs with stdin closed and a timeout (the policy's
command_timeout unless timeout is given), after which it and its
child processes are killed. Output is decoded strictly with encoding
(the load's encoding by default). A non-zero exit raises
:class:~anycfg.exceptions.CommandError, whose message carries the
tail of stderr and never stdout.
Source code in src/anycfg/sources/_command.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
command = command[match.end():]
instance-attribute
encoding = encoding
instance-attribute
executes = True
class-attribute
instance-attribute
format = format
instance-attribute
name
property
text = command
instance-attribute
timeout = timeout
instance-attribute
__repr__()
Source code in src/anycfg/sources/_command.py
288 289 | |
read(context)
Source code in src/anycfg/sources/_command.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
script(path, *, format=None, timeout=_UNSET, encoding=None)
classmethod
A script file (.sh, .bat, .cmd, .ps1), run without a shell.
Source code in src/anycfg/sources/_command.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
Schemes
anycfg.sources.register_scheme(scheme, factory, *, replace=False)
Make <scheme>://... strings load through factory(text).
Raises ValueError when scheme is taken, unless replace is true.
Source code in src/anycfg/sources/__init__.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
anycfg.sources.unregister_scheme(scheme)
Remove scheme; return whether it was registered.
Source code in src/anycfg/sources/__init__.py
132 133 134 135 136 137 138 139 140 | |
anycfg.sources.schemes()
Every registered scheme, sorted.
Source code in src/anycfg/sources/__init__.py
143 144 145 146 | |
anycfg.sources.from_text(text, *, allow_plugins=True)
The source text names, or None when it is not a registered scheme.
allow_plugins false limits the lookup to built-in and code-registered schemes, which is what a document is allowed to reach.
Source code in src/anycfg/sources/__init__.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |