fantasy/__init__.py
def create_app(app_name, config={}, db=None, celery=None):
    """
    App Factory 工具

    策略是：
        - 初始化app
        - 根据app_name,装载指定的模块
        - 尝试装载app.run_app
        - 如果指定了`FANTASY_PRIMARY_NODE`，则尝试进行migrate操作
        - 装载error handler

    :return:
    """

    track_mode = os.environ['FANTASY_TRACK_MODE'] == 'yes'
    if track_mode:
        print('(00/14)fantasy track mode active...')

    active_db = os.environ['FANTASY_ACTIVE_DB'] == 'yes'

    if track_mode:
        print('(01/14)hacking webargs...')

    from webargs.flaskparser import parser
    from . import error_handler, hacker, cli

    hacker.hack_webargs()

    migrations_root = os.path.join(
        os.environ.get('FANTASY_MIGRATION_PATH',
                       os.getcwd()),
        'migrations')

    if track_mode:
        print('(02/14)initial app...')

    mod = importlib.import_module(app_name)
    app = FantasyFlask(__name__, root_path=os.path.dirname(mod.__file__))

    if track_mode:
        print('(03/14)update app.config...')

    if config:
        app.config.update(config)

    # 由外部做显式声明，否则不做调用
    config_module = os.environ.get('FANTASY_SETTINGS_MODULE', None)
    if track_mode:
        print("       found config module %s,try load it..." % config_module)

    if config_module:
        app.config.from_object(config_module)

    if track_mode:
        print('(04/14)confirm celery...')

    if celery:
        app.celery = celery
        pass

    if track_mode:
        print('(05/14)bind app context...')

    with app.app_context():

        if track_mode:
            print('(06/14)confirm db handle...')

        if db is None:
            global _db
            app.db = _db
        else:
            app.db = db

        if track_mode:
            print('(07/14)confirm cache...')

        if os.environ['FANTASY_ACTIVE_CACHE'] != 'no':
            from flask_caching import Cache
            app.cache = Cache(app, config=app.config)
            pass

        if track_mode:
            print('(08/14)confirm sentry...')

        if os.environ.get('FANTASY_ACTIVE_SENTRY') != 'no':
            from raven.contrib.flask import Sentry
            Sentry(app)
            pass

        if track_mode:
            print('(09/14)active app...')

        if hasattr(mod, 'run_app'):
            run_app = getattr(mod, 'run_app')

            try:
                run_app(app)
            except Exception as e:
                if hasattr(app, 'sentry'):
                    app.sentry.handle_exception(e)
                    pass

                import sys
                import traceback
                traceback.print_exc()
                sys.exit(-1)

            pass

        if active_db and app.db:
            if track_mode:
                print('(10/14)trigger auto migrate...')

            smart_database(app)
            smart_migrate(app, migrations_root)
            smart_account(app)
            app.db.init_app(app)

            @app.teardown_request
            def session_clear(exception=None):
                if exception and app.db.session.is_active:
                    app.db.session.rollback()
                    app.db.session.remove()

            pass

        if track_mode:
            print('(11/14)bind error handle...')

        # 添加错误控制
        @parser.error_handler
        def h_webargs(error):
            return error_handler.webargs_error(error)

        @app.errorhandler(422)
        def h_422(error):
            return error_handler.http422(error)

        @app.errorhandler(500)
        def h_500(error):
            return error_handler.http500(error)

        if hasattr(mod, 'error_handler'):
            error_handle = getattr(mod, 'error_handle')
            error_handle(app)
            pass

        if track_mode:
            print('(12/14)bind admin handle...')

        if hasattr(mod, 'run_admin'):
            import flask_admin
            admin = flask_admin.Admin(name=os.environ.get('FANTASY_ADMIN_NAME',
                                                          'Admin'),
                                      template_mode=os.environ.get(
                                          'FANTASY_ADMIN_TEMPLATE_MODE',
                                          'bootstrap3'))

            run_admin = getattr(mod, 'run_admin')
            run_admin(admin)
            admin.init_app(app)
            pass

        pass

    if track_mode:
        print('(13/14)bind ff command...')

    app.cli.add_command(cli.ff)

    if track_mode:
        print('(14/14)bind cli command...')

    if hasattr(mod, 'run_cli'):
        run_cli = getattr(mod, 'run_cli')
        run_cli(app)
        pass

    return app
def create_app(app_name, config={}, db=None, celery=None):
    """
    App Factory 工具

    策略是：
        - 初始化app
        - 根据app_name,装载指定的模块
        - 尝试装载app.run_app
        - 如果指定了`FANTASY_PRIMARY_NODE`，则尝试进行migrate操作
        - 装载error handler

    :return:
    """

    track_mode = os.environ['FANTASY_TRACK_MODE'] == 'yes'
    if track_mode:
        print('(00/14)fantasy track mode active...')

    active_db = os.environ['FANTASY_ACTIVE_DB'] == 'yes'

    if track_mode:
        print('(01/14)hacking webargs...')

    from webargs.flaskparser import parser
    from . import error_handler, hacker, cli

    hacker.hack_webargs()

    migrations_root = os.path.join(
        os.environ.get('FANTASY_MIGRATION_PATH',
                       os.getcwd()),
        'migrations')

    if track_mode:
        print('(02/14)initial app...')

    mod = importlib.import_module(app_name)
    app = FantasyFlask(__name__, root_path=os.path.dirname(mod.__file__))

    if track_mode:
        print('(03/14)update app.config...')

    if config:
        app.config.update(config)

    # 由外部做显式声明，否则不做调用
    config_module = os.environ.get('FANTASY_SETTINGS_MODULE', None)
    if track_mode:
        print("       found config module %s,try load it..." % config_module)

    if config_module:
        app.config.from_object(config_module)

    if track_mode:
        print('(04/14)confirm celery...')

    if celery:
        app.celery = celery
        pass

    if track_mode:
        print('(05/14)bind app context...')

    with app.app_context():

        if track_mode:
            print('(06/14)confirm db handle...')

        if db is None:
            global _db
            app.db = _db
        else:
            app.db = db

        if track_mode:
            print('(07/14)confirm cache...')

        if os.environ['FANTASY_ACTIVE_CACHE'] != 'no':
            from flask_caching import Cache
            app.cache = Cache(app, config=app.config)
            pass

        if track_mode:
            print('(08/14)confirm sentry...')

        if os.environ.get('FANTASY_ACTIVE_SENTRY') != 'no':
            from raven.contrib.flask import Sentry
            Sentry(app)
            pass

        if track_mode:
            print('(09/14)active app...')

        if hasattr(mod, 'run_app'):
            run_app = getattr(mod, 'run_app')

            try:
                run_app(app)
            except Exception as e:
                if hasattr(app, 'sentry'):
                    app.sentry.handle_exception(e)
                    pass

                import sys
                import traceback
                traceback.print_exc()
                sys.exit(-1)

            pass

        if active_db and app.db:
            if track_mode:
                print('(10/14)trigger auto migrate...')

            smart_database(app)
            smart_migrate(app, migrations_root)
            smart_account(app)
            app.db.init_app(app)

            @app.teardown_request
            def session_clear(exception=None):
                if exception and app.db.session.is_active:
                    app.db.session.rollback()
                    app.db.session.remove()

            pass

        if track_mode:
            print('(11/14)bind error handle...')

        # 添加错误控制
        @parser.error_handler
        def h_webargs(error):
            return error_handler.webargs_error(error)

        @app.errorhandler(422)
        def h_422(error):
            return error_handler.http422(error)

        @app.errorhandler(500)
        def h_500(error):
            return error_handler.http500(error)

        if hasattr(mod, 'error_handler'):
            error_handle = getattr(mod, 'error_handle')
            error_handle(app)
            pass

        if track_mode:
            print('(12/14)bind admin handle...')

        if hasattr(mod, 'run_admin'):
            import flask_admin
            admin = flask_admin.Admin(name=os.environ.get('FANTASY_ADMIN_NAME',
                                                          'Admin'),
                                      template_mode=os.environ.get(
                                          'FANTASY_ADMIN_TEMPLATE_MODE',
                                          'bootstrap3'))

            run_admin = getattr(mod, 'run_admin')
            run_admin(admin)
            admin.init_app(app)
            pass

        pass

    if track_mode:
        print('(13/14)bind ff command...')

    app.cli.add_command(cli.ff)

    if track_mode:
        print('(14/14)bind cli command...')

    if hasattr(mod, 'run_cli'):
        run_cli = getattr(mod, 'run_cli')
        run_cli(app)
        pass

    return app
App Factory 工具

    策略是：
        - 初始化app
        - 根据app_name,装载指定的模块
        - 尝试装载app.run_app
        - 如果指定了`FANTASY_PRIMARY_NODE`，则尝试进行migrate操作
        - 装载error handler

    :return: