Skip to content

Commit 3bdb90f

Browse files
committed
Added click support to Flask
1 parent 0ac492a commit 3bdb90f

File tree

4 files changed

+392
-251
lines changed

4 files changed

+392
-251
lines changed

flask/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111

1212

1313
if __name__ == '__main__':
14-
from run import main
14+
from cli import main
1515
main(as_module=True)

flask/app.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
_default_template_ctx_processor
3535
from .signals import request_started, request_finished, got_request_exception, \
3636
request_tearing_down, appcontext_tearing_down
37+
from .cli import make_default_cli
3738
from ._compat import reraise, string_types, text_type, integer_types
3839

3940
# a lock used for logger initialization
@@ -476,6 +477,12 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
476477
None: [_default_template_ctx_processor]
477478
}
478479

480+
#: A list of shell context processor functions that should be run
481+
#: when a shell context is created.
482+
#:
483+
#: .. versionadded:: 1.0
484+
self.shell_context_processors = []
485+
479486
#: all the attached blueprints in a dictionary by name. Blueprints
480487
#: can be attached multiple times so this dictionary does not tell
481488
#: you how often they got attached.
@@ -531,6 +538,14 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
531538
endpoint='static',
532539
view_func=self.send_static_file)
533540

541+
#: The click command line context for this application. Commands
542+
#: registered here show up in the ``flask`` command once the
543+
#: application has been discovered. The default commands are
544+
#: provided by Flask itself and can be overridden.
545+
#:
546+
#: This is an instance of a :class:`click.Group` object.
547+
self.cli = make_default_cli(self)
548+
534549
def _get_error_handlers(self):
535550
from warnings import warn
536551
warn(DeprecationWarning('error_handlers is deprecated, use the '
@@ -748,6 +763,18 @@ def update_template_context(self, context):
748763
# existing views.
749764
context.update(orig_ctx)
750765

766+
def make_shell_context(self):
767+
"""Returns the shell context for an interactive shell for this
768+
application. This runs all the registered shell context
769+
processors.
770+
771+
.. versionadded:: 1.0
772+
"""
773+
rv = {'app': self, 'g': g}
774+
for processor in self.shell_context_processors:
775+
rv.update(processor())
776+
return rv
777+
751778
def run(self, host=None, port=None, debug=None, **options):
752779
"""Runs the application on a local development server. If the
753780
:attr:`debug` flag is set the server will automatically reload
@@ -758,6 +785,11 @@ def run(self, host=None, port=None, debug=None, **options):
758785
``use_evalex=False`` as parameter. This will keep the debugger's
759786
traceback screen active, but disable code execution.
760787
788+
It is not recommended to use this function for development with
789+
automatic reloading as this is badly supported. Instead you should
790+
be using the ``flask`` command line script's ``runserver``
791+
support.
792+
761793
.. admonition:: Keep in Mind
762794
763795
Flask will suppress any server error with a generic error page
@@ -1091,7 +1123,7 @@ def page_not_found(error):
10911123
Use :meth:`register_error_handler` instead of modifying
10921124
:attr:`error_handler_spec` directly, for application wide error
10931125
handlers.
1094-
1126+
10951127
.. versionadded:: 0.7
10961128
One can now additionally also register custom exception types
10971129
that do not necessarily have to be a subclass of the
@@ -1325,6 +1357,15 @@ def context_processor(self, f):
13251357
self.template_context_processors[None].append(f)
13261358
return f
13271359

1360+
@setupmethod
1361+
def shell_context_processor(self, f):
1362+
"""Registers a shell context processor function.
1363+
1364+
.. versionadded:: 1.0
1365+
"""
1366+
self.shell_context_processors.append(f)
1367+
return f
1368+
13281369
@setupmethod
13291370
def url_value_preprocessor(self, f):
13301371
"""Registers a function as URL value preprocessor for all view
@@ -1609,7 +1650,8 @@ def make_response(self, rv):
16091650
# some extra logic involved when creating these objects with
16101651
# specific values (like default content type selection).
16111652
if isinstance(rv, (text_type, bytes, bytearray)):
1612-
rv = self.response_class(rv, headers=headers, status=status_or_headers)
1653+
rv = self.response_class(rv, headers=headers,
1654+
status=status_or_headers)
16131655
headers = status_or_headers = None
16141656
else:
16151657
rv = self.response_class.force_type(rv, request.environ)
@@ -1624,7 +1666,6 @@ def make_response(self, rv):
16241666

16251667
return rv
16261668

1627-
16281669
def create_url_adapter(self, request):
16291670
"""Creates a URL adapter for the given request. The URL adapter
16301671
is created at a point where the request context is not yet set up

0 commit comments

Comments
 (0)