From ced719ea18a56f6c4075e08dbe05f0e77eac1866 Mon Sep 17 00:00:00 2001 From: Hendrik Makait Date: Mon, 22 May 2017 12:30:18 -0700 Subject: [PATCH 1/4] Auto-detect create_app and make_app factory functions --- flask/cli.py | 15 +++++++++++++++ tests/test_cli.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/flask/cli.py b/flask/cli.py index 3d361be8..cdb7f094 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -46,6 +46,21 @@ def find_best_app(module): if len(matches) == 1: return matches[0] + + # Search for app factory callables. + for attr_name in 'create_app', 'make_app': + app_factory = getattr(module, attr_name, None) + if app_factory is not None and callable(app_factory): + try: + app = app_factory() + if app is not None and isinstance(app, Flask): + return app + except TypeError: + raise NoAppException('Auto-detected "%s()" in module "%s", ' + 'but could not call it without ' + 'specifying arguments.' + % (attr_name, module.__name__)) + raise NoAppException('Failed to find application in module "%s". Are ' 'you sure it contains a Flask application? Maybe ' 'you wrapped it in a WSGI middleware or you are ' diff --git a/tests/test_cli.py b/tests/test_cli.py index ab875cef..bbb6fe58 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -51,6 +51,34 @@ def test_find_best_app(test_apps): myapp = Flask('appname') assert find_best_app(Module) == Module.myapp + class Module: + @staticmethod + def create_app(): + return Flask('appname') + assert isinstance(find_best_app(Module), Flask) + assert find_best_app(Module).name == 'appname' + + class Module: + @staticmethod + def make_app(): + return Flask('appname') + assert isinstance(find_best_app(Module), Flask) + assert find_best_app(Module).name == 'appname' + + class Module: + myapp = Flask('appname1') + @staticmethod + def create_app(): + return Flask('appname2') + assert find_best_app(Module) == Module.myapp + + class Module: + myapp = Flask('appname1') + @staticmethod + def create_app(foo): + return Flask('appname2') + assert find_best_app(Module) == Module.myapp + class Module: pass pytest.raises(NoAppException, find_best_app, Module) @@ -60,6 +88,12 @@ def test_find_best_app(test_apps): myapp2 = Flask('appname2') pytest.raises(NoAppException, find_best_app, Module) + class Module: + @staticmethod + def create_app(foo): + return Flask('appname2') + pytest.raises(NoAppException, find_best_app, Module) + def test_prepare_exec_for_file(test_apps): """Expect the correct path to be set and the correct module name to be returned. From b4eb6534d52e86af3979b06734e5779f6bb9a4f6 Mon Sep 17 00:00:00 2001 From: Hendrik Makait Date: Mon, 22 May 2017 14:26:00 -0700 Subject: [PATCH 2/4] Remove unnecessary checks and reformat NoAppException messages --- flask/cli.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/flask/cli.py b/flask/cli.py index cdb7f094..109ba3fe 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -37,7 +37,7 @@ def find_best_app(module): # Search for the most common names first. for attr_name in 'app', 'application': app = getattr(module, attr_name, None) - if app is not None and isinstance(app, Flask): + if isinstance(app, Flask): return app # Otherwise find the only object that is a Flask instance. @@ -50,21 +50,23 @@ def find_best_app(module): # Search for app factory callables. for attr_name in 'create_app', 'make_app': app_factory = getattr(module, attr_name, None) - if app_factory is not None and callable(app_factory): + if callable(app_factory): try: app = app_factory() - if app is not None and isinstance(app, Flask): + if isinstance(app, Flask): return app except TypeError: - raise NoAppException('Auto-detected "%s()" in module "%s", ' - 'but could not call it without ' - 'specifying arguments.' - % (attr_name, module.__name__)) + raise NoAppException( + 'Auto-detected "{callable}()" in module "{module}", but ' + 'could not call it without specifying arguments.' + .format(callable=attr_name, + module=module.__name__)) - raise NoAppException('Failed to find application in module "%s". Are ' - 'you sure it contains a Flask application? Maybe ' - 'you wrapped it in a WSGI middleware or you are ' - 'using a factory function.' % module.__name__) + raise NoAppException( + 'Failed to find application in module "{module}". Are you sure ' + 'it contains a Flask application? Maybe you wrapped it in a WSGI ' + 'middleware or you are using a factory function.' + .format(module=module.__name__)) def prepare_exec_for_file(filename): From 7ecdbcfa2b2d1c83c8c6561b49303ed5b1042350 Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 22 May 2017 15:48:08 -0700 Subject: [PATCH 3/4] show error if multiple Flask instances are detected add changelog --- CHANGES | 3 +++ flask/cli.py | 28 +++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CHANGES b/CHANGES index 76a0d7a2..7effdfac 100644 --- a/CHANGES +++ b/CHANGES @@ -40,6 +40,8 @@ Major release, unreleased - Allow IP address as exact session cookie domain. (`#2282`_) - ``SESSION_COOKIE_DOMAIN`` is set if it is detected through ``SERVER_NAME``. (`#2282`_) +- Auto-detect 0-argument app factory called ``create_app`` or ``make_app`` from + ``FLASK_APP``. (`#2297`_) .. _#1489: https://github.com/pallets/flask/pull/1489 .. _#1898: https://github.com/pallets/flask/pull/1898 @@ -50,6 +52,7 @@ Major release, unreleased .. _#2256: https://github.com/pallets/flask/pull/2256 .. _#2259: https://github.com/pallets/flask/pull/2259 .. _#2282: https://github.com/pallets/flask/pull/2282 +.. _#2297: https://github.com/pallets/flask/pull/2297 Version 0.12.2 -------------- diff --git a/flask/cli.py b/flask/cli.py index 109ba3fe..a10cf5e5 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -41,32 +41,42 @@ def find_best_app(module): return app # Otherwise find the only object that is a Flask instance. - matches = [v for k, v in iteritems(module.__dict__) - if isinstance(v, Flask)] + matches = [ + v for k, v in iteritems(module.__dict__) if isinstance(v, Flask) + ] if len(matches) == 1: return matches[0] + elif len(matches) > 1: + raise NoAppException( + 'Auto-detected multiple Flask applications in module "{module}".' + ' Use "FLASK_APP={module}:name" to specify the correct' + ' one.'.format(module=module.__name__) + ) # Search for app factory callables. for attr_name in 'create_app', 'make_app': app_factory = getattr(module, attr_name, None) + if callable(app_factory): try: app = app_factory() + if isinstance(app, Flask): return app except TypeError: raise NoAppException( 'Auto-detected "{callable}()" in module "{module}", but ' - 'could not call it without specifying arguments.' - .format(callable=attr_name, - module=module.__name__)) + 'could not call it without specifying arguments.'.format( + callable=attr_name, module=module.__name__ + ) + ) raise NoAppException( - 'Failed to find application in module "{module}". Are you sure ' - 'it contains a Flask application? Maybe you wrapped it in a WSGI ' - 'middleware or you are using a factory function.' - .format(module=module.__name__)) + 'Failed to find application in module "{module}". Are you sure ' + 'it contains a Flask application? Maybe you wrapped it in a WSGI ' + 'middleware.'.format(module=module.__name__) + ) def prepare_exec_for_file(filename): From 01ddf54b87850c50ee9020c6027647e0c5fca283 Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 22 May 2017 16:12:23 -0700 Subject: [PATCH 4/4] adjust for loop style --- CHANGES | 4 ++-- flask/cli.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 7effdfac..d243d66d 100644 --- a/CHANGES +++ b/CHANGES @@ -40,8 +40,8 @@ Major release, unreleased - Allow IP address as exact session cookie domain. (`#2282`_) - ``SESSION_COOKIE_DOMAIN`` is set if it is detected through ``SERVER_NAME``. (`#2282`_) -- Auto-detect 0-argument app factory called ``create_app`` or ``make_app`` from - ``FLASK_APP``. (`#2297`_) +- Auto-detect zero-argument app factory called ``create_app`` or ``make_app`` + from ``FLASK_APP``. (`#2297`_) .. _#1489: https://github.com/pallets/flask/pull/1489 .. _#1898: https://github.com/pallets/flask/pull/1898 diff --git a/flask/cli.py b/flask/cli.py index a10cf5e5..6aa66f4f 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -35,7 +35,7 @@ def find_best_app(module): from . import Flask # Search for the most common names first. - for attr_name in 'app', 'application': + for attr_name in ('app', 'application'): app = getattr(module, attr_name, None) if isinstance(app, Flask): return app @@ -55,7 +55,7 @@ def find_best_app(module): ) # Search for app factory callables. - for attr_name in 'create_app', 'make_app': + for attr_name in ('create_app', 'make_app'): app_factory = getattr(module, attr_name, None) if callable(app_factory):