Skip to content
Myr

Factor in code review comments and refactor functions to be more naturally split.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Hendrik Makait · committed by Hendrik Makait · parent 7a1a594 · Browse files · 2 files+65 added−58 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+65 added−58 removed

  • tests:+6 added−0 removed
  • other:+59 added−58 removed

2 files+65 added−58 removed

modified:flask/cli.py+59 added-58 removed
80 unmodified lines
81 'middleware.'.format(module=module.__name__)82 )838485def call_factory(func, script_info):86 """Checks if the given app factory function has an argument named 87 ``script_info`` or just a single argument and calls the function passing 88 ``script_info`` if so. Otherwise, calls the function without any arguments89 and returns the result.85def call_factory(app_factory, script_info, arguments=()):86 """Takes an app factory, a ``script_info` object and optionally a tuple87 of arguments. Checks for the existence of a script_info argument and calls88 the app_factory depending on that and the arguments provided.89 """91 arguments = getargspec(func).args92 if 'script_info' in arguments:93 return func(script_info=script_info)94 elif len(arguments) == 1:95 return func(script_info)96 return func()90 arg_names = getargspec(app_factory).args91 if 'script_info' in arg_names:92 return app_factory(*arguments, script_info=script_info)93 elif arguments:94 return app_factory(*arguments)95 elif not arguments and len(arg_names) == 1:96 return app_factory(script_info)97 return app_factory()989999def call_factory_from_regex(match, mod, script_info):100 """Checks if the given regex match has specified arguments and if the 101 function takes a ``script_info`` argument and calls the function with102 the appropriate arguments."""103 function_name = match.group(1)104 arguments = match.group(2)105 if arguments:106 arguments = ast.literal_eval(107 "({arguments}, )".format(arguments=arguments))100def find_app_by_string(string, script_info, module):101 """Checks if the given string is a variable name or a function. If it is 102 a function, it checks for specified arguments and whether it takes 103 a ``script_info`` argument and calls the function with the appropriate 104 arguments. If it is a """105 from . import Flask106 function_regex = r'^(?P<name>\w+)(?:\((?P<args>.*)\))?$'107 match = re.match(function_regex, string)108 if match:109 name, args = match.groups()110 try:111 if args is not None:112 args = args.rstrip(' ,')113 if args:114 args = ast.literal_eval(115 "({args}, )".format(args=args))116 else:117 args = ()118 app_factory = getattr(module, name, None)119 app = call_factory(app_factory, script_info, args)120 else:121 attr = getattr(module, name, None)122 if inspect.isfunction(attr):123 app = call_factory(attr, script_info)124 else:125 app = attr126127 if isinstance(app, Flask):128 return app129 else:130 raise RuntimeError('Failed to find application in module '131 '"{name}"'.format(name=module))132 except TypeError as e:133 new_error = NoAppException(134 '{e}\nThe app factory "{factory}" in module "{module}" could'135 ' not be called with the specified arguments (and a'136 ' script_info argument automatically added if applicable).'137 ' Did you make sure to use the right number of arguments as'138 ' well as not using keyword arguments or'139 ' non-literals?'.format(e=e, factory=string, module=module))140 reraise(NoAppException, new_error, sys.exc_info()[2])141 else:109 arguments = ()110 app_factory = getattr(mod, function_name, None)111 app_factory_arg_names = getargspec(app_factory).args112 if 'script_info' in app_factory_arg_names:113 app = app_factory(*arguments, script_info=script_info)114 elif arguments:115 app = app_factory(*arguments)116 elif not arguments and len(app_factory_arg_names) == 1:117 app = app_factory(script_info)118 else:119 app = app_factory()120 return app142 raise NoAppException(143 'The provided string "{string}" is not a valid variable name'144 'or function expression.'.format(string=string))145146147def prepare_exec_for_file(filename):148 """Given a filename this will try to calculate the python path, add it149 to the search path and return the actual module name that is expected.150 """
47 unmodified lines
198 'ensure the extension is .py' % module)199200 mod = sys.modules[module]201 if app_obj is None:177 app = find_best_app(script_info, mod)202 return find_best_app(script_info, mod)203 else:179 function_regex = r'^([\w_][\w_\d]*)\((.*)\)$'180 match = re.match(function_regex, app_obj)181 try:182 if match:183 app = call_factory_from_regex(match, mod, script_info)184 else:185 attr = getattr(mod, app_obj, None)186 if inspect.isfunction(attr):187 app = call_factory(attr, script_info)188 else:189 app = attr190 except TypeError as e:191 new_error = NoAppException(192 '{e}\nThe app factory "{factory}" in module "{module}" could'193 ' not be called with the specified arguments (and a'194 ' script_info argument automatically added if applicable).'195 ' Did you make sure to use the right number of arguments as'196 ' well as not using keyword arguments or'197 ' non-literals?'.format(e=e, factory=app_obj, module=module))198 reraise(NoAppException, new_error, sys.exc_info()[2])199 if app is None:200 raise RuntimeError('Failed to find application in module '201 '"{name}"'.format(name=module))204 return find_app_by_string(app_obj, script_info, mod)205203 return app206205207def find_default_import_path():208 app = os.environ.get('FLASK_APP')209 if app is None:210 return
457 unmodified lines
modified:tests/test_cli.py+6 added
159 unmodified lines
160 assert locate_app(161 script_info, "cliapp.factory:create_app2('foo', 'bar')"162 ).name == "create_app2_foo_bar"163 assert locate_app(164 script_info, "cliapp.factory:create_app2('foo', 'bar', )"165 ).name == "create_app2_foo_bar"166 assert locate_app(167 script_info, "cliapp.factory:create_app3('baz', 'qux')"168 ).name == "create_app3_baz_qux"169 assert locate_app(script_info, "cliapp.multiapp:app1").name == "app1"170 pytest.raises(
5 unmodified lines
176 pytest.raises(177 NoAppException, locate_app,178 script_info, "cliapp.factory:create_app2('foo')")179 pytest.raises(180 NoAppException, locate_app,181 script_info, "cliapp.factory:create_app ()")182 pytest.raises(183 NoAppException, locate_app, script_info, "cliapp.importerrorapp")184185186def test_find_default_import_path(test_apps, monkeypatch, tmpdir):
182 unmodified lines