Browse Source

Refactored loading logic to super properly.

pull/1040/head
Armin Ronacher 11 years ago
parent
commit
ed7b4ccac1
  1. 59
      flask/cli.py

59
flask/cli.py

@ -208,30 +208,7 @@ def without_appcontext(f):
return f
class FlaskGroup(click.Group):
"""Special subclass of the a regular click group that supports
loading more commands from the configured Flask app.
"""
def __init__(self, help=None):
def set_app_id(ctx, value):
if value is not None:
if os.path.isfile(value):
value = prepare_exec_for_file(value)
elif '.' not in sys.path:
sys.path.insert(0, '.')
ctx.ensure_object(ScriptInfo).app_import_path = value
def set_debug(ctx, value):
ctx.ensure_object(ScriptInfo).debug = value
click.Group.__init__(self, help=help, params=[
click.Option(['-a', '--app'],
help='The application to run',
callback=set_app_id, is_eager=True),
click.Option(['--debug/--no-debug'],
help='Enable or disable debug mode.',
default=None, callback=set_debug)
])
class ContextGroupMixin(object):
def get_command(self, ctx, name):
info = ctx.find_object(ScriptInfo)
@ -243,12 +220,12 @@ class FlaskGroup(click.Group):
return rv
except NoAppException:
pass
return click.Group.get_command(self, ctx, name)
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(click.Group.list_commands(self, ctx))
rv = set(super(ContextGroupMixin, self).list_commands(ctx))
info = ctx.find_object(ScriptInfo)
try:
rv.update(info.load_app().cli.list_commands(ctx))
@ -261,8 +238,34 @@ class FlaskGroup(click.Group):
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)
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.
"""
def __init__(self, help=None):
def set_app_id(ctx, value):
if value is not None:
if os.path.isfile(value):
value = prepare_exec_for_file(value)
elif '.' not in sys.path:
sys.path.insert(0, '.')
ctx.ensure_object(ScriptInfo).app_import_path = value
def set_debug(ctx, value):
ctx.ensure_object(ScriptInfo).debug = value
click.Group.__init__(self, help=help, params=[
click.Option(['-a', '--app'],
help='The application to run',
callback=set_app_id, is_eager=True),
click.Option(['--debug/--no-debug'],
help='Enable or disable debug mode.',
default=None, callback=set_debug)
])
cli = FlaskGroup(help='''\

Loading…
Cancel
Save