From 666ef171d8f06a36d722d2e888d70b3dd02b93b2 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sat, 13 May 2017 10:31:17 +0200 Subject: [PATCH] Use factory strategy. --- accountant/__init__.py | 42 ++++++++++++++++++++++++------------------ accountant/run.py | 9 +++++++++ 2 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 accountant/run.py diff --git a/accountant/__init__.py b/accountant/__init__.py index 48fbeaf..128a225 100644 --- a/accountant/__init__.py +++ b/accountant/__init__.py @@ -24,25 +24,35 @@ from flask_alembic import alembic_click from .models import db from .views import api, cors -# The app -app = Flask(__name__, static_folder=None, template_folder=None) - -# Configure it from config.cfg. -app.config.from_pyfile('config.cfg') - -app.config['SQLALCHEMY_ECHO'] = app.debug -app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True -app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True - -db.init_app(app) - -# Database migrations. -alembic = Alembic(app) +# pylint: disable=invalid-name +alembic = Alembic() +def create_app(config_path): + """Create the app using specific config file.""" + # The app + app = Flask(__name__, static_folder=None, template_folder=None) + # Configure it from configuration file. + app.config.from_pyfile(config_path) + # Database related stuff. + app.config['SQLALCHEMY_ECHO'] = app.debug + app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True + db.init_app(app) + + app.config['ALEMBIC'] = { + 'script_location': 'migrations' + } + alembic.init_app(app) + + # API views related stuff. + cors.init_app(app) + api.init_app(app) + + return app # Database initialization. @@ -62,7 +72,3 @@ def dropdb(): click.echo("Clearing database.") db.metadata.drop_all(bind=db.engine) click.echo("Database cleared.") - - -api.init_app(app) -cors.init_app(app) diff --git a/accountant/run.py b/accountant/run.py new file mode 100644 index 0000000..54f32de --- /dev/null +++ b/accountant/run.py @@ -0,0 +1,9 @@ +"""Accountant runner.""" + +from os import path, getcwd + +from . import create_app + + +config_path = path.join(getcwd(), 'config.cfg') # pylint: disable=invalid-name +app = create_app(config_path) # pylint: disable=invalid-name