Check if app factory takes script_info argument and call it with(out) script_info as an argument depending on that

This commit is contained in:
Hendrik Makait 2017-05-23 13:46:45 -07:00
parent 81c2440a05
commit ae41df9a77
3 changed files with 66 additions and 28 deletions

View file

@ -22,13 +22,14 @@ from . import __version__
from ._compat import iteritems, reraise
from .globals import current_app
from .helpers import get_debug_flag
from ._compat import getargspec
class NoAppException(click.UsageError):
"""Raised if an application cannot be found or loaded."""
def find_best_app(module):
def find_best_app(script_info, module):
"""Given a module instance this tries to find the best possible
application in the module or raises an exception.
"""
@ -60,8 +61,8 @@ def find_best_app(module):
if callable(app_factory):
try:
app = app_factory()
app = check_factory_for_script_info_and_call(app_factory,
script_info)
if isinstance(app, Flask):
return app
except TypeError:
@ -79,6 +80,21 @@ def find_best_app(module):
)
def check_factory_for_script_info_and_call(func, script_info):
"""Given a function this checks if the function has an argument named
script_info or just a single argument and calls the function with
script_info if so. Otherwise calls the function without any arguments and
returns the result."""
arguments = getargspec(func).args
if 'script_info' in arguments:
result = func(script_info=script_info)
elif len(arguments) == 1:
result = func(script_info)
else:
result = func()
return result
def prepare_exec_for_file(filename):
"""Given a filename this will try to calculate the python path, add it
to the search path and return the actual module name that is expected.
@ -108,7 +124,7 @@ def prepare_exec_for_file(filename):
return '.'.join(module[::-1])
def locate_app(app_id):
def locate_app(script_info, app_id):
"""Attempts to locate the application."""
__traceback_hide__ = True
if ':' in app_id:
@ -134,7 +150,7 @@ def locate_app(app_id):
mod = sys.modules[module]
if app_obj is None:
app = find_best_app(mod)
app = find_best_app(script_info, mod)
else:
app = getattr(mod, app_obj, None)
if app is None:
@ -259,7 +275,7 @@ class ScriptInfo(object):
if self._loaded_app is not None:
return self._loaded_app
if self.create_app is not None:
rv = self.create_app(self)
rv = check_factory_for_script_info_and_call(self.create_app, self)
else:
if not self.app_import_path:
raise NoAppException(
@ -267,7 +283,7 @@ class ScriptInfo(object):
'the FLASK_APP environment variable.\n\nFor more '
'information see '
'http://flask.pocoo.org/docs/latest/quickstart/')
rv = locate_app(self.app_import_path)
rv = locate_app(self, self.app_import_path)
debug = get_debug_flag()
if debug is not None:
rv.debug = debug