def init_app(module, BASE_DIR, **kwargs):
    """
    Initalize an app, call this method once from start_app
    """
    global app

    def init_config():
        """
        Load settings module and attach values to the application
        config dictionary
        """
        if 'FLASK_PHILO_SETTINGS_MODULE' not in os.environ:
            raise ConfigurationError('No settings has been defined')

        app.config['BASE_DIR'] = BASE_DIR

        # default settings
        for v in dir(default_settings):
            if not v.startswith('_'):
                app.config[v] = getattr(default_settings, v)

        app.debug = app.config['DEBUG']

        # app settings
        settings = importlib.import_module(
            os.environ['FLASK_PHILO_SETTINGS_MODULE'])
        for v in dir(settings):
            if not v.startswith('_'):
                app.config[v] = getattr(settings, v)

        def init_urls():
            # Reads urls definition from URLs file and bind routes and views
            urls_module = importlib.import_module(app.config['URLS'])
            for route in urls_module.URLS:
                app.add_url_rule(
                    route[0], view_func=route[1].as_view(route[2]))

        def init_logging():
            """
            initialize logger for the app
            """

            hndlr = logging.StreamHandler()
            formatter = logging.Formatter(
                '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            hndlr.setFormatter(formatter)
            app.logger.addHandler(hndlr)
            log_level = app.config['LOG_LEVEL']
            app.logger.setLevel(getattr(logging, log_level))

        def init_flask_oauthlib():
            """
            http://flask-oauthlib.readthedocs.io/en/latest/oauth2.html
            """
            oauth.init_app(app)

        def init_cors(app):
            """
            Initializes cors protection if config
            """

            if 'CORS' in app.config:
                CORS(
                    app,
                    resources=app.config['CORS'],
                    supports_credentials=app.config.get(
                        "CORS_SUPPORT_CREDENTIALS",
                        False
                    ),
                    allow_headers=app.config.get(
                        "CORS_ALLOW_HEADERS",
                        "Content-Type,Authorization,accept-language,accept"
                    )
                )

        init_db(g, app)
        init_logging()
        init_urls()
        init_flask_oauthlib()
        init_jinja2(g, app)
        init_cors(app)
    app = Flask(module)
    init_config()
    return app
def init_app ( module , BASE_DIR , ** kwargs ) : global app def init_config ( ) : if 'FLASK_PHILO_SETTINGS_MODULE' not in os . environ : raise ConfigurationError ( 'No settings has been defined' ) app . config [ 'BASE_DIR' ] = BASE_DIR for v in dir ( default_settings ) : if not v . startswith ( '_' ) : app . config [ v ] = getattr ( default_settings , v ) app . debug = app . config [ 'DEBUG' ] settings = importlib . import_module ( os . environ [ 'FLASK_PHILO_SETTINGS_MODULE' ] ) for v in dir ( settings ) : if not v . startswith ( '_' ) : app . config [ v ] = getattr ( settings , v ) def init_urls ( ) : urls_module = importlib . import_module ( app . config [ 'URLS' ] ) for route in urls_module . URLS : app . add_url_rule ( route [ 0 ] , view_func = route [ 1 ] . as_view ( route [ 2 ] ) ) def init_logging ( ) : hndlr = logging . StreamHandler ( ) formatter = logging . Formatter ( '%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) hndlr . setFormatter ( formatter ) app . logger . addHandler ( hndlr ) log_level = app . config [ 'LOG_LEVEL' ] app . logger . setLevel ( getattr ( logging , log_level ) ) def init_flask_oauthlib ( ) : oauth . init_app ( app ) def init_cors ( app ) : if 'CORS' in app . config : CORS ( app , resources = app . config [ 'CORS' ] , supports_credentials = app . config . get ( "CORS_SUPPORT_CREDENTIALS" , False ) , allow_headers = app . config . get ( "CORS_ALLOW_HEADERS" , "Content-Type,Authorization,accept-language,accept" ) ) init_db ( g , app ) init_logging ( ) init_urls ( ) init_flask_oauthlib ( ) init_jinja2 ( g , app ) init_cors ( app ) app = Flask ( module ) init_config ( ) return app
Initalize an app, call this method once from start_app