codesmidgen/codesmidgen/__init__.py

50 lines
1.2 KiB
Python

# -*- coding: utf-8 -*-
"""
CodeSmidgen, yet another paste bin
"""
from configparser import ConfigParser
from pathlib import Path
import quart_flask_patch # noqa: F401
from quart import Quart
from codesmidgen.db import db
from codesmidgen.views import views
def read_config(config_path=None):
"""
Read the configuration file and return the values in a dictionary
"""
if config_path:
config_file = config_path / 'codesmidgen.cfg'
else:
config_file = Path(__file__).parent / '..' / 'codesmidgen.cfg'
if not config_file.exists():
return {}
config_parser = ConfigParser()
config_parser.read(config_file)
config = {}
for option in config_parser.options('codesmidgen'):
config[option.upper()] = config_parser.get('codesmidgen', option)
return config
def make_app(config_path=None):
"""
Create the application object
"""
app = Quart(__name__)
# Load the config file
config = read_config(config_path)
app.config.update(config)
app.config.update({'SQLALCHEMY_TRACK_MODIFICATIONS': False})
db.init_app(app)
app.register_blueprint(views)
@app.before_first_request
async def setup_db():
db.create_all()
return app