SQLAlchemyConfig
This configuration loads the configuration data from a RDBMS table using SQLAlchemy.
Any SQLAlchemy engine is supported.
Library SQLAlchemy is required (pip install SQLAlchemy)
The examples below are based on PostgreSQL.
psql -c 'CREATE TABLE config (key character varying(255) PRIMARY KEY NOT NULL, value TEXT NOT NULL)'
psql -c 'INSERT INTO config VALUES ("database.host", "localhost")'
psql -c 'INSERT INTO config VALUES ("database.port", "1234")'
# main.py
from central.config.sqlalchemy import SQLAlchemyConfig
from sqlalchemy import create_engine
engine = create_engine('postgresql://scott:tiger@localhost/test')
config = SQLAlchemyConfig(engine, 'select key, value from config')
config.load()
print(config.get('database.host'))
print(config.get('database.port'))
python main.py
>> localhost
>> 1234
Choosing column names
SQLAlchemyConfig
by default uses'key'
as the key column name and 'value'
as the value column name.
You can also define the column names by yourself.
from central.config.sqlalchemy import SQLAlchemyConfig
from sqlalchemy import create_engine
engine = create_engine('postgresql://scott:tiger@localhost/test')
config = SQLAlchemyConfig(engine, 'select property_key, property_value from config',
key_column='property_key', value_column='property_value')
Reloading configuration
Either SQLAlchemy or RDBMS do not support update notification so if your application needs to be notified when a configuration has been changed it can be archived using polling.
from central.config.sqlalchemy import SQLAlchemyConfig
from sqlalchemy import create_engine
engine = create_engine('postgresql://scott:tiger@localhost/test')
config = SQLAlchemyConfig(engine, 'select key, value from config').reload_every(600) # 10 min in seconds
config.load()