Skip to content
Myr

Refactored loading logic to super properly.

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 4532e89 · Browse files · 1 file+31 added−28 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+31 added−28 removed

  • other:+31 added−28 removed

1 file+31 added−28 removed

modified:flask/cli.py+31 added-28 removed
206 unmodified lines
207 f.__flask_without_appcontext__ = True208 return f209210211class FlaskGroup(click.Group):212 """Special subclass of the a regular click group that supports213 loading more commands from the configured Flask app.214 """211class ContextGroupMixin(object):212216 def __init__(self, help=None):217 def set_app_id(ctx, value):218 if value is not None:219 if os.path.isfile(value):220 value = prepare_exec_for_file(value)221 elif '.' not in sys.path:222 sys.path.insert(0, '.')223 ctx.ensure_object(ScriptInfo).app_import_path = value224 def set_debug(ctx, value):225 ctx.ensure_object(ScriptInfo).debug = value226227 click.Group.__init__(self, help=help, params=[228 click.Option(['-a', '--app'],229 help='The application to run',230 callback=set_app_id, is_eager=True),231 click.Option(['--debug/--no-debug'],232 help='Enable or disable debug mode.',233 default=None, callback=set_debug)234 ])235213 def get_command(self, ctx, name):214 info = ctx.find_object(ScriptInfo)215 # Find the command in the application first, if we can find it.216 # If the app is not available, we just ignore this silently.
2 unmodified lines
219 if rv is not None:220 return rv221 except NoAppException:222 pass246 return click.Group.get_command(self, ctx, name)223 return super(ContextGroupMixin, self).get_command(ctx, name)224225 def list_commands(self, ctx):226 # The commands available is the list of both the application (if227 # available) plus the builtin commands.251 rv = set(click.Group.list_commands(self, ctx))228 rv = set(super(ContextGroupMixin, self).list_commands(ctx))229 info = ctx.find_object(ScriptInfo)230 try:231 rv.update(info.load_app().cli.list_commands(ctx))232 except NoAppException:
4 unmodified lines
237 with_context = cmd.callback is None or \238 not getattr(cmd.callback, '__flask_without_appcontext__', False)239240 with ctx.find_object(ScriptInfo).conditional_context(with_context):264 return click.Group.invoke_subcommand(265 self, ctx, cmd, cmd_name, args)241 return super(ContextGroupMixin, self).invoke_subcommand(242 ctx, cmd, cmd_name, args)243244245class FlaskGroup(ContextGroupMixin, click.Group):246 """Special subclass of the a regular click group that supports247 loading more commands from the configured Flask app.248 """249250 def __init__(self, help=None):251 def set_app_id(ctx, value):252 if value is not None:253 if os.path.isfile(value):254 value = prepare_exec_for_file(value)255 elif '.' not in sys.path:256 sys.path.insert(0, '.')257 ctx.ensure_object(ScriptInfo).app_import_path = value258 def set_debug(ctx, value):259 ctx.ensure_object(ScriptInfo).debug = value260261 click.Group.__init__(self, help=help, params=[262 click.Option(['-a', '--app'],263 help='The application to run',264 callback=set_app_id, is_eager=True),265 click.Option(['--debug/--no-debug'],266 help='Enable or disable debug mode.',267 default=None, callback=set_debug)268 ])269270271cli = FlaskGroup(help='''\272This shell command acts as general utility script for Flask applications.273274It loads the application configured (either through the FLASK_APP environment
111 unmodified lines