Made FlaskGroup an AppGroup
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 7c16054 · Browse files · 1 file·+30 added−30 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+30 added−30 removed
- other:+30 added−30 removed
Provenance
- Commit
9641f07- Tree
2900dff- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7c16054
1 file changed+30 added−30 removed
modified:flask/cli.py+30 added-30 removed
204 unmodified lines205 help='The application to run',206 callback=set_app_value, is_eager=True)207 208 209-class FlaskGroup(click.Group):210- """Special subclass of the a regular click group that supports loading211- more commands from the configured Flask app. Normally a developer212- does not have to interface with this class but there are some very213- advanced usecases for which it makes sense to create an instance of214- this. Not to be confused with :class:`AppGroup`.209+class AppGroup(click.Group):210+ """This works similar to a regular click :class:`~click.Group` but it211+ changes the behavior of the :meth:`command` decorator so that it212+ automatically wraps the functions in :func:`with_appcontext`.213 214+ Not to be confused with :class:`FlaskGroup`.215+ """216+217+ def command(self, *args, **kwargs):218+ """This works exactly like the method of the same name on a regular219+ :class:`click.Group` but it wraps callbacks in :func:`with_appcontext`220+ unless it's disabled by passing ``with_appcontext=False``.221+ """222+ wrap_for_ctx = kwargs.pop('with_appcontext', True)223+ def decorator(f):224+ if wrap_for_ctx:225+ f = with_appcontext(f)226+ return click.Group.command(self, *args, **kwargs)(f)227+ return decorator228+229+230+class FlaskGroup(AppGroup):231+ """Special subclass of the the :class:`AppGroup` group that supports232+ loading more commands from the configured Flask app. Normally a233+ developer does not have to interface with this class but there are234+ some very advanced usecases for which it makes sense to create an235+ instance of this.236+237 For information as of why this is useful see :ref:`custom-scripts`.238 239 :param add_default_commands: if this is True then the default run and240 shell commands wil be added.14 unmodified lines255 params.append(app_option)256 if add_debug_option:257 params.append(debug_option)258 238- click.Group.__init__(self, params=params, **extra)259+ AppGroup.__init__(self, params=params, **extra)260 self.create_app = create_app261 262 if add_default_commands:263 self.add_command(run_command)6 unmodified lines270 # and not attach the default commands.271 #272 # This also means that the script stays functional in case the273 # application completely fails.253- rv = click.Group.get_command(self, ctx, name)274+ rv = AppGroup.get_command(self, ctx, name)275 if rv is not None:276 return rv277 278 info = ctx.ensure_object(ScriptInfo)24 unmodified lines303 if obj is None:304 obj = ScriptInfo(create_app=self.create_app)305 kwargs['obj'] = obj306 kwargs.setdefault('auto_envvar_prefix', 'FLASK')286- return click.Group.main(self, *args, **kwargs)307+ return AppGroup.main(self, *args, **kwargs)308 309 289-class AppGroup(click.Group):290- """This works similar to a regular click :class:`~click.Group` but it291- changes the behavior of the :meth:`command` decorator so that it292- automatically wraps the functions in :func:`with_appcontext`.293-294- Not to be confused with :class:`FlaskGroup`.295- """296-297- def command(self, *args, **kwargs):298- """This works exactly like the method of the same name on a regular299- :class:`click.Group` but it wraps callbacks in :func:`with_appcontext`300- unless it's disabled by passing ``with_appcontext=False``.301- """302- wrap_for_ctx = kwargs.pop('with_appcontext', True)303- def decorator(f):304- if wrap_for_ctx:305- f = with_appcontext(f)306- return click.Group.command(self, *args, **kwargs)(f)307- return decorator308-309-310 def script_info_option(*args, **kwargs):311 """This decorator works exactly like :func:`click.option` but is eager312 by default and stores the value in the :attr:`ScriptInfo.data`. This313 is useful to further customize an application factory in very complex150 unmodified lines