rename to FLASK_SKIP_DOTENV, add docs, test
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent bac5d6b · Browse files · 4 files·+50 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+50 added−7 removed
- tests:+8 added−0 removed
- other:+42 added−7 removed
Provenance
- Commit
5965cb7- Tree
07545a6- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
bac5d6b
4 files changed+50 added−7 removed
modified:docs/cli.rst+24 added
199 unmodified lines200 These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to201 control default command options.202 203 204+Disable dotenv205+~~~~~~~~~~~~~~206+207+The ``flask`` command will show a message if it detects dotenv files but208+python-dotenv is not installed.209+210+.. code-block:: none211+212+ flask run213+ * Tip: There are .env files present. Do "pip install python-dotenv" to use them.214+215+You can tell Flask not to load dotenv files even when python-dotenv is216+installed by setting the ``FLASK_SKIP_DOTENV`` environment variable.217+This can be useful if you want to load them manually, or if you're using218+a project runner that loads them already. Keep in mind that the219+environment variables must be set before the app loads or it won't220+configure as expected.221+222+.. code-block:: none223+224+ export FLASK_SKIP_DOTENV=1225+ flask run226+227+228 Environment Variables From virtualenv229 -------------------------------------230 231 If you do not want to install dotenv support, you can still set environment235 unmodified linesmodified:flask/cli.py+3 added-7 removed
26 unmodified lines27 28 from . import __version__29 from ._compat import getargspec, iteritems, reraise, text_type30 from .globals import current_app31-from .helpers import get_debug_flag, get_env31+from .helpers import get_debug_flag, get_env, get_load_dotenv32 33 try:34 import dotenv35 except ImportError:507 unmodified lines543 # call into a no-op. This is necessary to avoid ugly errors when the544 # script that is loaded here also attempts to start a server.545 os.environ['FLASK_RUN_FROM_CLI'] = 'true'546 547- val = os.environ.get('FLASK_DONT_LOAD_ENV')548- load_dotenv = not val or val in ('0', 'false', 'no')549-550- if self.load_dotenv and load_dotenv:547+ if get_load_dotenv(self.load_dotenv):548 load_dotenv()549 550 obj = kwargs.get('obj')551 30 unmodified lines582 :return: ``True`` if a file was loaded.583 584 .. versionadded:: 1.0585 """589-586 if dotenv is None:587 if path or os.path.exists('.env') or os.path.exists('.flaskenv'):588 click.secho(589 ' * Tip: There are .env files present.'594- ' Do "pip install python-dotenv" to use them',590+ ' Do "pip install python-dotenv" to use them.',591 fg='yellow')592 return593 594 if path is not None:300 unmodified linesmodified:flask/helpers.py+15 added
66 unmodified lines67 68 return val.lower() not in ('0', 'false', 'no')69 70 71+def get_load_dotenv(default=True):72+ """Get whether the user has disabled loading dotenv files by setting73+ :envvar:`FLASK_SKIP_DOTENV`. The default is ``True``, load the74+ files.75+76+ :param default: What to return if the env var isn't set.77+ """78+ val = os.environ.get('FLASK_SKIP_DOTENV')79+80+ if not val:81+ return default82+83+ return val.lower() in ('0', 'false', 'no')84+85+86 def _endpoint_from_view_func(view_func):87 """Internal helper that returns the default endpoint for a given88 function. This always is the function name.89 """955 unmodified linesmodified:tests/test_cli.py+8 added
472 unmodified lines473 load_dotenv()474 assert 'FOO' not in os.environ475 476 477+@need_dotenv478+def test_disable_dotenv_from_env(monkeypatch, runner):479+ monkeypatch.chdir(test_path)480+ monkeypatch.setitem(os.environ, 'FLASK_SKIP_DOTENV', '1')481+ runner.invoke(FlaskGroup())482+ assert 'FOO' not in os.environ483+484+485 def test_run_cert_path():486 # no key487 with pytest.raises(click.BadParameter):488 run_command.make_context('run', ['--cert', __file__])53 unmodified lines