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

root/tags/cherrypy-3.1.0beta2/cherrypy/_cpconfig.py

Revision 1794 (checked in by fumanchu, 1 year ago)

More-meaningful config syntax errors.

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

Hosted by WebFaction

Log in as guest/cpguest to create tickets