Skip to content
Myr

Refactored script info to allow more advanced patterns.

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 b9013ed · Browse files · 1 file+43 added−17 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+43 added−17 removed

  • other:+43 added−17 removed

1 file+43 added−17 removed

modified:flask/cli.py+43 added-17 removed
108 unmodified lines
109 the application upfront because it means that we can forward all110 errors for import problems into the browser as error.111 """112113 def __init__(self, app_id, debug=None, use_eager_loading=False):114 self.app_id = app_id115 self.app = None116 self.debug = debug113 def __init__(self, loader, use_eager_loading=False):114 self.loader = loader115 self._app = None116 self._lock = Lock()117 if use_eager_loading:118 self._load_unlocked()119120 def _load_unlocked(self):122 self.app = rv = locate_app(self.app_id, self.debug)121 self._app = rv = self.loader()122 return rv123124 def __call__(self, environ, start_response):126 if self.app is not None:127 return self.app(environ, start_response)125 if self._app is not None:126 return self._app(environ, start_response)127 with self._lock:129 if self.app is not None:130 rv = self.app128 if self._app is not None:129 rv = self._app130 else:131 rv = self._load_unlocked()132 return rv(environ, start_response)133134135def _no_such_app():136 raise NoAppException('Could not locate Flask application. '137 'You did not provide FLASK_APP or the '138 '--app parameter.')139140141class ScriptInfo(object):142 """Help object to deal with Flask applications. This is usually not143 necessary to interface with as it's used internally in the dispatching144 to click.145 """146142 def __init__(self, app_import_path=None, debug=None):147 def __init__(self, app_import_path=None, debug=None, load_callback=None):148 self.app_import_path = app_import_path149 self.debug = debug150 self.load_callback = load_callback151 self._loaded_app = None152153 def get_app_import_path(self):154 """Return the actual application import path or fails if it is155 not yet set.156 """157 if self.app_import_path is not None:158 return self.app_import_path153 raise NoAppException('Could not locate application. '154 'You did not provide FLASK_APP or the '155 '--app parameter.')159 _no_such_app()160161 def load_app(self):162 """Loads the app (if not yet loaded) and returns it."""163 if self._loaded_app is not None:164 return self._loaded_app165 if self.load_callback is not None:166 rv = self.load_callback()167 else:161 rv = locate_app(self.get_app_import_path(), self.debug)168 rv = locate_app(self.get_app_import_path(), self.debug)169 self._loaded_app = rv170 return rv171172 def make_wsgi_app(self, use_eager_loading=False):173 """Returns a WSGI app that loads the actual application at a174 later stage.175 """176 if self.app_import_path is not None:177 def loader():178 return locate_app(self.app_import_path, self.debug)179 else:180 if self.load_callback is None:181 _no_such_app()182 def loader():183 return self.load_callback()184 return DispatchingApp(loader, use_eager_loading=use_eager_loading)185186 @contextmanager187 def conditional_context(self, with_context=True):188 """Creates an application context or not, depending on the189 given parameter but always works as context manager.
111 unmodified lines
301def run_command(info, host, port, reload, debugger, eager_loading,302 with_threads):303 """Runs a local development server for the Flask application."""304 from werkzeug.serving import run_simple284 app_id = info.get_app_import_path()305 if reload is None:306 reload = info.debug307 if debugger is None:308 debugger = info.debug309 if eager_loading is None:310 eager_loading = not reload311312 app = info.make_wsgi_app(use_eager_loading=eager_loading)313314 # Extra startup messages. This depends a but on Werkzeug internals to315 # not double execute when the reloader kicks in.316 if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':317 # If we have an import path we can print it out now which can help318 # people understand what's being served. If we do not have an319 # import path because the app was loaded through a callback then320 # we won't print anything.321 if info.app_import_path is not None:295 print(' * Serving Flask app "%s"' % app_id)322 print(' * Serving Flask app "%s"' % info.app_import_path)323 if info.debug is not None:324 print(' * Forcing debug %s' % (info.debug and 'on' or 'off'))325299 app = DispatchingApp(app_id, info.debug, eager_loading)326 run_simple(host, port, app, use_reloader=reload,327 use_debugger=debugger, threaded=with_threads)328329
53 unmodified lines