PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/cherrypy/cherrypy/_cpconfig.py

https://github.com/theosp/google_appengine
Python | 295 lines | 244 code | 22 blank | 29 comment | 11 complexity | aaf6fab9044a383d1499fd3011daf807 MD5 | raw file
  1. """
  2. Configuration system for CherryPy.
  3. Configuration in CherryPy is implemented via dictionaries. Keys are strings
  4. which name the mapped value, which may be of any type.
  5. Architecture
  6. ------------
  7. CherryPy Requests are part of an Application, which runs in a global context,
  8. and configuration data may apply to any of those three scopes:
  9. Global
  10. Configuration entries which apply everywhere are stored in
  11. cherrypy.config.
  12. Application
  13. Entries which apply to each mounted application are stored
  14. on the Application object itself, as 'app.config'. This is a two-level
  15. dict where each key is a path, or "relative URL" (for example, "/" or
  16. "/path/to/my/page"), and each value is a config dict. Usually, this
  17. data is provided in the call to tree.mount(root(), config=conf),
  18. although you may also use app.merge(conf).
  19. Request
  20. Each Request object possesses a single 'Request.config' dict.
  21. Early in the request process, this dict is populated by merging global
  22. config entries, Application entries (whose path equals or is a parent
  23. of Request.path_info), and any config acquired while looking up the
  24. page handler (see next).
  25. Declaration
  26. -----------
  27. Configuration data may be supplied as a Python dictionary, as a filename,
  28. or as an open file object. When you supply a filename or file, CherryPy
  29. uses Python's builtin ConfigParser; you declare Application config by
  30. writing each path as a section header::
  31. [/path/to/my/page]
  32. request.stream = True
  33. To declare global configuration entries, place them in a [global] section.
  34. You may also declare config entries directly on the classes and methods
  35. (page handlers) that make up your CherryPy application via the ``_cp_config``
  36. attribute. For example::
  37. class Demo:
  38. _cp_config = {'tools.gzip.on': True}
  39. def index(self):
  40. return "Hello world"
  41. index.exposed = True
  42. index._cp_config = {'request.show_tracebacks': False}
  43. .. note::
  44. This behavior is only guaranteed for the default dispatcher.
  45. Other dispatchers may have different restrictions on where
  46. you can attach _cp_config attributes.
  47. Namespaces
  48. ----------
  49. Configuration keys are separated into namespaces by the first "." in the key.
  50. Current namespaces:
  51. engine
  52. Controls the 'application engine', including autoreload.
  53. These can only be declared in the global config.
  54. tree
  55. Grafts cherrypy.Application objects onto cherrypy.tree.
  56. These can only be declared in the global config.
  57. hooks
  58. Declares additional request-processing functions.
  59. log
  60. Configures the logging for each application.
  61. These can only be declared in the global or / config.
  62. request
  63. Adds attributes to each Request.
  64. response
  65. Adds attributes to each Response.
  66. server
  67. Controls the default HTTP server via cherrypy.server.
  68. These can only be declared in the global config.
  69. tools
  70. Runs and configures additional request-processing packages.
  71. wsgi
  72. Adds WSGI middleware to an Application's "pipeline".
  73. These can only be declared in the app's root config ("/").
  74. checker
  75. Controls the 'checker', which looks for common errors in
  76. app state (including config) when the engine starts.
  77. Global config only.
  78. The only key that does not exist in a namespace is the "environment" entry.
  79. This special entry 'imports' other config entries from a template stored in
  80. cherrypy._cpconfig.environments[environment]. It only applies to the global
  81. config, and only when you use cherrypy.config.update.
  82. You can define your own namespaces to be called at the Global, Application,
  83. or Request level, by adding a named handler to cherrypy.config.namespaces,
  84. app.namespaces, or app.request_class.namespaces. The name can
  85. be any string, and the handler must be either a callable or a (Python 2.5
  86. style) context manager.
  87. """
  88. import cherrypy
  89. from cherrypy._cpcompat import set, basestring
  90. from cherrypy.lib import reprconf
  91. # Deprecated in CherryPy 3.2--remove in 3.3
  92. NamespaceSet = reprconf.NamespaceSet
  93. def merge(base, other):
  94. """Merge one app config (from a dict, file, or filename) into another.
  95. If the given config is a filename, it will be appended to
  96. the list of files to monitor for "autoreload" changes.
  97. """
  98. if isinstance(other, basestring):
  99. cherrypy.engine.autoreload.files.add(other)
  100. # Load other into base
  101. for section, value_map in reprconf.as_dict(other).items():
  102. if not isinstance(value_map, dict):
  103. raise ValueError(
  104. "Application config must include section headers, but the "
  105. "config you tried to merge doesn't have any sections. "
  106. "Wrap your config in another dict with paths as section "
  107. "headers, for example: {'/': config}.")
  108. base.setdefault(section, {}).update(value_map)
  109. class Config(reprconf.Config):
  110. """The 'global' configuration data for the entire CherryPy process."""
  111. def update(self, config):
  112. """Update self from a dict, file or filename."""
  113. if isinstance(config, basestring):
  114. # Filename
  115. cherrypy.engine.autoreload.files.add(config)
  116. reprconf.Config.update(self, config)
  117. def _apply(self, config):
  118. """Update self from a dict."""
  119. if isinstance(config.get("global", None), dict):
  120. if len(config) > 1:
  121. cherrypy.checker.global_config_contained_paths = True
  122. config = config["global"]
  123. if 'tools.staticdir.dir' in config:
  124. config['tools.staticdir.section'] = "global"
  125. reprconf.Config._apply(self, config)
  126. def __call__(self, *args, **kwargs):
  127. """Decorator for page handlers to set _cp_config."""
  128. if args:
  129. raise TypeError(
  130. "The cherrypy.config decorator does not accept positional "
  131. "arguments; you must use keyword arguments.")
  132. def tool_decorator(f):
  133. if not hasattr(f, "_cp_config"):
  134. f._cp_config = {}
  135. for k, v in kwargs.items():
  136. f._cp_config[k] = v
  137. return f
  138. return tool_decorator
  139. Config.environments = environments = {
  140. "staging": {
  141. 'engine.autoreload_on': False,
  142. 'checker.on': False,
  143. 'tools.log_headers.on': False,
  144. 'request.show_tracebacks': False,
  145. 'request.show_mismatched_params': False,
  146. },
  147. "production": {
  148. 'engine.autoreload_on': False,
  149. 'checker.on': False,
  150. 'tools.log_headers.on': False,
  151. 'request.show_tracebacks': False,
  152. 'request.show_mismatched_params': False,
  153. 'log.screen': False,
  154. },
  155. "embedded": {
  156. # For use with CherryPy embedded in another deployment stack.
  157. 'engine.autoreload_on': False,
  158. 'checker.on': False,
  159. 'tools.log_headers.on': False,
  160. 'request.show_tracebacks': False,
  161. 'request.show_mismatched_params': False,
  162. 'log.screen': False,
  163. 'engine.SIGHUP': None,
  164. 'engine.SIGTERM': None,
  165. },
  166. "test_suite": {
  167. 'engine.autoreload_on': False,
  168. 'checker.on': False,
  169. 'tools.log_headers.on': False,
  170. 'request.show_tracebacks': True,
  171. 'request.show_mismatched_params': True,
  172. 'log.screen': False,
  173. },
  174. }
  175. def _server_namespace_handler(k, v):
  176. """Config handler for the "server" namespace."""
  177. atoms = k.split(".", 1)
  178. if len(atoms) > 1:
  179. # Special-case config keys of the form 'server.servername.socket_port'
  180. # to configure additional HTTP servers.
  181. if not hasattr(cherrypy, "servers"):
  182. cherrypy.servers = {}
  183. servername, k = atoms
  184. if servername not in cherrypy.servers:
  185. from cherrypy import _cpserver
  186. cherrypy.servers[servername] = _cpserver.Server()
  187. # On by default, but 'on = False' can unsubscribe it (see below).
  188. cherrypy.servers[servername].subscribe()
  189. if k == 'on':
  190. if v:
  191. cherrypy.servers[servername].subscribe()
  192. else:
  193. cherrypy.servers[servername].unsubscribe()
  194. else:
  195. setattr(cherrypy.servers[servername], k, v)
  196. else:
  197. setattr(cherrypy.server, k, v)
  198. Config.namespaces["server"] = _server_namespace_handler
  199. def _engine_namespace_handler(k, v):
  200. """Backward compatibility handler for the "engine" namespace."""
  201. engine = cherrypy.engine
  202. if k == 'autoreload_on':
  203. if v:
  204. engine.autoreload.subscribe()
  205. else:
  206. engine.autoreload.unsubscribe()
  207. elif k == 'autoreload_frequency':
  208. engine.autoreload.frequency = v
  209. elif k == 'autoreload_match':
  210. engine.autoreload.match = v
  211. elif k == 'reload_files':
  212. engine.autoreload.files = set(v)
  213. elif k == 'deadlock_poll_freq':
  214. engine.timeout_monitor.frequency = v
  215. elif k == 'SIGHUP':
  216. engine.listeners['SIGHUP'] = set([v])
  217. elif k == 'SIGTERM':
  218. engine.listeners['SIGTERM'] = set([v])
  219. elif "." in k:
  220. plugin, attrname = k.split(".", 1)
  221. plugin = getattr(engine, plugin)
  222. if attrname == 'on':
  223. if v and hasattr(getattr(plugin, 'subscribe', None), '__call__'):
  224. plugin.subscribe()
  225. return
  226. elif (not v) and hasattr(getattr(plugin, 'unsubscribe', None), '__call__'):
  227. plugin.unsubscribe()
  228. return
  229. setattr(plugin, attrname, v)
  230. else:
  231. setattr(engine, k, v)
  232. Config.namespaces["engine"] = _engine_namespace_handler
  233. def _tree_namespace_handler(k, v):
  234. """Namespace handler for the 'tree' config namespace."""
  235. if isinstance(v, dict):
  236. for script_name, app in v.items():
  237. cherrypy.tree.graft(app, script_name)
  238. cherrypy.engine.log("Mounted: %s on %s" % (app, script_name or "/"))
  239. else:
  240. cherrypy.tree.graft(v, v.script_name)
  241. cherrypy.engine.log("Mounted: %s on %s" % (v, v.script_name or "/"))
  242. Config.namespaces["tree"] = _tree_namespace_handler