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

Changeset 1183

Show
Ignore:
Timestamp:
07/02/06 00:49:30
Author:
fumanchu
Message:

Cleanup of cherrypy and cherrypy.config namespaces. Changed 'log_config_options' to 'log_config'. Moved cherrypy.set_config to cherrypy.config.wrap.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/__init__.py

    r1164 r1183  
    33__version__ = '3.0.0alpha' 
    44 
    5 import logging 
    6  
    7 from _cperror import HTTPError, HTTPRedirect, InternalRedirect, NotFound, WrongConfigValue 
     5import logging as _logging 
     6 
     7from _cperror import (HTTPError, HTTPRedirect, InternalRedirect, 
     8                      NotFound, WrongConfigValue) 
    89import config 
    910 
     
    107108                } 
    108109    try: 
    109         request.app.access_log.log(logging.INFO, s) 
     110        request.app.access_log.log(_logging.INFO, s) 
    110111    except: 
    111112        log(traceback=True) 
    112113 
    113114 
    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 
     118def _log_message(msg, context = '', severity = _logging.DEBUG): 
    118119    """Default method for logging messages (error log). 
    119120     
     
    128129    log.log(severity, ' '.join((logtime(), context, msg))) 
    129130 
    130 def log(msg='', context='', severity=logging.DEBUG, traceback=False): 
     131def log(msg='', context='', severity=_logging.DEBUG, traceback=False): 
    131132    """Syntactic sugar for writing to the (error) log. 
    132133     
     
    217218        return expose_ 
    218219 
    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 f 
    226     return wrapper 
    227  
  • trunk/cherrypy/_cpengine.py

    r1135 r1183  
    4141         
    4242        # 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() 
    4545         
    4646        # Autoreload. Note that, if we're not starting our own HTTP server, 
  • trunk/cherrypy/config.py

    r1156 r1183  
    22 
    33import ConfigParser 
    4 import logging 
    5 _logfmt = logging.Formatter("%(message)s") 
    6 import os 
    7 import sys 
     4import logging as _logging 
     5_logfmt = _logging.Formatter("%(message)s") 
     6import os as _os 
    87 
    98import cherrypy 
    10 from cherrypy.lib import autoreload, unrepr 
    119 
    1210environments = { 
     
    3937    """Merge one app config (from a dict, file, or filename) into another.""" 
    4038    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) 
    4442    elif hasattr(other, 'read'): 
    45         other = dict_from_config_file(other) 
     43        other = Parser().dict_from_file(other) 
    4644     
    4745    # Load other into base 
     
    5856    'server.thread_pool': 10, 
    5957    '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__), 
    6159                             "error.log"), 
    6260    'tools.log_tracebacks.on': True, 
     
    7371    """Update globalconf from a dict, file or filename.""" 
    7472    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) 
    7876    elif hasattr(conf, 'read'): 
    79         conf = dict_from_config_file(conf) 
     77        conf = Parser().dict_from_file(conf) 
     78     
    8079    if isinstance(conf.get("global", None), dict): 
    8180        conf = conf["global"] 
     
    8584 
    8685def _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) 
    8989    h.setFormatter(_logfmt) 
    9090    h._cpbuiltin = "screen" 
     
    9292 
    9393def _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) 
    9696    h.setFormatter(_logfmt) 
    9797    h._cpbuiltin = "file" 
     
    115115    if fname: 
    116116        if h: 
    117             if h.baseFilename != os.path.abspath(fname): 
     117            if h.baseFilename != _os.path.abspath(fname): 
    118118                h.close() 
    119119                log.handlers.remove(h) 
     
    147147 
    148148 
    149 class CaseSensitiveConfigParser(ConfigParser.ConfigParser): 
     149def 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 
     159class Parser(ConfigParser.ConfigParser): 
    150160    """Sub-class of ConfigParser that keeps the case of options and that raises 
    151161    an exception if the file cannot be read. 
     
    168178            finally: 
    169179                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 file 
    175     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 dict 
    182     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 e 
    196             result[section][option] = value 
    197     return result 
    198  
    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 
     210def log_config(): 
    201211    """Log engine configuration parameters.""" 
    202212    cherrypy.log("Server parameters:", 'CONFIG') 
     
    218228        cherrypy.log("  %s: %s" % (var, get(var)), 'CONFIG') 
    219229 
     230 
     231del ConfigParser 
  • trunk/cherrypy/test/test_core.py

    r1175 r1183  
    218218         
    219219        # 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) 
    221221        def page_streamed(self): 
    222222            yield "word up" 
    223223            raise ValueError() 
    224224            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) 
    226226        assert(page_streamed._cp_config == {'stream_response': True}) 
    227227         
  • trunk/cherrypy/test/test_states.py

    r1125 r1183  
    200200            'server.thread_pool': 10, 
    201201            'log_to_screen': False, 
    202             'log_config_options': False, 
     202            'log_config': False, 
    203203            'environment': "production", 
    204204            'show_tracebacks': True, 
     
    217217                'server.thread_pool': 10, 
    218218                'log_to_screen': False, 
    219                 'log_config_options': False, 
     219                'log_config': False, 
    220220                'environment': "production", 
    221221                'show_tracebacks': True, 

Hosted by WebFaction

Log in as guest/cpguest to create tickets