PageRenderTime 532ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/Paste-1.7.5.1-py2.7.egg/paste/config.py

https://github.com/renfers/ageliaco.tracker
Python | 120 lines | 68 code | 5 blank | 47 comment | 1 complexity | 826a433d8e5c42cc7e56ccb94c11f30c MD5 | raw file
  1. # (c) 2006 Ian Bicking, Philip Jenvey and contributors
  2. # Written for Paste (http://pythonpaste.org)
  3. # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
  4. """Paste Configuration Middleware and Objects"""
  5. from paste.registry import RegistryManager, StackedObjectProxy
  6. __all__ = ['DispatchingConfig', 'CONFIG', 'ConfigMiddleware']
  7. class DispatchingConfig(StackedObjectProxy):
  8. """
  9. This is a configuration object that can be used globally,
  10. imported, have references held onto. The configuration may differ
  11. by thread (or may not).
  12. Specific configurations are registered (and deregistered) either
  13. for the process or for threads.
  14. """
  15. # @@: What should happen when someone tries to add this
  16. # configuration to itself? Probably the conf should become
  17. # resolved, and get rid of this delegation wrapper
  18. def __init__(self, name='DispatchingConfig'):
  19. super(DispatchingConfig, self).__init__(name=name)
  20. self.__dict__['_process_configs'] = []
  21. def push_thread_config(self, conf):
  22. """
  23. Make ``conf`` the active configuration for this thread.
  24. Thread-local configuration always overrides process-wide
  25. configuration.
  26. This should be used like::
  27. conf = make_conf()
  28. dispatching_config.push_thread_config(conf)
  29. try:
  30. ... do stuff ...
  31. finally:
  32. dispatching_config.pop_thread_config(conf)
  33. """
  34. self._push_object(conf)
  35. def pop_thread_config(self, conf=None):
  36. """
  37. Remove a thread-local configuration. If ``conf`` is given,
  38. it is checked against the popped configuration and an error
  39. is emitted if they don't match.
  40. """
  41. self._pop_object(conf)
  42. def push_process_config(self, conf):
  43. """
  44. Like push_thread_config, but applies the configuration to
  45. the entire process.
  46. """
  47. self._process_configs.append(conf)
  48. def pop_process_config(self, conf=None):
  49. self._pop_from(self._process_configs, conf)
  50. def _pop_from(self, lst, conf):
  51. popped = lst.pop()
  52. if conf is not None and popped is not conf:
  53. raise AssertionError(
  54. "The config popped (%s) is not the same as the config "
  55. "expected (%s)"
  56. % (popped, conf))
  57. def _current_obj(self):
  58. try:
  59. return super(DispatchingConfig, self)._current_obj()
  60. except TypeError:
  61. if self._process_configs:
  62. return self._process_configs[-1]
  63. raise AttributeError(
  64. "No configuration has been registered for this process "
  65. "or thread")
  66. current = current_conf = _current_obj
  67. CONFIG = DispatchingConfig()
  68. no_config = object()
  69. class ConfigMiddleware(RegistryManager):
  70. """
  71. A WSGI middleware that adds a ``paste.config`` key (by default)
  72. to the request environment, as well as registering the
  73. configuration temporarily (for the length of the request) with
  74. ``paste.config.CONFIG`` (or any other ``DispatchingConfig``
  75. object).
  76. """
  77. def __init__(self, application, config, dispatching_config=CONFIG,
  78. environ_key='paste.config'):
  79. """
  80. This delegates all requests to `application`, adding a *copy*
  81. of the configuration `config`.
  82. """
  83. def register_config(environ, start_response):
  84. popped_config = environ.get(environ_key, no_config)
  85. current_config = environ[environ_key] = config.copy()
  86. environ['paste.registry'].register(dispatching_config,
  87. current_config)
  88. try:
  89. app_iter = application(environ, start_response)
  90. finally:
  91. if popped_config is no_config:
  92. environ.pop(environ_key, None)
  93. else:
  94. environ[environ_key] = popped_config
  95. return app_iter
  96. super(self.__class__, self).__init__(register_config)
  97. def make_config_filter(app, global_conf, **local_conf):
  98. conf = global_conf.copy()
  99. conf.update(local_conf)
  100. return ConfigMiddleware(app, conf)
  101. make_config_middleware = ConfigMiddleware.__doc__