Like dataclasses but for config.
Project description
configclasses
Like dataclases but for config.
Specify your config with a class and load it with your env vars or env files.
>>> import httpx
... from configclasses import configclass
>>> class UserAPIClient(httpx.AsyncClient):
... def __init__(self, config: ClientConfig, *args, **kwargs):
... self.config = config
... super().__init__(*args, **kwargs)
...
... async def get_users(self, headers: Optional[Headers] = None) -> Dict[str, Any]:
... response = await self.get(f"{self.path}/users", auth=headers)
... response.raise_for_status()
... return response.json()
...
>>> @configclass
... class ClientConfig:
... host: str
... port: int
...
>>> config = ClientConfig.from_path(".env")
... async with UserAPIClient(config) as client:
... users = await client.get_users(auth_headers)
...
Features
- Fill your configclasses with existent env vars.
- Define default values in case these variables have no value at all.
- Load your config files in env vars following 12factor apps recommendations.
- Support for .env, yaml, toml, ini and json.
- Convert your env vars with specified type in configclass:
int
,float
,str
orbool
. - Use nested configclasses to more complex configurations.
- Specify a prefix with
@configclass(prefix="<PREFIX>")
to append this prefix to your configclass' attribute names. - Config groups (TODO): https://cli.dev/docs/tutorial/config_groups/
Requirements
Python 3.8+
Installation
pip install 12factor-configclasses
Supported formats
- .env ->
pip install 12factor-configclasses[dotenv]
- .yaml ->
pip install 12factor-configclasses[yaml]
- .toml ->
pip install 12factor-configclasses[toml]
- .ini
- .json
Install all dependencies with:
pip install 12factor-configclasses[full]
Example
# .env
HOST=0.0.0.0
PORT=8000
DB_URL=sqlite://:memory:
GENERATE_SCHEMAS=True
DEBUG=True
HTTPS_ONLY=False
GZIP=True
SENTRY=False
#config.py
from configclasses import configclass
@configclass
class DB:
user: str
password: str
url: str
@configclass
class AppConfig:
host: str
port: int
db: DB
generate_schemas: bool
debug: bool
https_only: bool
gzip: bool
sentry: bool
...
from api.config import AppConfig
app_config = AppConfig.from_path(".env")
app = Starlette(debug=app_config.debug)
if app_config.https_only:
app.add_middleware(
HTTPSRedirectMiddleware)
if app_config.gzip:
app.add_middleware(GZipMiddleware)
if app_config.sentry:
app.add_middleware(SentryAsgiMiddleware)
...
register_tortoise(
app,
db_url=app_config.db.url,
modules={"models": ["api.models"]},
generate_schemas=app_config.generate_schemas,
)
if __name__ == "__main__":
uvicorn.run(app, host=app_config.host, port=app_config.port)
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
Close
Hashes for 12factor-configclasses-0.2.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ef316ca3c42da92139426ab8e8b23d9e5a73feaed9a11242535b53683534e63 |
|
MD5 | 7dde780e401bd2bd78d14d2306479ace |
|
BLAKE2b-256 | c6b0d3d413a839fd146df95d8a5639037627ce078ce97791752a2bb601a8d5dc |
Close
Hashes for 12factor_configclasses-0.2.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0e729b9d261824c759e9a49952c951d7e1ac08f6186102f6c691f3a3fe34cb0 |
|
MD5 | 54baad1a19473af34866abb544410634 |
|
BLAKE2b-256 | 8fdc2464026ac14d7f9d780d5dba967356da8c605b2a1e65db167d26675f3dde |