source file: /Library/Python/2.3/site-packages/CherryPy-3.0.1-py2.3.egg/cherrypy/_cpconfig.py
file stats: 120 lines, 50 executed: 41.7% covered
1. """Configuration system for CherryPy. 2. 3. Configuration in CherryPy is implemented via dictionaries. Keys are strings 4. which name the mapped value, which may be of any type. 5. 6. 7. Architecture 8. ------------ 9. 10. CherryPy Requests are part of an Application, which runs in a global context, 11. and configuration data may apply to any of those three scopes: 12. 13. Global: configuration entries which apply everywhere are stored in 14. cherrypy.config. 15. 16. Application: entries which apply to each mounted application are stored 17. on the Application object itself, as 'app.config'. This is a two-level 18. dict where each key is a path, or "relative URL" (for example, "/" or 19. "/path/to/my/page"), and each value is a config dict. Usually, this 20. data is provided in the call to tree.mount(root(), config=conf), 21. although you may also use app.merge(conf). 22. 23. Request: each Request object possesses a single 'Request.config' dict. 24. Early in the request process, this dict is populated by merging global 25. config entries, Application entries (whose path equals or is a parent 26. of Request.path_info), and any config acquired while looking up the 27. page handler (see next). 28. 29. 30. Declaration 31. ----------- 32. 33. Configuration data may be supplied as a Python dictionary, as a filename, 34. or as an open file object. When you supply a filename or file, CherryPy 35. uses Python's builtin ConfigParser; you declare Application config by 36. writing each path as a section header: 37. 38. [/path/to/my/page] 39. request.stream = True 40. 41. To declare global configuration entries, place them in a [global] section. 42. 43. You may also declare config entries directly on the classes and methods 44. (page handlers) that make up your CherryPy application via the '_cp_config' 45. attribute. For example: 46. 47. class Demo: 48. _cp_config = {'tools.gzip.on': True} 49. 50. def index(self): 51. return "Hello world" 52. index.exposed = True 53. index._cp_config = {'request.show_tracebacks': False} 54. 55. Note, however, that this behavior is only guaranteed for the default 56. dispatcher. Other dispatchers may have different restrictions on where 57. you can attach _cp_config attributes. 58. 59. 60. Namespaces 61. ---------- 62. 63. Configuration keys are separated into namespaces by the first "." in the key. 64. Current namespaces: 65. 66. engine: Controls the 'application engine', including autoreload. 67. These can only be declared in the global config. 68. hooks: Declares additional request-processing functions. 69. log: Configures the logging for each application. 70. These can only be declared in the global or / config. 71. request: Adds attributes to each Request. 72. response: Adds attributes to each Response. 73. server: Controls the default HTTP server via cherrypy.server. 74. These can only be declared in the global config. 75. tools: Runs and configures additional request-processing packages. 76. wsgi: Adds WSGI middleware to an Application's "pipeline". 77. These can only be declared in the app's root config ("/"). 78. checker: Controls the 'checker', which looks for common errors in 79. app state (including config) when the engine starts. 80. Global config only. 81. 82. The only key that does not exist in a namespace is the "environment" entry. 83. This special entry 'imports' other config entries from a template stored in 84. cherrypy._cpconfig.environments[environment]. It only applies to the global 85. config, and only when you use cherrypy.config.update. 86. 87. You can define your own namespaces to be called at the Global, Application, 88. or Request level, by adding a named handler to cherrypy.config.namespaces, 89. app.namespaces, or cherrypy.engine.request_class.namespaces. The name can 90. be any string, and the handler must be either a callable or a (Python 2.5 91. style) context manager. 92. """ 93. 94. import ConfigParser 95. import sys 96. from warnings import warn 97. 98. import cherrypy 99. 100. 101. environments = { 102. "staging": { 103. 'engine.autoreload_on': False, 104. 'checker.on': False, 105. 'tools.log_headers.on': False, 106. 'request.show_tracebacks': False, 107. }, 108. "production": { 109. 'engine.autoreload_on': False, 110. 'checker.on': False, 111. 'tools.log_headers.on': False, 112. 'request.show_tracebacks': False, 113. 'log.screen': False, 114. }, 115. "embedded": { 116. # For use with CherryPy embedded in another deployment stack. 117. 'engine.autoreload_on': False, 118. 'checker.on': False, 119. 'tools.log_headers.on': False, 120. 'request.show_tracebacks': False, 121. 'log.screen': False, 122. 'engine.SIGHUP': None, 123. 'engine.SIGTERM': None, 124. }, 125. "test_suite": { 126. 'engine.autoreload_on': False, 127. 'checker.on': False, 128. 'tools.log_headers.on': False, 129. 'request.show_tracebacks': True, 130. 'log.screen': False, 131. }, 132. } 133. 134. def as_dict(config): 135. """Return a dict from 'config' whether it is a dict, file, or filename.""" 136. if isinstance(config, basestring): 137. config = _Parser().dict_from_file(config) 138. elif hasattr(config, 'read'): 139. config = _Parser().dict_from_file(config) 140. return config 141. 142. def merge(base, other): 143. """Merge one app config (from a dict, file, or filename) into another. 144. 145. If the given config is a filename, it will be appended to 146. cherrypy.engine.reload_files and monitored for changes. 147. """ 148. if isinstance(other, basestring): 149. if other not in cherrypy.engine.reload_files: 150. cherrypy.engine.reload_files.append(other) 151. 152. # Load other into base 153. for section, value_map in as_dict(other).iteritems(): 154. base.setdefault(section, {}).update(value_map) 155. 156. 157. class NamespaceSet(dict): 158. """A dict of config namespace names and handlers. 159. 160. Each config entry should begin with a namespace name; the corresponding 161. namespace handler will be called once for each config entry in that 162. namespace, and will be passed two arguments: the config key (with the 163. namespace removed) and the config value. 164. 165. Namespace handlers may be any Python callable; they may also be 166. Python 2.5-style 'context managers', in which case their __enter__ 167. method should return a callable to be used as the handler. 168. See cherrypy.tools (the Toolbox class) for an example. 169. """ 170. 171. def __call__(self, config): 172. """Iterate through config and pass it to each namespace handler. 173. 174. 'config' should be a flat dict, where keys use dots to separate 175. namespaces, and values are arbitrary. 176. 177. The first name in each config key is used to look up the corresponding 178. namespace handler. For example, a config entry of {'tools.gzip.on': v} 179. will call the 'tools' namespace handler with the args: ('gzip.on', v) 180. """ 181. # Separate the given config into namespaces 182. ns_confs = {} 183. for k in config: 184. if "." in k: 185. ns, name = k.split(".", 1) 186. bucket = ns_confs.setdefault(ns, {}) 187. bucket[name] = config[k] 188. 189. # I chose __enter__ and __exit__ so someday this could be 190. # rewritten using Python 2.5's 'with' statement: 191. # for ns, handler in self.iteritems(): 192. # with handler as callable: 193. # for k, v in ns_confs.get(ns, {}).iteritems(): 194. # callable(k, v) 195. for ns, handler in self.iteritems(): 196. exit = getattr(handler, "__exit__", None) 197. if exit: 198. callable = handler.__enter__() 199. no_exc = True 200. try: 201. try: 202. for k, v in ns_confs.get(ns, {}).iteritems(): 203. callable(k, v) 204. except: 205. # The exceptional case is handled here 206. no_exc = False 207. if exit is None: 208. raise 209. if not exit(*sys.exc_info()): 210. raise 211. # The exception is swallowed if exit() returns true 212. finally: 213. # The normal and non-local-goto cases are handled here 214. if no_exc and exit: 215. exit(None, None, None) 216. else: 217. for k, v in ns_confs.get(ns, {}).iteritems(): 218. handler(k, v) 219. 220. def __repr__(self): 221. return "%s.%s(%s)" % (self.__module__, self.__class__.__name__, 222. dict.__repr__(self)) 223. 224. def __copy__(self): 225. newobj = self.__class__() 226. newobj.update(self) 227. return newobj 228. copy = __copy__ 229. 230. 231. class Config(dict): 232. """The 'global' configuration data for the entire CherryPy process.""" 233. 234. defaults = { 235. 'tools.log_tracebacks.on': True, 236. 'tools.log_headers.on': True, 237. 'tools.trailing_slash.on': True, 238. } 239. 240. namespaces = NamespaceSet( 241. **{"server": lambda k, v: setattr(cherrypy.server, k, v), 242. "engine": lambda k, v: setattr(cherrypy.engine, k, v), 243. "log": lambda k, v: setattr(cherrypy.log, k, v), 244. "checker": lambda k, v: setattr(cherrypy.checker, k, v), 245. }) 246. 247. def __init__(self): 248. self.reset() 249. 250. def reset(self): 251. """Reset self to default values.""" 252. self.clear() 253. dict.update(self, self.defaults) 254. 255. def update(self, config): 256. """Update self from a dict, file or filename.""" 257. if isinstance(config, basestring): 258. # Filename 259. if config not in cherrypy.engine.reload_files: 260. cherrypy.engine.reload_files.append(config) 261. config = _Parser().dict_from_file(config) 262. elif hasattr(config, 'read'): 263. # Open file object 264. config = _Parser().dict_from_file(config) 265. else: 266. config = config.copy() 267. 268. if isinstance(config.get("global", None), dict): 269. if len(config) > 1: 270. cherrypy.checker.global_config_contained_paths = True 271. config = config["global"] 272. 273. if 'environment' in config: 274. env = environments[config['environment']] 275. for k in env: 276. if k not in config: 277. config[k] = env[k] 278. 279. if 'tools.staticdir.dir' in config: 280. config['tools.staticdir.section'] = "global" 281. 282. dict.update(self, config) 283. self.namespaces(config) 284. 285. def __setitem__(self, k, v): 286. dict.__setitem__(self, k, v) 287. self.namespaces({k: v}) 288. 289. 290. 291. class _Parser(ConfigParser.ConfigParser): 292. """Sub-class of ConfigParser that keeps the case of options and that raises 293. an exception if the file cannot be read. 294. """ 295. 296. def optionxform(self, optionstr): 297. return optionstr 298. 299. def read(self, filenames): 300. if isinstance(filenames, basestring): 301. filenames = [filenames] 302. for filename in filenames: 303. # try: 304. # fp = open(filename) 305. # except IOError: 306. # continue 307. fp = open(filename) 308. try: 309. self._read(fp, filename) 310. finally: 311. fp.close() 312. 313. def as_dict(self, raw=False, vars=None): 314. """Convert an INI file to a dictionary""" 315. # Load INI file into a dict 316. from cherrypy.lib import unrepr 317. result = {} 318. for section in self.sections(): 319. if section not in result: 320. result[section] = {} 321. for option in self.options(section): 322. value = self.get(section, option, raw, vars) 323. try: 324. value = unrepr(value) 325. except Exception, x: 326. msg = ("Config error in section: %s, option: %s, value: %s" % 327. (repr(section), repr(option), repr(value))) 328. raise ValueError(msg, x.__class__.__name__, x.args) 329. result[section][option] = value 330. return result 331. 332. def dict_from_file(self, file): 333. if hasattr(file, 'read'): 334. self.readfp(file) 335. else: 336. self.read(file) 337. return self.as_dict() 338. 339. del ConfigParser