Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyconf-ezload

pyconf-ezload is a simple Python library for loading configurations from different sources such as environment variables and configuration files (JSON/YAML). It supports flexible configurations with features like default values, required keys, and filtering based on key prefixes.

Installation

To install the package, you can use pip:

pip install pyconf-ezload

Alternatively, you can install it from the source using poetry:

poetry install

Usage

1. Loading Configuration from Environment Variables

You can load configuration directly from environment variables, optionally filtering by key prefix, providing default values, or marking certain variables as required.

from pyconfig_loader import ConfigLoader

# Example of loading configuration from environment variables
loader = ConfigLoader("env")

# Load with required keys and default values
config = loader.load_from_env(
    required_keys=["DB_USERNAME", "DB_PASSWORD"],
    default_values={"DB_PASSWORD": "default_pass"},
    key_prefix="DB_"
)

print(config)

This would output a dictionary like:

{
    'DB_USERNAME': 'admin',   # from env
    'DB_PASSWORD': 'default_pass'  # default value as it's missing in the environment
}

2. Loading Configuration from a File (JSON or YAML)

You can also load configuration from a file, supporting both JSON and YAML formats.

JSON Example:

{
    "DB_USERNAME": "admin",
    "DB_PASSWORD": "secret"
}

YAML Example:

DB_USERNAME: admin
DB_PASSWORD: secret
from pyconfig_loader import ConfigLoader

# Example of loading configuration from a JSON or YAML file
loader = ConfigLoader("path/to/config.json")  # Or "path/to/config.yaml"
config = loader.load()

print(config)

3. Error Handling

By default, if required environment variables are missing, a ValueError will be raised. You can modify this behavior with the raise_on_missing argument.

config = loader.load_from_env(
    required_keys=["DB_USERNAME", "DB_PASSWORD"],
    raise_on_missing=False  # Will not raise an error if keys are missing
)

4. Advanced Configuration Features

  • Prefix Filtering: You can filter environment variables based on a prefix (e.g., load only variables starting with DB_).
  • Default Values: Specify default values for missing environment variables.
  • Required Keys: Define which environment variables are required to exist in the configuration, and optionally set default values for them.
config = loader.load_from_env(
    required_keys=["API_KEY", "DB_PASSWORD"],
    default_values={"DB_PASSWORD": "default_pass"},
    key_prefix="APP_"
)

Development

To contribute to pyconf-ezload, you can clone the repository and use poetry to manage dependencies and run tests.

git clone https://github.com/yourusername/pyconf-ezload.git
cd pyconf-ezload
poetry install
poetry run pytest  # To run tests

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages