Changeset 1183
- Timestamp:
- 07/02/06 00:49:30
- Files:
-
- trunk/cherrypy/__init__.py (modified) (4 diffs)
- trunk/cherrypy/_cpengine.py (modified) (1 diff)
- trunk/cherrypy/config.py (modified) (10 diffs)
- trunk/cherrypy/test/test_core.py (modified) (1 diff)
- trunk/cherrypy/test/test_states.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r1164 r1183 3 3 __version__ = '3.0.0alpha' 4 4 5 import logging 6 7 from _cperror import HTTPError, HTTPRedirect, InternalRedirect, NotFound, WrongConfigValue 5 import logging as _logging 6 7 from _cperror import (HTTPError, HTTPRedirect, InternalRedirect, 8 NotFound, WrongConfigValue) 8 9 import config 9 10 … … 107 108 } 108 109 try: 109 request.app.access_log.log( logging.INFO, s)110 request.app.access_log.log(_logging.INFO, s) 110 111 except: 111 112 log(traceback=True) 112 113 113 114 114 _error_log = logging.getLogger("cherrypy.error")115 _error_log.setLevel( logging.DEBUG)116 117 def _log_message(msg, context = '', severity = logging.DEBUG):115 _error_log = _logging.getLogger("cherrypy.error") 116 _error_log.setLevel(_logging.DEBUG) 117 118 def _log_message(msg, context = '', severity = _logging.DEBUG): 118 119 """Default method for logging messages (error log). 119 120 … … 128 129 log.log(severity, ' '.join((logtime(), context, msg))) 129 130 130 def log(msg='', context='', severity= logging.DEBUG, traceback=False):131 def log(msg='', context='', severity=_logging.DEBUG, traceback=False): 131 132 """Syntactic sugar for writing to the (error) log. 132 133 … … 217 218 return expose_ 218 219 219 def set_config(**kwargs):220 """Decorator to set _cp_config using the given kwargs."""221 def wrapper(f):222 if not hasattr(f, "_cp_config"):223 f._cp_config = {}224 f._cp_config.update(kwargs)225 return f226 return wrapper227 trunk/cherrypy/_cpengine.py
r1135 r1183 41 41 42 42 # Output config options to log 43 if conf("log_config _options", True):44 cherrypy.config. output_config_map()43 if conf("log_config", True): 44 cherrypy.config.log_config() 45 45 46 46 # Autoreload. Note that, if we're not starting our own HTTP server, trunk/cherrypy/config.py
r1156 r1183 2 2 3 3 import ConfigParser 4 import logging 5 _logfmt = logging.Formatter("%(message)s") 6 import os 7 import sys 4 import logging as _logging 5 _logfmt = _logging.Formatter("%(message)s") 6 import os as _os 8 7 9 8 import cherrypy 10 from cherrypy.lib import autoreload, unrepr11 9 12 10 environments = { … … 39 37 """Merge one app config (from a dict, file, or filename) into another.""" 40 38 if isinstance(other, basestring): 41 if other not in autoreload.reloadFiles:42 autoreload.reloadFiles.append(other)43 other = dict_from_config_file(other)39 if other not in cherrypy.lib.autoreload.reloadFiles: 40 cherrypy.lib.autoreload.reloadFiles.append(other) 41 other = Parser().dict_from_file(other) 44 42 elif hasattr(other, 'read'): 45 other = dict_from_config_file(other)43 other = Parser().dict_from_file(other) 46 44 47 45 # Load other into base … … 58 56 'server.thread_pool': 10, 59 57 'log_to_screen': True, 60 'log_file': os.path.join(os.getcwd(),os.path.dirname(__file__),58 'log_file': _os.path.join(_os.getcwd(),_os.path.dirname(__file__), 61 59 "error.log"), 62 60 'tools.log_tracebacks.on': True, … … 73 71 """Update globalconf from a dict, file or filename.""" 74 72 if isinstance(conf, basestring): 75 if conf not in autoreload.reloadFiles:76 autoreload.reloadFiles.append(conf)77 conf = dict_from_config_file(conf)73 if conf not in cherrypy.lib.autoreload.reloadFiles: 74 cherrypy.lib.autoreload.reloadFiles.append(conf) 75 conf = Parser().dict_from_file(conf) 78 76 elif hasattr(conf, 'read'): 79 conf = dict_from_config_file(conf) 77 conf = Parser().dict_from_file(conf) 78 80 79 if isinstance(conf.get("global", None), dict): 81 80 conf = conf["global"] … … 85 84 86 85 def _add_builtin_screen_handler(log): 87 h = logging.StreamHandler(sys.stdout) 88 h.setLevel(logging.DEBUG) 86 import sys 87 h = _logging.StreamHandler(sys.stdout) 88 h.setLevel(_logging.DEBUG) 89 89 h.setFormatter(_logfmt) 90 90 h._cpbuiltin = "screen" … … 92 92 93 93 def _add_builtin_file_handler(log, fname): 94 h = logging.FileHandler(fname)95 h.setLevel( logging.DEBUG)94 h = _logging.FileHandler(fname) 95 h.setLevel(_logging.DEBUG) 96 96 h.setFormatter(_logfmt) 97 97 h._cpbuiltin = "file" … … 115 115 if fname: 116 116 if h: 117 if h.baseFilename != os.path.abspath(fname):117 if h.baseFilename != _os.path.abspath(fname): 118 118 h.close() 119 119 log.handlers.remove(h) … … 147 147 148 148 149 class CaseSensitiveConfigParser(ConfigParser.ConfigParser): 149 def wrap(**kwargs): 150 """Decorator to set _cp_config on a handler using the given kwargs.""" 151 def wrapper(f): 152 if not hasattr(f, "_cp_config"): 153 f._cp_config = {} 154 f._cp_config.update(kwargs) 155 return f 156 return wrapper 157 158 159 class Parser(ConfigParser.ConfigParser): 150 160 """Sub-class of ConfigParser that keeps the case of options and that raises 151 161 an exception if the file cannot be read. … … 168 178 finally: 169 179 fp.close() 170 171 def dict_from_config_file(config_file, raw=False, vars=None):172 """Convert an INI file to a dictionary"""173 174 # Parse config file175 configParser = CaseSensitiveConfigParser()176 if hasattr(config_file, 'read'):177 configParser.readfp(config_file)178 else:179 configParser.read(config_file)180 181 # Load INI file into a dict182 result = {}183 for section in configParser.sections():184 if section not in result:185 result[section] = {}186 for option in configParser.options(section):187 value = configParser.get(section, option, raw, vars)188 try:189 value = unrepr(value)190 except Exception, x:191 msg = ("section: %s, option: %s, value: %s" %192 (repr(section), repr(option), repr(value)))193 e = cherrypy.WrongConfigValue(msg)194 e.args += (x.__class__.__name__, x.args)195 raise e196 result[section][option] = value197 return result198 199 200 def output_config_map():180 181 def as_dict(self, raw=False, vars=None): 182 """Convert an INI file to a dictionary""" 183 # Load INI file into a dict 184 from cherrypy.lib import unrepr 185 result = {} 186 for section in self.sections(): 187 if section not in result: 188 result[section] = {} 189 for option in self.options(section): 190 value = self.get(section, option, raw, vars) 191 try: 192 value = unrepr(value) 193 except Exception, x: 194 msg = ("section: %s, option: %s, value: %s" % 195 (repr(section), repr(option), repr(value))) 196 e = cherrypy.WrongConfigValue(msg) 197 e.args += (x.__class__.__name__, x.args) 198 raise e 199 result[section][option] = value 200 return result 201 202 def dict_from_file(self, file): 203 if hasattr(file, 'read'): 204 self.readfp(file) 205 else: 206 self.read(file) 207 return self.as_dict() 208 209 210 def log_config(): 201 211 """Log engine configuration parameters.""" 202 212 cherrypy.log("Server parameters:", 'CONFIG') … … 218 228 cherrypy.log(" %s: %s" % (var, get(var)), 'CONFIG') 219 229 230 231 del ConfigParser trunk/cherrypy/test/test_core.py
r1175 r1183 218 218 219 219 # We support Python 2.3, but the @-deco syntax would look like this: 220 # @cherrypy. set_config(stream_response=True)220 # @cherrypy.config.wrap(stream_response=True) 221 221 def page_streamed(self): 222 222 yield "word up" 223 223 raise ValueError() 224 224 yield "very oops" 225 page_streamed = cherrypy. set_config(stream_response=True)(page_streamed)225 page_streamed = cherrypy.config.wrap(stream_response=True)(page_streamed) 226 226 assert(page_streamed._cp_config == {'stream_response': True}) 227 227 trunk/cherrypy/test/test_states.py
r1125 r1183 200 200 'server.thread_pool': 10, 201 201 'log_to_screen': False, 202 'log_config _options': False,202 'log_config': False, 203 203 'environment': "production", 204 204 'show_tracebacks': True, … … 217 217 'server.thread_pool': 10, 218 218 'log_to_screen': False, 219 'log_config _options': False,219 'log_config': False, 220 220 'environment': "production", 221 221 'show_tracebacks': True,

