zabgrab.adapters.configfiles¶
Config file discovery, loading and layering.
Layers, lowest to highest priority:
built-in defaults (
zabgrab.model.config.BUILTIN_DEFAULTS)system:
/etc/zabgrab/config.toml(Windows:%PROGRAMDATA%)user:
$XDG_CONFIG_HOME/zabgrab/config.toml, default~/.config/...on Linux and macOS (Windows:%APPDATA%unlessXDG_CONFIG_HOMEis set)project: the nearest
zabgrab.tomlin the working directory or its parents, stopping at the home directoryexplicit:
--config PATH/$ZABGRAB_CONFIG
--isolated skips layers 2-4.
Merge rules: each key in [defaults] is taken from the highest layer that sets it.
Named entries (servers.X, templates.X) are replaced whole by higher layers.
- zabgrab.adapters.configfiles.NAMED_SECTIONS¶
The tables whose entries are replaced whole by higher layers.
Functions
|
Add |
|
Add |
|
Find every config layer. |
|
Get a hint telling the user how to edit a config file. |
|
Find the nearest |
Describe a validation error for users. |
|
|
Read, validate and merge config layers. |
Get where the project config is, or would be created. |
|
|
Read and validate a single config file. |
|
Change the URL of |
|
Get the path of the system-wide config file. |
|
Get the path of the user's config file. |
|
Write a config file via a temporary file, so it's never left half-written. |
Classes
|
One config layer and its file, if any. |
|
The parts of the process environment that affect config discovery. |
|
Config layers, in increasing order of priority. |
|
The merged config, plus where each part of it came from. |
- class zabgrab.adapters.configfiles.LayerKind(*values)[source]¶
Bases:
StrEnumConfig layers, in increasing order of priority.
- class zabgrab.adapters.configfiles.Environment(variables, cwd, home, windows)[source]¶
Bases:
objectThe parts of the process environment that affect config discovery.
- variables¶
Environment variables.
- Type:
mapping of str to str
- cwd, home
The working and home directories.
- Type:
Path
- class zabgrab.adapters.configfiles.ConfigLayer(kind, path, exists, skipped=False)[source]¶
Bases:
objectOne config layer and its file, if any.
- path¶
Its file (
Nonefor built-in values, or no project file).- Type:
Path or None
- class zabgrab.adapters.configfiles.LoadedConfig(config, layers, sources=<factory>)[source]¶
Bases:
objectThe merged config, plus where each part of it came from.
- layers¶
Every layer, lowest priority first.
- Type:
tuple of ConfigLayer
- sources¶
Maps
("defaults", key),("servers", name)and("templates", name)to the source label of the layer that provided the value.
- zabgrab.adapters.configfiles.system_config_path(env)[source]¶
Get the path of the system-wide config file.
- Parameters:
env (Environment)
- Return type:
- zabgrab.adapters.configfiles.user_config_path(env)[source]¶
Get the path of the user’s config file.
- Parameters:
env (Environment)
- Return type:
- zabgrab.adapters.configfiles.find_project_config(env)[source]¶
Find the nearest
zabgrab.tomlin the working directory or its parents.The search stops before the home directory, so a file there is never treated as a project config.
- Parameters:
env (Environment) – Where to start, and the home directory.
- Returns:
Path or None – The project config file, if there is one.
- Return type:
Path | None
- zabgrab.adapters.configfiles.project_config_target(env)[source]¶
Get where the project config is, or would be created.
- Parameters:
env (Environment)
- Return type:
- zabgrab.adapters.configfiles.discover_layers(env, *, explicit=None, isolated=False)[source]¶
Find every config layer.
- Parameters:
env (Environment) – Where to look.
explicit (Path, optional) – A config file given with
--configor$ZABGRAB_CONFIG.isolated (bool, optional) – Skip the system, user and project layers (
--isolated).
- Returns:
list of ConfigLayer – The layers, lowest priority first, including ones without a file.
- Return type:
- zabgrab.adapters.configfiles.load_config(layers)[source]¶
Read, validate and merge config layers.
Each file is validated on its own first, so errors point at the file that caused them.
- Parameters:
layers (iterable of ConfigLayer) – The layers, lowest priority first.
- Returns:
LoadedConfig – The merged config and where each value came from.
- Raises:
ConfigError – If a file can’t be read or is invalid, or an explicitly given file doesn’t exist.
- Return type:
- zabgrab.adapters.configfiles.read_config_file(path, *, kind=None)[source]¶
Read and validate a single config file.
- Parameters:
path (Path) – The file.
kind (LayerKind, optional) – Its layer, for the hint on how to fix it.
- Returns:
dict – The file’s contents, as parsed TOML.
- Raises:
ConfigError – If the file can’t be read, isn’t valid TOML, or isn’t a valid config.
- Return type:
- zabgrab.adapters.configfiles.edit_hint(path, kind)[source]¶
Get a hint telling the user how to edit a config file.
- zabgrab.adapters.configfiles.format_validation_errors(exc)[source]¶
Describe a validation error for users.
- zabgrab.adapters.configfiles.add_server(path, name, url)[source]¶
Add
[servers.NAME]to a config file, keeping its existing formatting.The file (and its directory) is created if needed.
- Parameters:
- Raises:
ConfigError – If the file can’t be parsed, already defines the server, or can’t be written.
- Return type:
None
- zabgrab.adapters.configfiles.add_template(path, name, table, *, replace=False)[source]¶
Add
[templates.NAME]to a config file, keeping its existing formatting.The file (and its directory) is created if needed.
- Parameters:
path (Path) – The config file.
name (str) – The template’s name.
table (mapping) – The template’s settings, as config values (see
zabgrab.services.templates.template_table()).replace (bool, optional) – Replace a template with the same name (
--force).
- Returns:
str – The TOML of the template as written, for showing to the user.
- Raises:
ConfigError – If the file can’t be parsed or written, or already defines the template and
replaceisn’t set.- Return type:
- zabgrab.adapters.configfiles.set_server_url(path, name, url)[source]¶
Change the URL of
[servers.NAME]in a config file, keeping its formatting.- Parameters:
- Raises:
ConfigError – If the file can’t be parsed or written, or doesn’t define the server.
- Return type:
None
- zabgrab.adapters.configfiles.write_atomically(path, text)[source]¶
Write a config file via a temporary file, so it’s never left half-written.
- Parameters:
path (Path) – The file (created, with its directory, if needed).
text (str) – The new contents.
- Raises:
ConfigError – If the file can’t be written.
- Return type:
None