simplelogincmd.config.Config¶
- class Config(user_config_path: Path | None = None, base_obj: dict | None = None, schema_obj: dict | None = None)¶
Bases:
objectManage application configuration
Constructor
Construct a complete configuration given a base set, a user- specific set that is merged into the base, and a JSON schema that validates both, as well as any attempts to modify values after initialization.
Assuming both the user config file and the base config object pass validation, the final full configuration will be the base config, plus any values defined in the user config. Further, any values defined in both sets resolve to those in the user config.
If the user configuration fails validation, merging is quietly not performed. That is, the final configuration will be the same as the base config with no modifications. This is also the case when reading the file fails due either to an OS error or a JSON decoding error.
If the base configuration or the schema itself fail validation, exceptions are raised. This ensures that at least one valid configuration is available to the application.
base_obj and schema_obj are deep-copied, so users can feel free to use them elsewhere without fear of modification by this class.
- Parameters:
user_config_path (
pathlib.Path, optional) – Path to the user’s configuration file, defaults to a path defined by the applicationbase_obj (dict) – The default configuration, defaults to the application’s default config
schema_obj (dict) – The JSON schema that defines the structure of config files/objects, defaults to a schema defined by the application
- Raises:
jsonschema.ValidationError – If base_obj is invalid according to the schema
jsonschema.SchemaError – If the schema itself is invalid
Methods
Construct a set of all configuration keys with their values
Ensure that the parent directory of the config file exists
Retrieve a configuration value
Restore all configuration to their default values
Save the configuration to a file
Attempt to modify a config setting
Validate a configuration
- all() dict¶
Construct a set of all configuration keys with their values
- ensure_directory() bool¶
Ensure that the parent directory of the config file exists
Attempt to create it if it does not.
- Returns:
Whether the directory now exists
- Return type:
bool
- get(key: str) Any¶
Retrieve a configuration value
- Parameters:
key (str) – A dot-separated path from the config’s root to the value desired (e.g., “api.api-key”) to get the value of api-key under the api section.
- Raises:
KeyError – If the given key leads to no value
- Returns:
The config value requested. If the given path leads to an object, return a deep copy of that object in order to prevent inadvertently making un-validated changes to the configuration
- Return type:
Any
- restore() None¶
Restore all configuration to their default values
- save() bool¶
Save the configuration to a file
- Returns:
Whether the save succeeds
- Return type:
bool
- set(key: str, value: Any) str | None¶
Attempt to modify a config setting
If the config created by this modification fails to pass validation, no change is actually made, and an error message is produced.
- Parameters:
key (str) – A dot-separated path from the config’s root to the value desired (e.g., “api.api-key”) to set the value of api-key under the api section.
value (Any) – The new value of the given setting
- Raises:
KeyError – If the given key leads to no value
- Returns:
A (mostly) human-readable error message if the config created by this modification is invalid; otherwise, None
- Return type:
str, optional
- validate(config) ValidationError | None¶
Validate a configuration
- Parameters:
config (dict) – The configuration to validate
- Returns:
None if validation succeeds; otherwise, the ValidationError instance that was raised during validation
- Return type:
jsonschema.ValidationError, optional