Formats
anycfg.formats: the contract a format implements and the registry that
chooses one. The built-in formats and their dialects are described in the
Formats guide.
The contract
anycfg.formats.Format
Base class for a configuration format.
A format turns document text into data and, optionally, data back into text. It never touches a file, a stream or the environment: the loader reads the bytes, decodes them and hands the text over, so confinement, encoding and the policy apply to every format alike.
Subclass it, set the class attributes, override :meth:loads and any of the
optional methods, then register the class with
:func:anycfg.formats.register_format (or the anycfg.formats
entry-point group).
Class attributes
name: The registry name, lower-case ("yaml").
aliases: Other names format= accepts (("yml",)).
suffixes: File suffixes this format claims, lower-case with the dot
((".yaml", ".yml")); matched case-insensitively, longest first.
priority: Breaks a tie when two formats claim the same suffix.
load_options: The keywords :meth:loads accepts; any other is refused
with TypeError before the format is called.
dump_options: The keywords :meth:dumps accepts, likewise.
binary: When true, :meth:loads receives bytes instead of text.
Capabilities follow from what is overridden: :func:capabilities reports
"load_all" when :meth:loads_all is and "dump" when :meth:dumps
is. A format holds no state, so one instance serves every load.
Errors: a malformed document raises
:class:~anycfg.exceptions.ParseError, translated from the parser's
own error and raised after the except block, so neither
__cause__ nor __context__ keeps the parser's error (which may hold
the document), and carrying no document text itself.
aliases = ()
class-attribute
binary = False
class-attribute
dump_options = frozenset()
class-attribute
load_options = frozenset()
class-attribute
name = ''
class-attribute
priority = 0
class-attribute
suffixes = ()
class-attribute
__repr__()
Source code in src/anycfg/formats/_base.py
103 104 | |
claims_document(data)
classmethod
Whether data, a whole document as str or bytes, names this format.
By default: a #! first line naming it or one of its aliases
(#!json, #!/usr/bin/env yaml). A format whose documents carry
their own marker (YAML's %YAML) overrides this, calling super().
It must decide from the document's start and never by trying to parse.
Source code in src/anycfg/formats/_base.py
57 58 59 60 61 62 63 64 65 66 67 | |
dumps(obj, /, **options)
Serialize obj. Override for a format that can write.
Raises :class:~anycfg.exceptions.ConfigTypeError for a value the
format cannot represent, before anything is written.
Source code in src/anycfg/formats/_base.py
95 96 97 98 99 100 101 | |
loads(data, /, *, context=None, **options)
Parse one document from data and return plain data.
context is None for a direct call. A format with includes reads
them through context.include(...), and refuses them without one.
Source code in src/anycfg/formats/_base.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
loads_all(data, /, *, context=None, **options)
Yield every document in data. Override for a multi-document format.
Source code in src/anycfg/formats/_base.py
84 85 86 87 88 89 90 91 92 93 | |
anycfg.formats.capabilities(fmt)
What fmt can do: "load", plus "load_all" and "dump" when overridden.
Source code in src/anycfg/formats/_base.py
149 150 151 152 153 154 155 156 157 | |
The registry
anycfg.formats.register_format(cls=None, *, replace=False)
Register a format class; usable bare or as a decorator.
Raises ValueError when another class holds one of its names, or claims
one of its suffixes at the same priority, unless replace is true. A
registered format can be selected by documents, like a built-in.
Source code in src/anycfg/formats/__init__.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
anycfg.formats.unregister_format(name)
Remove the format registered under name; return whether one was.
Source code in src/anycfg/formats/__init__.py
239 240 241 242 243 244 245 246 247 | |
anycfg.formats.names()
Every registered format name, sorted.
Source code in src/anycfg/formats/__init__.py
250 251 252 253 | |
anycfg.formats.get(name, *, allowed=None)
The format class registered under name or one of its aliases.
allowed, when given, is the set of format names that may be returned; anything else is refused like an unknown name.
Raises:
| Type | Description |
|---|---|
UnsupportedFormatError
|
For a name nothing registers, listing the names that are. |
Source code in src/anycfg/formats/__init__.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
anycfg.formats.for_filename(filename, *, allowed=None)
Like :func:find_for_filename, but raises when no format claims the name.
Raises:
| Type | Description |
|---|---|
UnsupportedFormatError
|
Naming the registered formats. |
Source code in src/anycfg/formats/__init__.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
anycfg.formats.find_for_filename(filename, *, allowed=None)
The format a file name selects, or None when none claims it.
filename is a base name or a path; only the last component is used.
Source code in src/anycfg/formats/__init__.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
anycfg.formats.document_formats()
The formats a document may select by default: built-in and code-registered.
Source code in src/anycfg/formats/__init__.py
256 257 258 259 260 261 | |