Config
duho.config
Read and write configuration documents in JSON, TOML, YAML and INI.
A format is a :class:ConfigBackend; the module functions pick one by name or
by file suffix. Importing this package loads no parser: each backend imports
its library when it is used.
__all__ = ['ConfigBackend', 'ConfigDependencyError', 'ConfigError', 'INIBackend', 'JSONBackend', 'TOMLBackend', 'UnsupportedFormatError', 'YAMLBackend', 'backend_for', 'backend_names', 'dump', 'dumps', 'get_backend', 'load', 'loads', 'user_path']
module-attribute
ConfigBackend(*, name=None, aliases=None, suffixes=None)
One configuration format: reads and writes a document as a plain value.
Subclass it, set name in the class body and implement loads and
dumps: the class is then registered and every duho.config function
and every config file duho reads can use it. replace=True in the class
statement takes over the names and suffixes of backends already registered
(the replaced class leaves the registry whole). A subclass that sets no
name of its own is not registered.
An instance may carry other name, aliases and suffixes than its
class: YAMLBackend(suffixes=(".conf",)). Names, aliases and suffixes are
lowercase and a suffix carries its dot; the suffix "" matches any file
name. extra names the pip extra a backend's library comes with.
Source code in src/duho/config/_backend.py
79 80 81 82 83 84 85 86 87 88 89 90 91 | |
aliases = ()
class-attribute
instance-attribute
extra = None
class-attribute
instance-attribute
name
instance-attribute
suffixes = ()
class-attribute
instance-attribute
__init_subclass__(*, replace=False, **kwargs)
Source code in src/duho/config/_backend.py
74 75 76 77 | |
__repr__()
Source code in src/duho/config/_backend.py
93 94 | |
dump(data, target)
Write data to a path (~ expanded) or a text file object.
The document is built first, so a failure leaves an existing file as it was.
Source code in src/duho/config/_backend.py
142 143 144 145 146 147 148 149 150 151 152 153 | |
dumps(data)
data as the text of a document.
Source code in src/duho/config/_backend.py
100 101 102 | |
load(source)
Read a document from a path (~ expanded) or a file object.
The bytes are strict UTF-8 and one leading byte-order mark is dropped.
A parse failure is a ConfigError that names the format and the file.
Source code in src/duho/config/_backend.py
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 | |
loads(text)
Parse text; a bad document raises ConfigError holding none of its values.
Source code in src/duho/config/_backend.py
96 97 98 | |
ConfigDependencyError(message='', extra=None, path=None)
Bases: ConfigError
A backend needs a library that is not installed; extra names the pip extra.
Source code in src/duho/exceptions.py
71 72 73 74 75 76 77 78 | |
extra = extra
instance-attribute
ConfigError(message='', path=None, lineno=None, colno=None)
Bases: ValueError
A configuration document could not be read or written.
str(exc) is the message. It never holds a value of the document; it
may name a key, a section, or a YAML alias or tag, cut to a bounded length.
path, lineno and colno locate the problem when known.
Source code in src/duho/exceptions.py
47 48 49 50 51 52 53 54 55 56 57 58 | |
colno = colno
instance-attribute
lineno = lineno
instance-attribute
message = message
instance-attribute
path = path
instance-attribute
__str__()
Source code in src/duho/exceptions.py
60 61 | |
INIBackend(*, name=None, aliases=None, suffixes=None)
Bases: ConfigBackend
INI through configparser, read as plain text with no interpolation.
The keys of [DEFAULT] are the top level, every other section is a table
one level deep, sections do not inherit [DEFAULT], key case is kept and
% is text. Every value is a string.
Source code in src/duho/config/_backend.py
79 80 81 82 83 84 85 86 87 88 89 90 91 | |
name = 'ini'
class-attribute
instance-attribute
suffixes = ('.ini', '.cfg')
class-attribute
instance-attribute
dumps(data)
Source code in src/duho/config/_ini.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
loads(text)
Source code in src/duho/config/_ini.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
JSONBackend(*, name=None, aliases=None, suffixes=None)
Bases: ConfigBackend
JSON through the standard library.
Source code in src/duho/config/_backend.py
79 80 81 82 83 84 85 86 87 88 89 90 91 | |
name = 'json'
class-attribute
instance-attribute
suffixes = ('.json',)
class-attribute
instance-attribute
dumps(data)
Source code in src/duho/config/_json.py
27 28 29 30 31 32 33 | |
loads(text)
Source code in src/duho/config/_json.py
17 18 19 20 21 22 23 24 25 | |
TOMLBackend(*, name=None, aliases=None, suffixes=None)
Bases: ConfigBackend
TOML: tomllib (or tomli) reads, tomli_w writes.
Source code in src/duho/config/_backend.py
79 80 81 82 83 84 85 86 87 88 89 90 91 | |
extra = 'config'
class-attribute
instance-attribute
name = 'toml'
class-attribute
instance-attribute
suffixes = ('.toml',)
class-attribute
instance-attribute
dumps(data)
Source code in src/duho/config/_toml.py
57 58 59 60 61 62 63 64 65 66 67 68 69 | |
loads(text)
Source code in src/duho/config/_toml.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
UnsupportedFormatError(message='', path=None, lineno=None, colno=None)
Bases: ConfigError
No backend matches the format name or the file name.
Source code in src/duho/exceptions.py
47 48 49 50 51 52 53 54 55 56 57 58 | |
YAMLBackend(*, name=None, aliases=None, suffixes=None)
Bases: ConfigBackend
YAML through PyYAML's safe loader and dumper.
Source code in src/duho/config/_backend.py
79 80 81 82 83 84 85 86 87 88 89 90 91 | |
aliases = ('yml',)
class-attribute
instance-attribute
extra = 'yaml'
class-attribute
instance-attribute
name = 'yaml'
class-attribute
instance-attribute
suffixes = ('.yaml', '.yml')
class-attribute
instance-attribute
dumps(data)
Source code in src/duho/config/_yaml.py
49 50 51 52 53 54 55 56 57 | |
loads(text)
Source code in src/duho/config/_yaml.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
backend_for(path, format=None, *, default=None, backends=None)
The backend for a file: format if given, else the longest suffix its
name ends with (case ignored), else default.
No match and no default is an UnsupportedFormatError listing the
accepted suffixes.
Source code in src/duho/config/_backend.py
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 | |
backend_names(*, backends=None)
The primary names of the backends in the set, sorted.
Source code in src/duho/config/_backend.py
210 211 212 213 | |
dump(data, target, format=None, *, backends=None)
Write data to a file (a path or a text file object).
Source code in src/duho/config/_backend.py
284 285 286 287 288 289 290 291 292 293 | |
dumps(data, format, *, backends=None)
data as a document of the named format.
Source code in src/duho/config/_backend.py
279 280 281 | |
get_backend(name, *, backends=None)
The backend answering to name or an alias, in any case.
Source code in src/duho/config/_backend.py
216 217 218 219 220 221 222 223 224 225 | |
load(source, format=None, *, backends=None)
Read a file (a path or a file object); the format is format or the file name's.
Source code in src/duho/config/_backend.py
271 272 273 274 275 276 | |
loads(text, format, *, backends=None)
Parse text as the named format.
Source code in src/duho/config/_backend.py
266 267 268 | |
user_path(app, *names)
The per-user configuration path <config dir>/<app>/<names...>.
The result resolves each time it is used as a path, so it is safe as a class
attribute (_config_ = user_path("tool", "config.toml")): the environment
is read when the file is looked up, not when the class is defined. The
configuration directory is APPDATA (when absolute, else
~/AppData/Roaming) on Windows and XDG_CONFIG_HOME (when absolute,
else ~/.config) elsewhere.
str(path) is the resolved path; repr shows the call.
Raises ValueError when app is empty.
Source code in src/duho/config/_paths.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |