Skip to main content

ini config model for flask

Project description

When ini config file looks like below:

[default]
debug = False

[mysql]
connection = sqlite:///test.db
#connection = mysql+pymysql://root:password@localhost:13306/project?charset=utf8
track_modifications = True

[secure]
secure_key = YjFjMDVkYzAtZGI4YS0xMWU5LWIwMjctMTdhNzg4ZjJiMGUyCg==

[jwt]
secret = YTgzNmM0OWUtNjllMS00MjI4LWFlZTMtNjgxNjAwNDdiNTBlCg==
algorithm = HS256

[test]
int_value = 1231

You need to create class based on ConfigOpts

from flask_fastconfig import ConfigOpts, ConfigGroup
from flask_fastconfig.config_opt import BooleanOpt, StrOpt, IntOpt
import uuid


class CONF(ConfigOpts):
    """
    ini configs
    """

    class default(ConfigGroup):
        debug = BooleanOpt(default=True, app_config="DEBUG")

    class mysql(ConfigGroup):
        connection = StrOpt(default='sqlite:///gevoton.db', app_config="SQLALCHEMY_DATABASE_URI")
        track_modifications = BooleanOpt(default=False, app_config="SQLALCHEMY_TRACK_MODIFICATIONS")

    class secure(ConfigGroup):
        secure_key = StrOpt(default=uuid.uuid4().hex, app_config="SECRET_KEY")

    class jwt(ConfigGroup):
        secret = StrOpt(default=uuid.uuid4().hex)
        algorithm = StrOpt(default='HS256')

    class test(ConfigGroup):
        int_value = IntOpt(default=123)

Then you can init flask app like below

from flask import Flask
from test import CONF

app = Flask(__name__)
cfg = CONF(app, "../system/etc/gevoton.ini")
app.config.from_mapping(cfg.get_app_config())

Finally, you can use cfg in other places:

from app import cfg

print(cfg.mysql.connection)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for flask-fastconfig, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size flask_fastconfig-0.0.3-py3-none-any.whl (4.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size flask-fastconfig-0.0.3.tar.gz (3.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page