Browse Source

Merge pull request #2765 from ThiefMaster/custom-cli-ignore-debug

Add option to not overwrite debug flag in cli
pull/2928/head
Adrian 7 years ago committed by GitHub
parent
commit
1a9caedb49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      CHANGES.rst
  2. 23
      flask/cli.py
  3. 22
      tests/test_cli.py

3
CHANGES.rst

@ -12,8 +12,11 @@ Unreleased
- :func:`send_file` encodes filenames as ASCII instead of Latin-1 - :func:`send_file` encodes filenames as ASCII instead of Latin-1
(ISO-8859-1). This fixes compatibility with Gunicorn, which is (ISO-8859-1). This fixes compatibility with Gunicorn, which is
stricter about header encodings than PEP 3333. (`#2766`_) stricter about header encodings than PEP 3333. (`#2766`_)
- Allow custom CLIs using ``FlaskGroup`` to set the debug flag without
it always being overwritten based on environment variables. (`#2765`_)
.. _#2766: https://github.com/pallets/flask/issues/2766 .. _#2766: https://github.com/pallets/flask/issues/2766
.. _#2765: https://github.com/pallets/flask/pull/2765
Version 1.0.2 Version 1.0.2

23
flask/cli.py

@ -340,7 +340,8 @@ class ScriptInfo(object):
onwards as click object. onwards as click object.
""" """
def __init__(self, app_import_path=None, create_app=None): def __init__(self, app_import_path=None, create_app=None,
set_debug_flag=True):
#: Optionally the import path for the Flask application. #: Optionally the import path for the Flask application.
self.app_import_path = app_import_path or os.environ.get('FLASK_APP') self.app_import_path = app_import_path or os.environ.get('FLASK_APP')
#: Optionally a function that is passed the script info to create #: Optionally a function that is passed the script info to create
@ -349,6 +350,7 @@ class ScriptInfo(object):
#: A dictionary with arbitrary data that can be associated with #: A dictionary with arbitrary data that can be associated with
#: this script info. #: this script info.
self.data = {} self.data = {}
self.set_debug_flag = set_debug_flag
self._loaded_app = None self._loaded_app = None
def load_app(self): def load_app(self):
@ -386,12 +388,10 @@ class ScriptInfo(object):
'"app.py" module was not found in the current directory.' '"app.py" module was not found in the current directory.'
) )
debug = get_debug_flag() if self.set_debug_flag:
# Update the app's debug flag through the descriptor so that
# Update the app's debug flag through the descriptor so that other # other values repopulate as well.
# values repopulate as well. app.debug = get_debug_flag()
if debug is not None:
app.debug = debug
self._loaded_app = app self._loaded_app = app
return app return app
@ -459,6 +459,8 @@ class FlaskGroup(AppGroup):
:param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv` :param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv`
files to set environment variables. Will also change the working files to set environment variables. Will also change the working
directory to the directory containing the first file found. directory to the directory containing the first file found.
:param set_debug_flag: Set the app's debug flag based on the active
environment
.. versionchanged:: 1.0 .. versionchanged:: 1.0
If installed, python-dotenv will be used to load environment variables If installed, python-dotenv will be used to load environment variables
@ -466,7 +468,8 @@ class FlaskGroup(AppGroup):
""" """
def __init__(self, add_default_commands=True, create_app=None, def __init__(self, add_default_commands=True, create_app=None,
add_version_option=True, load_dotenv=True, **extra): add_version_option=True, load_dotenv=True,
set_debug_flag=True, **extra):
params = list(extra.pop('params', None) or ()) params = list(extra.pop('params', None) or ())
if add_version_option: if add_version_option:
@ -475,6 +478,7 @@ class FlaskGroup(AppGroup):
AppGroup.__init__(self, params=params, **extra) AppGroup.__init__(self, params=params, **extra)
self.create_app = create_app self.create_app = create_app
self.load_dotenv = load_dotenv self.load_dotenv = load_dotenv
self.set_debug_flag = set_debug_flag
if add_default_commands: if add_default_commands:
self.add_command(run_command) self.add_command(run_command)
@ -550,7 +554,8 @@ class FlaskGroup(AppGroup):
obj = kwargs.get('obj') obj = kwargs.get('obj')
if obj is None: if obj is None:
obj = ScriptInfo(create_app=self.create_app) obj = ScriptInfo(create_app=self.create_app,
set_debug_flag=self.set_debug_flag)
kwargs['obj'] = obj kwargs['obj'] = obj
kwargs.setdefault('auto_envvar_prefix', 'FLASK') kwargs.setdefault('auto_envvar_prefix', 'FLASK')

22
tests/test_cli.py

@ -356,6 +356,28 @@ def test_flaskgroup(runner):
assert result.output == 'flaskgroup\n' assert result.output == 'flaskgroup\n'
@pytest.mark.parametrize('set_debug_flag', (True, False))
def test_flaskgroup_debug(runner, set_debug_flag):
"""Test FlaskGroup debug flag behavior."""
def create_app(info):
app = Flask("flaskgroup")
app.debug = True
return app
@click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)
def cli(**params):
pass
@cli.command()
def test():
click.echo(str(current_app.debug))
result = runner.invoke(cli, ['test'])
assert result.exit_code == 0
assert result.output == '%s\n' % str(not set_debug_flag)
def test_print_exceptions(runner): def test_print_exceptions(runner):
"""Print the stacktrace if the CLI.""" """Print the stacktrace if the CLI."""

Loading…
Cancel
Save