Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/598-sendall/cherrypy/_cpconfig.py

Revision 1861 (checked in by fumanchu, 11 months ago)

Spurious import.

  • Property svn:eol-style set to native
Line 
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 app.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
97 import cherrypy
98
99
100 environments = {
101     "staging": {
102         'engine.autoreload_on': False,
103         'checker.on': False,
104         'tools.log_headers.on': False,
105         'request.show_tracebacks': False,
106         },
107     "production": {
108         'engine.autoreload_on': False,
109         'checker.on': False,
110         'tools.log_headers.on': False,
111         'request.show_tracebacks': False,
112         'log.screen': False,
113         },
114     "embedded": {
115         # For use with CherryPy embedded in another deployment stack.
116         'engine.autoreload_on': False,
117         'checker.on': False,
118         'tools.log_headers.on': False,
119         'request.show_tracebacks': False,
120         'log.screen': False,
121         'engine.SIGHUP': None,
122         'engine.SIGTERM': None,
123         },
124     "test_suite": {
125         'engine.autoreload_on': False,
126         'checker.on': False,
127         'tools.log_headers.on': False,
128         'request.show_tracebacks': True,
129         'log.screen': False,
130         },
131     }
132
133 def as_dict(config):
134     """Return a dict from 'config' whether it is a dict, file, or filename."""
135     if isinstance(config, basestring):
136         config = _Parser().dict_from_file(config)
137     elif hasattr(config, 'read'):
138         config = _Parser().dict_from_file(config)
139     return config
140
141 def merge(base, other):
142     """Merge one app config (from a dict, file, or filename) into another.
143     
144     If the given config is a filename, it will be appended to
145     the list of files to monitor for "autoreload" changes.
146     """
147     if isinstance(other, basestring):
148         cherrypy.engine.autoreload.files.add(other)
149    
150     # Load other into base
151     for section, value_map in as_dict(other).iteritems():
152         base.setdefault(section, {}).update(value_map)
153
154
155 class NamespaceSet(dict):
156     """A dict of config namespace names and handlers.
157     
158     Each config entry should begin with a namespace name; the corresponding
159     namespace handler will be called once for each config entry in that
160     namespace, and will be passed two arguments: the config key (with the
161     namespace removed) and the config value.
162     
163     Namespace handlers may be any Python callable; they may also be
164     Python 2.5-style 'context managers', in which case their __enter__
165     method should return a callable to be used as the handler.
166     See cherrypy.tools (the Toolbox class) for an example.
167     """
168    
169     def __call__(self, config):
170         """Iterate through config and pass it to each namespace handler.
171         
172         'config' should be a flat dict, where keys use dots to separate
173         namespaces, and values are arbitrary.
174         
175         The first name in each config key is used to look up the corresponding
176         namespace handler. For example, a config entry of {'tools.gzip.on': v}
177         will call the 'tools' namespace handler with the args: ('gzip.on', v)
178         """
179         # Separate the given config into namespaces
180         ns_confs = {}
181         for k in config:
182             if "." in k:
183                 ns, name = k.split(".", 1)
184                 bucket = ns_confs.setdefault(ns, {})
185                 bucket[name] = config[k]
186        
187         # I chose __enter__ and __exit__ so someday this could be
188         # rewritten using Python 2.5's 'with' statement:
189         # for ns, handler in self.iteritems():
190         #     with handler as callable:
191         #         for k, v in ns_confs.get(ns, {}).iteritems():
192         #             callable(k, v)
193         for ns, handler in self.iteritems():
194             exit = getattr(handler, "__exit__", None)
195             if exit:
196                 callable = handler.__enter__()
197                 no_exc = True
198                 try:
199                     try:
200                         for k, v in ns_confs.get(ns, {}).iteritems():
201                             callable(k, v)
202                     except:
203                         # The exceptional case is handled here
204                         no_exc = False
205                         if exit is None:
206                             raise
207                         if not exit(*sys.exc_info()):
208                             raise
209                         # The exception is swallowed if exit() returns true
210                 finally:
211                     # The normal and non-local-goto cases are handled here
212                     if no_exc and exit:
213                         exit(None, None, None)
214             else:
215                 for k, v in ns_confs.get(ns, {}).iteritems():
216                     handler(k, v)
217    
218     def __repr__(self):
219         return "%s.%s(%s)" % (self.__module__, self.__class__.__name__,
220                               dict.__repr__(self))
221    
222     def __copy__(self):
223         newobj = self.__class__()
224         newobj.update(self)
225         return newobj
226     copy = __copy__
227
228
229 class Config(dict):
230     """The 'global' configuration data for the entire CherryPy process."""
231    
232     defaults = {
233         'tools.log_tracebacks.on': True,
234         'tools.log_headers.on': True,
235         'tools.trailing_slash.on': True,
236         }
237    
238     namespaces = NamespaceSet(
239         **{"server": lambda k, v: setattr(cherrypy.server, k, v),
240            "log": lambda k, v: setattr(cherrypy.log, k, v),
241            "checker": lambda k, v: setattr(cherrypy.checker, k, v),
242            })
243    
244     def __init__(self):
245         self.reset()
246    
247     def reset(self):
248         """Reset self to default values."""
249         self.clear()
250         dict.update(self, self.defaults)
251    
252     def update(self, config):
253         """Update self from a dict, file or filename."""
254         if isinstance(config, basestring):
255             # Filename
256             cherrypy.engine.autoreload.files.add(config)
257             config = _Parser().dict_from_file(config)
258         elif hasattr(config, 'read'):
259             # Open file object
260             config = _Parser().dict_from_file(config)
261         else:
262             config = config.copy()
263        
264         if isinstance(config.get("global", None), dict):
265             if len(config) > 1:
266                 cherrypy.checker.global_config_contained_paths = True
267             config = config["global"]
268        
269         which_env = config.get('environment')
270         if which_env:
271             env = environments[which_env]
272             for k in env:
273                 if k not in config:
274                     config[k] = env[k]
275        
276         if 'tools.staticdir.dir' in config:
277             config['tools.staticdir.section'] = "global"
278        
279         dict.update(self, config)
280         self.namespaces(config)
281    
282     def __setitem__(self, k, v):
283         dict.__setitem__(self, k, v)
284         self.namespaces({k: v})
285
286
287 # Backward compatibility handler for the "engine" namespace.
288 def _engine_namespace_handler(k, v):
289     engine = cherrypy.engine
290     if k == 'autoreload_on':
291         if v:
292             engine.autoreload.subscribe()
293         else:
294             engine.autoreload.unsubscribe()
295     elif k == 'autoreload_frequency':
296         engine.autoreload.frequency = v
297     elif k == 'autoreload_match':
298         engine.autoreload.match = v
299     elif k == 'reload_files':
300         engine.autoreload.files = v
301     elif k == 'deadlock_poll_freq':
302         cherrypy.timeout_monitor.frequency = v
303     elif k == 'SIGHUP':
304         engine.listeners['SIGHUP'] = set([v])
305     elif k == 'SIGTERM':
306         engine.listeners['SIGTERM'] = set([v])
307 Config.namespaces["engine"] = _engine_namespace_handler
308
309
310 class _Parser(ConfigParser.ConfigParser):
311     """Sub-class of ConfigParser that keeps the case of options and that raises
312     an exception if the file cannot be read.
313     """
314    
315     def optionxform(self, optionstr):
316         return optionstr
317    
318     def read(self, filenames):
319         if isinstance(filenames, basestring):
320             filenames = [filenames]
321         for filename in filenames:
322             # try:
323             #     fp = open(filename)
324             # except IOError:
325             #     continue
326             fp = open(filename)
327             try:
328                 self._read(fp, filename)
329             finally:
330                 fp.close()
331    
332     def as_dict(self, raw=False, vars=None):
333         """Convert an INI file to a dictionary"""
334         # Load INI file into a dict
335         from cherrypy.lib import unrepr
336         result = {}
337         for section in self.sections():
338             if section not in result:
339                 result[section] = {}
340             for option in self.options(section):
341                 value = self.get(section, option, raw, vars)
342                 try:
343                     value = unrepr(value)
344                 except Exception, x:
345                     msg = ("Config error in section: %r, option: %r, "
346                            "value: %r. Config values must be valid Python." %
347                            (section, option, value))
348                     raise ValueError(msg, x.__class__.__name__, x.args)
349                 result[section][option] = value
350         return result
351    
352     def dict_from_file(self, file):
353         if hasattr(file, 'read'):
354             self.readfp(file)
355         else:
356             self.read(file)
357         return self.as_dict()
358
359 del ConfigParser
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets