Browse Source

Add option to not overwrite debug flag in cli

This is mainly intended for custom CLIs that may load a config file
which already sets the debug flag and does not make use of the `FLASK_*`
env vars at all.
pull/2765/head
ThiefMaster 7 years ago
parent
commit
50227f0954
  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
(ISO-8859-1). This fixes compatibility with Gunicorn, which is
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
.. _#2765: https://github.com/pallets/flask/pull/2765
Version 1.0.2

23
flask/cli.py

@ -340,7 +340,8 @@ class ScriptInfo(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.
self.app_import_path = app_import_path or os.environ.get('FLASK_APP')
#: 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
#: this script info.
self.data = {}
self.set_debug_flag = set_debug_flag
self._loaded_app = None
def load_app(self):
@ -386,12 +388,10 @@ class ScriptInfo(object):
'"app.py" module was not found in the current directory.'
)
debug = get_debug_flag()
# Update the app's debug flag through the descriptor so that other
# values repopulate as well.
if debug is not None:
app.debug = debug
if self.set_debug_flag:
# Update the app's debug flag through the descriptor so that
# other values repopulate as well.
app.debug = get_debug_flag()
self._loaded_app = app
return app
@ -459,6 +459,8 @@ class FlaskGroup(AppGroup):
:param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv`
files to set environment variables. Will also change the working
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
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,
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 ())
if add_version_option:
@ -475,6 +478,7 @@ class FlaskGroup(AppGroup):
AppGroup.__init__(self, params=params, **extra)
self.create_app = create_app
self.load_dotenv = load_dotenv
self.set_debug_flag = set_debug_flag
if add_default_commands:
self.add_command(run_command)
@ -550,7 +554,8 @@ class FlaskGroup(AppGroup):
obj = kwargs.get('obj')
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.setdefault('auto_envvar_prefix', 'FLASK')

22
tests/test_cli.py

@ -356,6 +356,28 @@ def test_flaskgroup(runner):
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):
"""Print the stacktrace if the CLI."""

Loading…
Cancel
Save