| 1 |
"""Configuration system for CherryPy.""" |
|---|
| 2 |
|
|---|
| 3 |
import ConfigParser |
|---|
| 4 |
import logging as _logging |
|---|
| 5 |
_logfmt = _logging.Formatter("%(message)s") |
|---|
| 6 |
import os as _os |
|---|
| 7 |
|
|---|
| 8 |
import cherrypy |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
environments = { |
|---|
| 12 |
"development": { |
|---|
| 13 |
'autoreload.on': True, |
|---|
| 14 |
'log_file_not_found': True, |
|---|
| 15 |
'show_tracebacks': True, |
|---|
| 16 |
'log_request_headers': True, |
|---|
| 17 |
}, |
|---|
| 18 |
"staging": { |
|---|
| 19 |
'autoreload.on': False, |
|---|
| 20 |
'log_file_not_found': False, |
|---|
| 21 |
'show_tracebacks': False, |
|---|
| 22 |
'log_request_headers': False, |
|---|
| 23 |
}, |
|---|
| 24 |
"production": { |
|---|
| 25 |
'autoreload.on': False, |
|---|
| 26 |
'log_file_not_found': False, |
|---|
| 27 |
'show_tracebacks': False, |
|---|
| 28 |
'log_request_headers': False, |
|---|
| 29 |
}, |
|---|
| 30 |
"embedded": { |
|---|
| 31 |
'autoreload.on': False, |
|---|
| 32 |
'log_to_screen': False, |
|---|
| 33 |
'server.class': None, |
|---|
| 34 |
}, |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
def merge(base, other): |
|---|
| 38 |
"""Merge one app config (from a dict, file, or filename) into another.""" |
|---|
| 39 |
if isinstance(other, basestring): |
|---|
| 40 |
if other not in cherrypy.engine.reload_files: |
|---|
| 41 |
cherrypy.engine.reload_files.append(other) |
|---|
| 42 |
other = Parser().dict_from_file(other) |
|---|
| 43 |
elif hasattr(other, 'read'): |
|---|
| 44 |
other = Parser().dict_from_file(other) |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
for section, value_map in other.iteritems(): |
|---|
| 48 |
|
|---|
| 49 |
if 'environment' in value_map: |
|---|
| 50 |
env = environments[value_map['environment']] |
|---|
| 51 |
for k in env: |
|---|
| 52 |
if k not in value_map: |
|---|
| 53 |
value_map[k] = env[k] |
|---|
| 54 |
del value_map['environment'] |
|---|
| 55 |
|
|---|
| 56 |
base.setdefault(section, {}).update(value_map) |
|---|
| 57 |
|
|---|
| 58 |
default_conf = { |
|---|
| 59 |
'server.socket_port': 8080, |
|---|
| 60 |
'server.socket_host': '', |
|---|
| 61 |
'server.socket_file': '', |
|---|
| 62 |
'server.socket_queue_size': 5, |
|---|
| 63 |
'server.socket_timeout': 10, |
|---|
| 64 |
'server.protocol_version': 'HTTP/1.0', |
|---|
| 65 |
'server.reverse_dns': False, |
|---|
| 66 |
'server.thread_pool': 10, |
|---|
| 67 |
'log_to_screen': True, |
|---|
| 68 |
'log_file': _os.path.join(_os.getcwd(), _os.path.dirname(__file__), |
|---|
| 69 |
"error.log"), |
|---|
| 70 |
'tools.log_tracebacks.on': True, |
|---|
| 71 |
'environment': "development", |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
globalconf = default_conf.copy() |
|---|
| 75 |
|
|---|
| 76 |
def reset(): |
|---|
| 77 |
globalconf.clear() |
|---|
| 78 |
update(default_conf) |
|---|
| 79 |
|
|---|
| 80 |
def update(conf): |
|---|
| 81 |
"""Update globalconf from a dict, file or filename.""" |
|---|
| 82 |
if isinstance(conf, basestring): |
|---|
| 83 |
if conf not in cherrypy.engine.reload_files: |
|---|
| 84 |
cherrypy.engine.reload_files.append(conf) |
|---|
| 85 |
conf = Parser().dict_from_file(conf) |
|---|
| 86 |
elif hasattr(conf, 'read'): |
|---|
| 87 |
conf = Parser().dict_from_file(conf) |
|---|
| 88 |
|
|---|
| 89 |
if isinstance(conf.get("global", None), dict): |
|---|
| 90 |
conf = conf["global"] |
|---|
| 91 |
|
|---|
| 92 |
if 'environment' in conf: |
|---|
| 93 |
env = environments[conf['environment']] |
|---|
| 94 |
for k in env: |
|---|
| 95 |
if k not in conf: |
|---|
| 96 |
conf[k] = env[k] |
|---|
| 97 |
|
|---|
| 98 |
if 'tools.staticdir.dir' in conf: |
|---|
| 99 |
conf['tools.staticdir.section'] = "global" |
|---|
| 100 |
|
|---|
| 101 |
globalconf.update(conf) |
|---|
| 102 |
|
|---|
| 103 |
_configure_builtin_logging(globalconf, cherrypy._error_log) |
|---|
| 104 |
_configure_builtin_logging(globalconf, cherrypy._access_log, "log_access_file") |
|---|
| 105 |
|
|---|
| 106 |
def _add_builtin_screen_handler(log): |
|---|
| 107 |
import sys |
|---|
| 108 |
h = _logging.StreamHandler(sys.stdout) |
|---|
| 109 |
h.setLevel(_logging.DEBUG) |
|---|
| 110 |
h.setFormatter(_logfmt) |
|---|
| 111 |
h._cpbuiltin = "screen" |
|---|
| 112 |
log.addHandler(h) |
|---|
| 113 |
|
|---|
| 114 |
def _add_builtin_file_handler(log, fname): |
|---|
| 115 |
h = _logging.FileHandler(fname) |
|---|
| 116 |
h.setLevel(_logging.DEBUG) |
|---|
| 117 |
h.setFormatter(_logfmt) |
|---|
| 118 |
h._cpbuiltin = "file" |
|---|
| 119 |
log.addHandler(h) |
|---|
| 120 |
|
|---|
| 121 |
def _configure_builtin_logging(conf, log, filekey="log_file"): |
|---|
| 122 |
"""Create/destroy builtin log handlers as needed from conf.""" |
|---|
| 123 |
|
|---|
| 124 |
existing = dict([(getattr(x, "_cpbuiltin", None), x) |
|---|
| 125 |
for x in log.handlers]) |
|---|
| 126 |
h = existing.get("screen") |
|---|
| 127 |
screen = conf.get('log_to_screen') |
|---|
| 128 |
if screen: |
|---|
| 129 |
if not h: |
|---|
| 130 |
_add_builtin_screen_handler(log) |
|---|
| 131 |
elif h: |
|---|
| 132 |
log.handlers.remove(h) |
|---|
| 133 |
|
|---|
| 134 |
h = existing.get("file") |
|---|
| 135 |
fname = conf.get(filekey) |
|---|
| 136 |
if fname: |
|---|
| 137 |
if h: |
|---|
| 138 |
if h.baseFilename != _os.path.abspath(fname): |
|---|
| 139 |
h.close() |
|---|
| 140 |
log.handlers.remove(h) |
|---|
| 141 |
_add_builtin_file_handler(log, fname) |
|---|
| 142 |
else: |
|---|
| 143 |
_add_builtin_file_handler(log, fname) |
|---|
| 144 |
else: |
|---|
| 145 |
if h: |
|---|
| 146 |
h.close() |
|---|
| 147 |
log.handlers.remove(h) |
|---|
| 148 |
|
|---|
| 149 |
def get(key, default=None): |
|---|
| 150 |
"""Return the config value corresponding to key, or default.""" |
|---|
| 151 |
try: |
|---|
| 152 |
conf = cherrypy.request.config |
|---|
| 153 |
if conf is None: |
|---|
| 154 |
conf = globalconf |
|---|
| 155 |
except AttributeError: |
|---|
| 156 |
|
|---|
| 157 |
conf = globalconf |
|---|
| 158 |
|
|---|
| 159 |
return conf.get(key, default) |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
def wrap(**kwargs): |
|---|
| 163 |
"""Decorator to set _cp_config on a handler using the given kwargs.""" |
|---|
| 164 |
def wrapper(f): |
|---|
| 165 |
if not hasattr(f, "_cp_config"): |
|---|
| 166 |
f._cp_config = {} |
|---|
| 167 |
f._cp_config.update(kwargs) |
|---|
| 168 |
return f |
|---|
| 169 |
return wrapper |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
class Parser(ConfigParser.ConfigParser): |
|---|
| 173 |
"""Sub-class of ConfigParser that keeps the case of options and that raises |
|---|
| 174 |
an exception if the file cannot be read. |
|---|
| 175 |
""" |
|---|
| 176 |
|
|---|
| 177 |
def optionxform(self, optionstr): |
|---|
| 178 |
return optionstr |
|---|
| 179 |
|
|---|
| 180 |
def read(self, filenames): |
|---|
| 181 |
if isinstance(filenames, basestring): |
|---|
| 182 |
filenames = [filenames] |
|---|
| 183 |
for filename in filenames: |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
fp = open(filename) |
|---|
| 189 |
try: |
|---|
| 190 |
self._read(fp, filename) |
|---|
| 191 |
finally: |
|---|
| 192 |
fp.close() |
|---|
| 193 |
|
|---|
| 194 |
def as_dict(self, raw=False, vars=None): |
|---|
| 195 |
"""Convert an INI file to a dictionary""" |
|---|
| 196 |
|
|---|
| 197 |
from cherrypy.lib import unrepr |
|---|
| 198 |
result = {} |
|---|
| 199 |
for section in self.sections(): |
|---|
| 200 |
if section not in result: |
|---|
| 201 |
result[section] = {} |
|---|
| 202 |
for option in self.options(section): |
|---|
| 203 |
value = self.get(section, option, raw, vars) |
|---|
| 204 |
try: |
|---|
| 205 |
value = unrepr(value) |
|---|
| 206 |
except Exception, x: |
|---|
| 207 |
msg = ("section: %s, option: %s, value: %s" % |
|---|
| 208 |
(repr(section), repr(option), repr(value))) |
|---|
| 209 |
e = cherrypy.WrongConfigValue(msg) |
|---|
| 210 |
e.args += (x.__class__.__name__, x.args) |
|---|
| 211 |
raise e |
|---|
| 212 |
result[section][option] = value |
|---|
| 213 |
return result |
|---|
| 214 |
|
|---|
| 215 |
def dict_from_file(self, file): |
|---|
| 216 |
if hasattr(file, 'read'): |
|---|
| 217 |
self.readfp(file) |
|---|
| 218 |
else: |
|---|
| 219 |
self.read(file) |
|---|
| 220 |
return self.as_dict() |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
def log_config(): |
|---|
| 224 |
"""Log engine configuration parameters.""" |
|---|
| 225 |
cherrypy.log("Server parameters:", 'CONFIG') |
|---|
| 226 |
|
|---|
| 227 |
server_vars = [ |
|---|
| 228 |
'environment', |
|---|
| 229 |
'log_to_screen', |
|---|
| 230 |
'log_file', |
|---|
| 231 |
'server.protocol_version', |
|---|
| 232 |
'server.socket_host', |
|---|
| 233 |
'server.socket_port', |
|---|
| 234 |
'server.socket_file', |
|---|
| 235 |
'server.reverse_dns', |
|---|
| 236 |
'server.socket_queue_size', |
|---|
| 237 |
'server.thread_pool', |
|---|
| 238 |
] |
|---|
| 239 |
|
|---|
| 240 |
for var in server_vars: |
|---|
| 241 |
cherrypy.log(" %s: %s" % (var, get(var)), 'CONFIG') |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
del ConfigParser |
|---|