From ed7b4ccac1b646aa725007db7fd3ec2d359beaf4 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 7 May 2014 11:36:49 +0200 Subject: [PATCH] Refactored loading logic to super properly. --- flask/cli.py | 67 +++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/flask/cli.py b/flask/cli.py index 2bcfdee7..adca5ca1 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -208,7 +208,41 @@ def without_appcontext(f): return f -class FlaskGroup(click.Group): +class ContextGroupMixin(object): + + def get_command(self, ctx, name): + info = ctx.find_object(ScriptInfo) + # Find the command in the application first, if we can find it. + # If the app is not available, we just ignore this silently. + try: + rv = info.load_app().cli.get_command(ctx, name) + if rv is not None: + return rv + except NoAppException: + pass + return super(ContextGroupMixin, self).get_command(ctx, name) + + def list_commands(self, ctx): + # The commands available is the list of both the application (if + # available) plus the builtin commands. + rv = set(super(ContextGroupMixin, self).list_commands(ctx)) + info = ctx.find_object(ScriptInfo) + try: + rv.update(info.load_app().cli.list_commands(ctx)) + except NoAppException: + pass + return sorted(rv) + + def invoke_subcommand(self, ctx, cmd, cmd_name, args): + with_context = cmd.callback is None or \ + not getattr(cmd.callback, '__flask_without_appcontext__', False) + + with ctx.find_object(ScriptInfo).conditional_context(with_context): + return super(ContextGroupMixin, self).invoke_subcommand( + ctx, cmd, cmd_name, args) + + +class FlaskGroup(ContextGroupMixin, click.Group): """Special subclass of the a regular click group that supports loading more commands from the configured Flask app. """ @@ -233,37 +267,6 @@ class FlaskGroup(click.Group): default=None, callback=set_debug) ]) - def get_command(self, ctx, name): - info = ctx.find_object(ScriptInfo) - # Find the command in the application first, if we can find it. - # If the app is not available, we just ignore this silently. - try: - rv = info.load_app().cli.get_command(ctx, name) - if rv is not None: - return rv - except NoAppException: - pass - return click.Group.get_command(self, ctx, name) - - def list_commands(self, ctx): - # The commands available is the list of both the application (if - # available) plus the builtin commands. - rv = set(click.Group.list_commands(self, ctx)) - info = ctx.find_object(ScriptInfo) - try: - rv.update(info.load_app().cli.list_commands(ctx)) - except NoAppException: - pass - return sorted(rv) - - def invoke_subcommand(self, ctx, cmd, cmd_name, args): - with_context = cmd.callback is None or \ - not getattr(cmd.callback, '__flask_without_appcontext__', False) - - with ctx.find_object(ScriptInfo).conditional_context(with_context): - return click.Group.invoke_subcommand( - self, ctx, cmd, cmd_name, args) - cli = FlaskGroup(help='''\ This shell command acts as general utility script for Flask applications.