Blame view

app/__init__.py 1.25 KB
7b83b0b5   hitier   Fix #19 Add confi...
1
import os
a40ff516   hitier   Typo: update name...
2
import sys
7b83b0b5   hitier   Fix #19 Add confi...
3

3122f2f8   hitier   Running mode 1: f...
4
5
from flask import Flask

7b83b0b5   hitier   Fix #19 Add confi...
6
# Please, set a config file on top project dir
919d8186   hitier   Change config fil...
7
# find example in ressources/pdc_config.py
7b83b0b5   hitier   Fix #19 Add confi...
8
try:
919d8186   hitier   Change config fil...
9
    from pdc_config import Config, ProdConfig, DevConfig, TestConfig
7b83b0b5   hitier   Fix #19 Add confi...
10
except ImportError:
a40ff516   hitier   Typo: update name...
11
    # TODO: use logging system
919d8186   hitier   Change config fil...
12
13
    print("Please set an pdc_config.py file in you PYTHON_PATH")
    print("See INSTALL.md for more info")
a40ff516   hitier   Typo: update name...
14
    sys.exit(-1)
7b83b0b5   hitier   Fix #19 Add confi...
15

3122f2f8   hitier   Running mode 1: f...
16
17
18
19
20

def create_app(config_class=None):
    """ App Factory.
    can be called with configuration class as argument.
    """
7b83b0b5   hitier   Fix #19 Add confi...
21
22
23
24
25
26

    # Configuration Switcher
    # try to set configuration depending on environment
    # if no configuration class was given
    #
    if config_class is None:
a40ff516   hitier   Typo: update name...
27
28
29
        flask_env = os.environ.get('FLASK_ENV')
        if not flask_env:
            flask_env = 'testing'
7b83b0b5   hitier   Fix #19 Add confi...
30

a40ff516   hitier   Typo: update name...
31
        if flask_env == 'production':
7b83b0b5   hitier   Fix #19 Add confi...
32
            config_class = ProdConfig
a40ff516   hitier   Typo: update name...
33
        elif flask_env == 'development':
7b83b0b5   hitier   Fix #19 Add confi...
34
            config_class = DevConfig
a40ff516   hitier   Typo: update name...
35
        elif flask_env == 'testing':
7b83b0b5   hitier   Fix #19 Add confi...
36
37
38
39
40
41
            config_class = TestConfig
        else:
            config_class = Config

    # Main flask app
    #
3122f2f8   hitier   Running mode 1: f...
42
    app = Flask(__name__)
7b83b0b5   hitier   Fix #19 Add confi...
43
    app.config.from_object(config_class)
8bd0f3cb   hitier   Fix #25: Flask Bl...
44

7b83b0b5   hitier   Fix #19 Add confi...
45
46
    # Get and activate blueprints
    #
8bd0f3cb   hitier   Fix #25: Flask Bl...
47
48
49
    from .main import bp as main_bp
    app.register_blueprint(main_bp)

3122f2f8   hitier   Running mode 1: f...
50
    return app