Factor out call_factory_from_regex function
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 7106fb6 · Browse files · 1 file·+24 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+24 added−17 removed
- other:+24 added−17 removed
Provenance
- Commit
7a1a594- Tree
284d1c5- Author
Hendrik Makait <hendrik.makait@googlemail.com>- Committer
Hendrik Makait <hendrik.makait@googlemail.com>- Parent
7106fb6
1 file changed+24 added−17 removed
modified:flask/cli.py+24 added-17 removed
94 unmodified lines95 return func(script_info)96 return func()97 98 99+def 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))108+ 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 app121+122 def prepare_exec_for_file(filename):123 """Given a filename this will try to calculate the python path, add it124 to the search path and return the actual module name that is expected.125 """53 unmodified lines179 function_regex = r'^([\w_][\w_\d]*)\((.*)\)$'180 match = re.match(function_regex, app_obj)181 try:182 if match:160- function_name = match.group(1)161- arguments = match.group(2)162- if arguments:163- arguments = ast.literal_eval(164- "({arguments}, )".format(arguments=arguments))165- else:166- arguments = ()167- app_factory = getattr(mod, function_name, None)168- app_factory_arg_names = getargspec(app_factory).args169- if 'script_info' in app_factory_arg_names:170- app = app_factory(*arguments, script_info=script_info)171- elif arguments:172- app = app_factory(*arguments)173- elif not arguments and len(app_factory_arg_names) == 1:174- app = app_factory(script_info)175- else:176- app = app_factory()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)479 unmodified lines