Browse Source

rename to FLASK_SKIP_DOTENV, add docs, test

pull/2724/head
David Lord 6 years ago
parent
commit
5965cb7e1c
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
  1. 24
      docs/cli.rst
  2. 10
      flask/cli.py
  3. 15
      flask/helpers.py
  4. 8
      tests/test_cli.py

24
docs/cli.rst

@ -201,6 +201,30 @@ These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to
control default command options. control default command options.
Disable dotenv
~~~~~~~~~~~~~~
The ``flask`` command will show a message if it detects dotenv files but
python-dotenv is not installed.
.. code-block:: none
flask run
* Tip: There are .env files present. Do "pip install python-dotenv" to use them.
You can tell Flask not to load dotenv files even when python-dotenv is
installed by setting the ``FLASK_SKIP_DOTENV`` environment variable.
This can be useful if you want to load them manually, or if you're using
a project runner that loads them already. Keep in mind that the
environment variables must be set before the app loads or it won't
configure as expected.
.. code-block:: none
export FLASK_SKIP_DOTENV=1
flask run
Environment Variables From virtualenv Environment Variables From virtualenv
------------------------------------- -------------------------------------

10
flask/cli.py

@ -28,7 +28,7 @@ from werkzeug.utils import import_string
from . import __version__ from . import __version__
from ._compat import getargspec, iteritems, reraise, text_type from ._compat import getargspec, iteritems, reraise, text_type
from .globals import current_app from .globals import current_app
from .helpers import get_debug_flag, get_env from .helpers import get_debug_flag, get_env, get_load_dotenv
try: try:
import dotenv import dotenv
@ -544,10 +544,7 @@ class FlaskGroup(AppGroup):
# script that is loaded here also attempts to start a server. # script that is loaded here also attempts to start a server.
os.environ['FLASK_RUN_FROM_CLI'] = 'true' os.environ['FLASK_RUN_FROM_CLI'] = 'true'
val = os.environ.get('FLASK_DONT_LOAD_ENV') if get_load_dotenv(self.load_dotenv):
load_dotenv = not val or val in ('0', 'false', 'no')
if self.load_dotenv and load_dotenv:
load_dotenv() load_dotenv()
obj = kwargs.get('obj') obj = kwargs.get('obj')
@ -586,12 +583,11 @@ def load_dotenv(path=None):
.. versionadded:: 1.0 .. versionadded:: 1.0
""" """
if dotenv is None: if dotenv is None:
if path or os.path.exists('.env') or os.path.exists('.flaskenv'): if path or os.path.exists('.env') or os.path.exists('.flaskenv'):
click.secho( click.secho(
' * Tip: There are .env files present.' ' * Tip: There are .env files present.'
' Do "pip install python-dotenv" to use them', ' Do "pip install python-dotenv" to use them.',
fg='yellow') fg='yellow')
return return

15
flask/helpers.py

@ -68,6 +68,21 @@ def get_debug_flag():
return val.lower() not in ('0', 'false', 'no') return val.lower() not in ('0', 'false', 'no')
def get_load_dotenv(default=True):
"""Get whether the user has disabled loading dotenv files by setting
:envvar:`FLASK_SKIP_DOTENV`. The default is ``True``, load the
files.
:param default: What to return if the env var isn't set.
"""
val = os.environ.get('FLASK_SKIP_DOTENV')
if not val:
return default
return val.lower() in ('0', 'false', 'no')
def _endpoint_from_view_func(view_func): def _endpoint_from_view_func(view_func):
"""Internal helper that returns the default endpoint for a given """Internal helper that returns the default endpoint for a given
function. This always is the function name. function. This always is the function name.

8
tests/test_cli.py

@ -474,6 +474,14 @@ def test_dotenv_optional(monkeypatch):
assert 'FOO' not in os.environ assert 'FOO' not in os.environ
@need_dotenv
def test_disable_dotenv_from_env(monkeypatch, runner):
monkeypatch.chdir(test_path)
monkeypatch.setitem(os.environ, 'FLASK_SKIP_DOTENV', '1')
runner.invoke(FlaskGroup())
assert 'FOO' not in os.environ
def test_run_cert_path(): def test_run_cert_path():
# no key # no key
with pytest.raises(click.BadParameter): with pytest.raises(click.BadParameter):

Loading…
Cancel
Save