From 954d9ca0b8519ee651daba64ca9292aaa74fff0d Mon Sep 17 00:00:00 2001 From: Levi Roth Date: Tue, 23 May 2017 14:30:39 -0400 Subject: [PATCH 1/6] Added documentation for PowerShell environment variables --- docs/quickstart.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index aea776f3..d56fa8e2 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -50,7 +50,14 @@ to tell your terminal the application to work with by exporting the $ flask run * Running on http://127.0.0.1:5000/ -If you are on Windows you need to use ``set`` instead of ``export``. +If you are on Windows, the environment variable syntax depends on command line +interpreter. On Command Prompt:: + + C:\path\to\app>set FLASK_APP=hello.py + +And on PowerShell:: + + PS C:\path\to\app> $env:FLASK_APP = "hello.py" Alternatively you can use :command:`python -m flask`:: From ae41df9a77ceab4469029b043e38333b8654e1da Mon Sep 17 00:00:00 2001 From: Hendrik Makait Date: Tue, 23 May 2017 13:46:45 -0700 Subject: [PATCH 2/6] Check if app factory takes script_info argument and call it with(out) script_info as an argument depending on that --- flask/_compat.py | 2 ++ flask/cli.py | 30 +++++++++++++++++------ tests/test_cli.py | 62 +++++++++++++++++++++++++++++++---------------- 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/flask/_compat.py b/flask/_compat.py index 071628fc..173b3689 100644 --- a/flask/_compat.py +++ b/flask/_compat.py @@ -25,6 +25,7 @@ if not PY2: itervalues = lambda d: iter(d.values()) iteritems = lambda d: iter(d.items()) + from inspect import getfullargspec as getargspec from io import StringIO def reraise(tp, value, tb=None): @@ -43,6 +44,7 @@ else: itervalues = lambda d: d.itervalues() iteritems = lambda d: d.iteritems() + from inspect import getargspec from cStringIO import StringIO exec('def reraise(tp, value, tb=None):\n raise tp, value, tb') diff --git a/flask/cli.py b/flask/cli.py index 6aa66f4f..ea294ae6 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index bbb6fe58..1c843d61 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -39,60 +39,75 @@ def test_cli_name(test_apps): def test_find_best_app(test_apps): """Test if `find_best_app` behaves as expected with different combinations of input.""" + script_info = ScriptInfo() class Module: app = Flask('appname') - assert find_best_app(Module) == Module.app + assert find_best_app(script_info, Module) == Module.app class Module: application = Flask('appname') - assert find_best_app(Module) == Module.application + assert find_best_app(script_info, Module) == Module.application class Module: myapp = Flask('appname') - assert find_best_app(Module) == Module.myapp + assert find_best_app(script_info, 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' + assert isinstance(find_best_app(script_info, Module), Flask) + assert find_best_app(script_info, Module).name == 'appname' + + class Module: + @staticmethod + def create_app(foo): + return Flask('appname') + assert isinstance(find_best_app(script_info, Module), Flask) + assert find_best_app(script_info, Module).name == 'appname' + + class Module: + @staticmethod + def create_app(foo=None, script_info=None): + return Flask('appname') + assert isinstance(find_best_app(script_info, Module), Flask) + assert find_best_app(script_info, 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' + assert isinstance(find_best_app(script_info, Module), Flask) + assert find_best_app(script_info, Module).name == 'appname' class Module: myapp = Flask('appname1') @staticmethod def create_app(): return Flask('appname2') - assert find_best_app(Module) == Module.myapp + assert find_best_app(script_info, Module) == Module.myapp class Module: myapp = Flask('appname1') @staticmethod - def create_app(foo): + def create_app(): return Flask('appname2') - assert find_best_app(Module) == Module.myapp + assert find_best_app(script_info, Module) == Module.myapp class Module: pass - pytest.raises(NoAppException, find_best_app, Module) + pytest.raises(NoAppException, find_best_app, script_info, Module) class Module: myapp1 = Flask('appname1') myapp2 = Flask('appname2') - pytest.raises(NoAppException, find_best_app, Module) + pytest.raises(NoAppException, find_best_app, script_info, Module) class Module: @staticmethod - def create_app(foo): + def create_app(foo, bar): return Flask('appname2') - pytest.raises(NoAppException, find_best_app, Module) + pytest.raises(NoAppException, find_best_app, script_info, Module) def test_prepare_exec_for_file(test_apps): @@ -117,13 +132,18 @@ def test_prepare_exec_for_file(test_apps): def test_locate_app(test_apps): """Test of locate_app.""" - assert locate_app("cliapp.app").name == "testapp" - assert locate_app("cliapp.app:testapp").name == "testapp" - assert locate_app("cliapp.multiapp:app1").name == "app1" - pytest.raises(NoAppException, locate_app, "notanpp.py") - pytest.raises(NoAppException, locate_app, "cliapp/app") - pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp") - pytest.raises(NoAppException, locate_app, "cliapp.importerrorapp") + script_info = ScriptInfo() + assert locate_app(script_info, "cliapp.app").name == "testapp" + assert locate_app(script_info, "cliapp.app:testapp").name == "testapp" + assert locate_app(script_info, "cliapp.multiapp:app1").name == "app1" + pytest.raises(NoAppException, locate_app, + script_info, "notanpp.py") + pytest.raises(NoAppException, locate_app, + script_info, "cliapp/app") + pytest.raises(RuntimeError, locate_app, + script_info, "cliapp.app:notanapp") + pytest.raises(NoAppException, locate_app, + script_info, "cliapp.importerrorapp") def test_find_default_import_path(test_apps, monkeypatch, tmpdir): From d05862574f1ea19d99526ca893be2888389eab1c Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Tue, 23 May 2017 15:29:37 -0700 Subject: [PATCH 3/6] Update click dependency info. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 08995073..d97e4876 100644 --- a/setup.py +++ b/setup.py @@ -74,7 +74,7 @@ setup( 'Werkzeug>=0.7', 'Jinja2>=2.4', 'itsdangerous>=0.21', - 'click>=2.0', + 'click>=4.0', ], classifiers=[ 'Development Status :: 4 - Beta', From a11212377d74a48591dda7015c9208048d34caa3 Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 23 May 2017 16:08:46 -0700 Subject: [PATCH 4/6] add lowest click version to tox add devel click version to tox add devel markupsafe version to tox --- tox.ini | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index 764b4030..8946ace9 100644 --- a/tox.ini +++ b/tox.ini @@ -1,8 +1,6 @@ [tox] envlist = {py26,py27,pypy}-{lowest,release,devel}{,-simplejson}, {py33,py34,py35,py36}-{release,devel}{,-simplejson} - - [testenv] passenv = LANG usedevelop=true @@ -20,12 +18,18 @@ deps= lowest: Werkzeug==0.7 lowest: Jinja2==2.4 lowest: itsdangerous==0.21 + lowest: Click==4.0 lowest: blinker==1.0 + release: blinker + devel: git+https://github.com/pallets/werkzeug.git + devel: git+https://github.com/pallets/markupsafe.git devel: git+https://github.com/pallets/jinja.git devel: git+https://github.com/pallets/itsdangerous.git + devel: git+https://github.com/pallets/click.git devel: git+https://github.com/jek/blinker.git + simplejson: simplejson [testenv:docs] From 38df3df7357a152ae8e6fa8c596a7c9c595b2fc3 Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 23 May 2017 16:19:41 -0700 Subject: [PATCH 5/6] shorten cli factory name [ci skip] --- flask/cli.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/flask/cli.py b/flask/cli.py index ea294ae6..e6e8f65f 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -61,8 +61,7 @@ def find_best_app(script_info, module): if callable(app_factory): try: - app = check_factory_for_script_info_and_call(app_factory, - script_info) + app = call_factory(app_factory, script_info) if isinstance(app, Flask): return app except TypeError: @@ -80,19 +79,18 @@ def find_best_app(script_info, 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.""" +def call_factory(func, script_info): + """Checks if the given app factory function has an argument named + ``script_info`` or just a single argument and calls the function passing + ``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) + return func(script_info=script_info) elif len(arguments) == 1: - result = func(script_info) - else: - result = func() - return result + return func(script_info) + return func() def prepare_exec_for_file(filename): @@ -275,7 +273,7 @@ class ScriptInfo(object): if self._loaded_app is not None: return self._loaded_app if self.create_app is not None: - rv = check_factory_for_script_info_and_call(self.create_app, self) + rv = call_factory(self.create_app, self) else: if not self.app_import_path: raise NoAppException( From 964c5c5aca08a81164968b8d71eca6925cdbd935 Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 23 May 2017 16:24:07 -0700 Subject: [PATCH 6/6] add changelog [ci skip] --- CHANGES | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES b/CHANGES index d243d66d..c37d3f7f 100644 --- a/CHANGES +++ b/CHANGES @@ -42,6 +42,10 @@ Major release, unreleased (`#2282`_) - Auto-detect zero-argument app factory called ``create_app`` or ``make_app`` from ``FLASK_APP``. (`#2297`_) +- Factory functions are not required to take a ``script_info`` parameter to + work with the ``flask`` command. If they take a single parameter or a + parameter named ``script_info``, the ``ScriptInfo`` object will be passed. + (`#2319`_) .. _#1489: https://github.com/pallets/flask/pull/1489 .. _#1898: https://github.com/pallets/flask/pull/1898 @@ -53,6 +57,7 @@ Major release, unreleased .. _#2259: https://github.com/pallets/flask/pull/2259 .. _#2282: https://github.com/pallets/flask/pull/2282 .. _#2297: https://github.com/pallets/flask/pull/2297 +.. _#2319: https://github.com/pallets/flask/pull/2319 Version 0.12.2 --------------