Skip to main content

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):

Metadata

Release files for configuration-settings 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for configuration-settings 1.0.0
File Size Uploaded
configuration-settings-1.0.0.tar.gz 19.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for configuration-settings 1.0.0
File Interpreter ABI Platform
configuration_settings-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 39.2 kB

Release files / configuration-settings-1.0.0.tar.gz

Download URL configuration-settings-1.0.0.tar.gz
Size 19.9 kB
Tags Source
SHA-256 checksum
How to use checksums
cfb7956784c733023b399d6ec4b9fd005bb3bcf57d8cf4f64d37246d4cd2fd97
BLAKE2b-256 checksum
How to use checksums
482c709ef66523d7a053fc94f0dcb8a0cc274fcd339a7f6e0a971d00e0fef8e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / configuration_settings-1.0.0-py3-none-any.whl

Download URL configuration_settings-1.0.0-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d259783fda11e55d9c4c8a3a062f2ef51661017ed472387e52da2c2560a0810
BLAKE2b-256 checksum
How to use checksums
b6b36b782e3a6107649081938b50a2666f6fda9595eebf55ff4763815492fac5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page