Metaclass for handling configuration class objects using environment variables
Project description
Metaclass for handling configuration class objects using environment variables.
If an environment variable is specified, the metaclass will pull the variable from the environment, the variable defined on the class will be used.
This was built to be a dropin replacement for flask-env but supporting change environment variables after the meta class is loaded.
Config
There are 2 configuration options, that are set on the base class object.
ENV_LOAD_ALL = <True/False>
Setting this on the class will allow loading any environment variable even if it is not set on the base class.
ENV_PREFIX = <string>
Setting this will will be a prefix for variables in the environment.
Install
This should just be pip installed
python3 -m pip install figenv
Usage
The basic usecase is below.
import os
import figenv
class Config(metaclass=figenv.MetaConfig):
ENV_LOAD_ALL = True
ENV_PREFIX = 'ROCKSTEADY_'
BLAH = True
TIMEOUT = 5
POSTGRES_HOST = 'localhost'
POSTGRES_PORT = 5432
POSTGRES_USER = 'bebop'
POSTGRES_PASS = 'secret'
POSTGRES_DB = 'main'
def SQLALCHEMY_DATABASE_URI(cls):
return 'postgresql://{user}:{secret}@{host}:{port}/{database}?sslmode=require'.format(
user=cls.POSTGRES_USER,
secret=cls.POSTGRES_PASS,
host=cls.POSTGRES_HOST,
port=cls.POSTGRES_PORT,
database=cls.POSTGRES_DB,
)
assert Config.TIMEOUT == 5
assert Config.BLAH is True
assert Config.SQLALCHEMY_DATABASE_URI == 'postgresql://bebop:secret@localhost:5432/main?sslmode=require'
try:
Config.WHAT
except AttributeError:
pass
os.environ.update({
'ROCKSTEADY_BLAH': 'false',
'ROCKSTEADY_TIMEOUT': '15',
'ROCKSTEADY_WHAT': '2.9',
'ROCKSTEADY_SQLALCHEMY_DATABASE_URI': 'postgres://localhost:5432/db',
})
assert Config.TIMEOUT == 15
assert Config.BLAH is False
assert Config.WHAT == 2.9
assert Config.SQLALCHEMY_DATABASE_URI == 'postgres://localhost:5432/db'
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
File details
Details for the file figenv-1.4.1.tar.gz
.
File metadata
- Download URL: figenv-1.4.1.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ddfd35672623779aeb2ef53145f034601ffd2e8f3958ea7be5eecd14aa684da |
|
MD5 | d1ee00e74147c856b192f7f9a89198cf |
|
BLAKE2b-256 | d0fd8412c6c07aaa408fde2dceffc4e4ef001b1ee4326bd7dba3a8ca55f945c9 |
File details
Details for the file figenv-1.4.1-py3-none-any.whl
.
File metadata
- Download URL: figenv-1.4.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62ecb1c41d64173bbf769a59df6ceada32407322b22da32df1d4ddfd9bc27779 |
|
MD5 | d390f7b9082b4628fc5826eb316629db |
|
BLAKE2b-256 | 4763b5b91210f1b49b7d8542f807ef1dcdad40b4103b865f718914258c6281fd |