2016-08-13 49 views
1

我使用自定義的manage.py腳本來創建具有工廠模式的flask應用程序。避免使用flask-cli創建應用程序

該腳本需要能夠運行服務器並運行我的測試。我使用外部工具來運行我的測試,所以我不需要創建一個應用程序來運行測試。我如何才能在運行某些命令時創建應用程序?

我的劇本目前是這樣的:

import subprocess 
import sys 

from flask import current_app 
from flask.cli import FlaskGroup 
import click 

from quizApp import create_app 


@click.pass_context 
def get_app(ctx, _): 
    """Create an app with the correct config. 
    """ 
    return create_app(ctx.find_root().params["config"]) 


@click.option("-c", "--config", default="development", 
       help="Name of config to use for the app") 
@click.group(cls=FlaskGroup, create_app=get_app) 
def cli(**_): 
    """Define the top level group. 
    """ 
    pass 


@cli.command("test") 
def test(): 
    """Set the app config to testing and run pytest, passing along command 
    line args. 
    """ 
    # This looks stupid, but see 
    # https://github.com/pytest-dev/pytest/issues/1357 
    sys.exit(subprocess.call(['py.test', 
           '--cov=quizApp', 
           '--flake8', 
           '--pylint', 
           './'])) 


if __name__ == '__main__': 
    cli() 

我試圖創建一個小組第二,但它似乎是隻有一組在同一時間「跑」,所以我不完全知道如何解決這個問題。

回答

0

您需要添加:

cli.add_command(test) 

前:

if __name__ == '__main__': 
    cli() 

而且我不知道這是否是你需要做的唯一剩下的東西。我也在研究這個,並感到困惑。但是如果你想在FlaskGroup中添加一個命令,你必須這樣做。當我想在燒瓶項目中使用Factory和CLI時,我感到困惑。也許最後你需要嘗試Flask-Script擴展,至少它可以工作。

2

使用with_appcontext參數到你的附加命令:

@click.group(cls=FlaskGroup, create_app=get_app) 
def cli(**_): 
    pass 

@cli.command(with_appcontext=false) 
def extra(): 
    pass