globals.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. """
  3. flask.globals
  4. ~~~~~~~~~~~~~
  5. Defines all the global objects that are proxies to the current
  6. active context.
  7. :copyright: (c) 2011 by Armin Ronacher.
  8. :license: BSD, see LICENSE for more details.
  9. """
  10. from functools import partial
  11. from werkzeug.local import LocalStack, LocalProxy
  12. def _lookup_req_object(name):
  13. top = _request_ctx_stack.top
  14. if top is None:
  15. raise RuntimeError('working outside of request context')
  16. return getattr(top, name)
  17. def _lookup_app_object(name):
  18. top = _app_ctx_stack.top
  19. if top is None:
  20. raise RuntimeError('working outside of application context')
  21. return getattr(top, name)
  22. def _find_app():
  23. top = _app_ctx_stack.top
  24. if top is None:
  25. raise RuntimeError('working outside of application context')
  26. return top.app
  27. # context locals
  28. _request_ctx_stack = LocalStack()
  29. _app_ctx_stack = LocalStack()
  30. current_app = LocalProxy(_find_app)
  31. request = LocalProxy(partial(_lookup_req_object, 'request'))
  32. session = LocalProxy(partial(_lookup_req_object, 'session'))
  33. g = LocalProxy(partial(_lookup_app_object, 'g'))