General purpose config file parser
Project description
configuration_settings module
General purpose config file parser.
Installation
Install with pip:
pip install configuration-settings
Usage
Load a set of config files:
from configuration_settings import Config
config = Config.load(__file__)
You may optionally pass a path to a specific config file (generally passed as a command line argument), and a dict containing default values.
def load(cls, script_path: str, config_path: str = None, default: Mapping[str, Any] = None) -> Config:
Config files may be JSON or YAML, and can contain arbitrary structure. Dicts in config files are converted to Config objects. Names (keys) are case-insensitive.
Config files are loaded from the following locations:
-
If no config path is provided:
- All parent directories from the location the script is located
- The directory the script is located
- /etc/{script_name}
- The current directory
-
If a config path is provided
- All parent directories from the location the script is located
- The directory the script is located
- The provided config path
For each location, if the location is a file, load that file, then search the file's directory for directories named: 'conf.d', 'config.d', '{script_name}.d', '{file_name}.d', load all files in those directories in alphabetical order. If the location is a directory, search for files named: 'config', 'config.local', '{script_name}', '{script_name}.local', then search for subdirectories named: 'conf.d', 'config.d', '{script_name}.d', '{file_name}.d'.
Files loaded later override values found in earlier files. Dict values are merged so only provided keys are replaced.
Config files may have the following extensions: '.json', '.yml', '.yaml'.
Common usage would be to have a config file with default values installed in the same location as the main script, and then the user would override settings in /etc/{script_name}/config.yaml and /etc/{script_name}/config.d/*.yaml.
Config objects are dict-like and also allow accessing values as properties.
In addition, there are methods to get values as a specific type:
def get_int(self, name: str, default: int = None) -> (int | None):
"""
Get an item as an int.
Returns default if missing or not an int.
"""
def get_float(self, name: str, default: float = None) -> (float | None):
"""
Get an item as a float.
Returns default if missing or not a float.
"""
def get_bool(self, name: str, default: bool = None) -> (bool | None):
"""
Get an item as a bool.
Returns default if missing or not a bool.
"""
def get_path(self, name: str, default: str = None) -> (str | None):
"""
Get an item as an absolute path.
Relative paths are resolved to the config file the item was loaded from.
Returns default if missing.
"""
def get_duration(self, name: str, default: timedelta = None) -> (timedelta | None):
"""
Get an item as a timedelta.
Accepts int values (seconds),
or string values with s|m|h|d|w suffix for seconds, minutes, hours, days, or weeks.
"""
You can set a default value for any item via:
def set_default(self, name: str, value: Any) -> None:
You can retrieve the full set of patsh to config files via the config_file_paths
property,
or get the file a specific value was loaded from via:
def get_config_file_path(self, name: str) -> (str | None):
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for configuration-settings-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfb7956784c733023b399d6ec4b9fd005bb3bcf57d8cf4f64d37246d4cd2fd97 |
|
MD5 | 8382b10c69f4339ee1179f1776667e6a |
|
BLAKE2b-256 | 482c709ef66523d7a053fc94f0dcb8a0cc274fcd339a7f6e0a971d00e0fef8e4 |
Hashes for configuration_settings-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d259783fda11e55d9c4c8a3a062f2ef51661017ed472387e52da2c2560a0810 |
|
MD5 | 2f12605dac52c7782b65c1f549d3a5ee |
|
BLAKE2b-256 | b6b36b782e3a6107649081938b50a2666f6fda9595eebf55ff4763815492fac5 |