Browse Source

style cleanup

[ci skip]
pull/2378/head
David Lord 7 years ago
parent
commit
448368e226
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
  1. 8
      docs/cli.rst
  2. 30
      flask/cli.py
  3. 18
      tests/test_cli.py

8
docs/cli.rst

@ -25,12 +25,12 @@ your Flask application's :attr:`Flask.cli` instance as well as some
built-in commands that are always there. Flask extensions can also built-in commands that are always there. Flask extensions can also
register more commands there if they desire so. register more commands there if they desire so.
For the :command:`flask` script to work, an application needs to be For the :command:`flask` script to work, an application needs to be discovered.
discovered. Flask looks for a module named wsgi.py or app.py by default, and Flask looks for a module named :file:`wsgi.py` or :file:`app.py` by default,
if it finds one it assumes the application is defined in it. and if it finds one it assumes the application is defined in it.
You can instruct Flask to look for the application in a different module by You can instruct Flask to look for the application in a different module by
exporting the ``FLASK_APP`` environment variable. It can be either set to an exporting the ``FLASK_APP`` environment variable. It can be either set to an
import path or to a filename of a Python module that contains a Flask import path or to a filename of a Python module that contains a Flask
application. application.

30
flask/cli.py

@ -174,12 +174,14 @@ def prepare_exec_for_file(filename):
if sys.path[0] != dirpath: if sys.path[0] != dirpath:
sys.path.insert(0, dirpath) sys.path.insert(0, dirpath)
return '.'.join(module[::-1]) return '.'.join(module[::-1])
def locate_app(script_info, app_id, raise_if_not_found=True): def locate_app(script_info, app_id, raise_if_not_found=True):
"""Attempts to locate the application.""" """Attempts to locate the application."""
__traceback_hide__ = True __traceback_hide__ = True
if ':' in app_id: if ':' in app_id:
module, app_obj = app_id.split(':', 1) module, app_obj = app_id.split(':', 1)
else: else:
@ -193,17 +195,23 @@ def locate_app(script_info, app_id, raise_if_not_found=True):
# Determine this by checking whether the trace has a depth > 1. # Determine this by checking whether the trace has a depth > 1.
if sys.exc_info()[-1].tb_next: if sys.exc_info()[-1].tb_next:
stack_trace = traceback.format_exc() stack_trace = traceback.format_exc()
raise NoAppException('There was an error trying to import' raise NoAppException(
' the app (%s):\n%s' % (module, stack_trace)) 'There was an error trying to import the app ({module}):\n'
'{stack_trace}'.format(
module=module, stack_trace=stack_trace
)
)
elif raise_if_not_found: elif raise_if_not_found:
raise NoAppException('The file/path provided (%s) does not appear' raise NoAppException(
' to exist. Please verify the path is ' 'The file/path provided (%s) does not appear to exist. Please'
'correct. If app is not on PYTHONPATH, ' ' verify the path is correct. If app is not on PYTHONPATH,'
'ensure the extension is .py' % module) ' ensure the extension is .py.'.format(module=module)
)
else: else:
return return
mod = sys.modules[module] mod = sys.modules[module]
if app_obj is None: if app_obj is None:
return find_best_app(script_info, mod) return find_best_app(script_info, mod)
else: else:
@ -334,17 +342,21 @@ class ScriptInfo(object):
else: else:
for module in ['wsgi.py', 'app.py']: for module in ['wsgi.py', 'app.py']:
import_path = prepare_exec_for_file(module) import_path = prepare_exec_for_file(module)
rv = locate_app(self, import_path, rv = locate_app(
raise_if_not_found=False) self, import_path, raise_if_not_found=False
)
if rv: if rv:
break break
if not rv: if not rv:
raise NoAppException( raise NoAppException(
'Could not locate Flask application. You did not provide ' 'Could not locate Flask application. You did not provide '
'the FLASK_APP environment variable, and a wsgi.py or ' 'the FLASK_APP environment variable, and a wsgi.py or '
'app.py module was not found in the current directory.\n\n' 'app.py module was not found in the current directory.\n\n'
'For more information see ' 'For more information see '
'http://flask.pocoo.org/docs/latest/quickstart/') 'http://flask.pocoo.org/docs/latest/quickstart/'
)
debug = get_debug_flag() debug = get_debug_flag()

18
tests/test_cli.py

@ -181,8 +181,9 @@ def test_locate_app(test_apps):
script_info, "cliapp.factory:create_app ()") script_info, "cliapp.factory:create_app ()")
pytest.raises( pytest.raises(
NoAppException, locate_app, script_info, "cliapp.importerrorapp") NoAppException, locate_app, script_info, "cliapp.importerrorapp")
assert locate_app(script_info, "notanpp.py", assert locate_app(
raise_if_not_found=False) is None script_info, "notanpp.py", raise_if_not_found=False
) is None
def test_find_default_import_path(test_apps, monkeypatch, tmpdir): def test_find_default_import_path(test_apps, monkeypatch, tmpdir):
@ -231,19 +232,20 @@ def test_scriptinfo(test_apps, monkeypatch):
assert obj.load_app() == app assert obj.load_app() == app
obj = ScriptInfo() obj = ScriptInfo()
pytest.raises( pytest.raises(NoAppException, obj.load_app)
NoAppException, obj.load_app)
# import app from wsgi.py in current directory # import app from wsgi.py in current directory
monkeypatch.chdir(os.path.abspath( monkeypatch.chdir(os.path.abspath(os.path.join(
os.path.join(os.path.dirname(__file__), 'test_apps', 'helloworld'))) os.path.dirname(__file__), 'test_apps', 'helloworld'
)))
obj = ScriptInfo() obj = ScriptInfo()
app = obj.load_app() app = obj.load_app()
assert app.name == 'hello' assert app.name == 'hello'
# import app from app.py in current directory # import app from app.py in current directory
monkeypatch.chdir(os.path.abspath( monkeypatch.chdir(os.path.abspath(os.path.join(
os.path.join(os.path.dirname(__file__), 'test_apps', 'cliapp'))) os.path.dirname(__file__), 'test_apps', 'cliapp'
)))
obj = ScriptInfo() obj = ScriptInfo()
app = obj.load_app() app = obj.load_app()
assert app.name == 'testapp' assert app.name == 'testapp'

Loading…
Cancel
Save