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

Changeset 338

Show
Ignore:
Timestamp:
06/17/05 11:50:52
Author:
fumanchu
Message:

Fix for ticket #191. _cpdefaults code cleaned up and moved into _cputil.

Files:

Legend:

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

    r337 r338  
    3434import mimetypes, Cookie, urlparse 
    3535from lib.filter import basefilter 
    36 import cpg, _cputil, cperror, _cpdefaults, _cpcgifs 
     36import cpg, _cputil, cperror, _cpcgifs 
    3737 
    3838# Can't use cStringIO; doesn't support unicode strings   
     
    395395def applyFilters(methodName): 
    396396    if methodName in ('beforeRequestBody', 'beforeMain'): 
    397         filterList = (_cpdefaults._cpDefaultInputFilterList + 
     397        filterList = (_cputil._cpDefaultInputFilterList + 
    398398                      _cputil.getSpecialAttribute('_cpFilterList')) 
    399399    elif methodName in ('beforeFinalize',): 
    400400        filterList = (_cputil.getSpecialAttribute('_cpFilterList') + 
    401                       _cpdefaults._cpDefaultOutputFilterList) 
     401                      _cputil._cpDefaultOutputFilterList) 
    402402    else: 
    403403        # 'onStartResource', 'onEndResource' 
    404404        # 'beforeErrorResponse', 'afterErrorResponse' 
    405         filterList = (_cpdefaults._cpDefaultInputFilterList + 
     405        filterList = (_cputil._cpDefaultInputFilterList + 
    406406                      _cputil.getSpecialAttribute('_cpFilterList') + 
    407                       _cpdefaults._cpDefaultOutputFilterList) 
     407                      _cputil._cpDefaultOutputFilterList) 
    408408    for filter in filterList: 
    409409        method = getattr(filter, methodName, None) 
  • trunk/cherrypy/_cputil.py

    r337 r338  
    3232""" 
    3333 
    34 import time, thread, cpg, _cpdefaults, cperror 
     34import time, cpg, cperror 
     35 
    3536 
    3637class EmptyClass: 
     
    3839    pass 
    3940 
     41 
    4042def getSpecialAttribute(name): 
    4143    """ Return the special function """ 
    42  
     44     
    4345    # First, we look in the right-most object if this special function is implemented. 
    4446    # If not, then we try the previous object and so on until we reach cpg.root 
    45     # If it's still not there, we use the implementation from the 
    46     # "_cpdefaults.py" module 
    47      
    48  
    49     moduleList = [_cpdefaults] 
     47    # If it's still not there, we use the implementation from this module. 
     48     
    5049    root = getattr(cpg, 'root', None) 
    5150    if root: 
    52         moduleList.append(root) 
     51        moduleList = [root] 
    5352        # Try object path 
    5453        try: 
    5554            path = cpg.request.objectPath or cpg.request.path 
    56         except
     55        except AttributeError
    5756            path = '/' 
    5857        if path: 
    5958            pathList = path.split('/')[1:] 
    60  
    61             obj = cpg.root 
    62             previousObj = None 
     59             
    6360            # Successively get objects from the path 
    6461            for newObj in pathList: 
    65                 previousObj = obj 
    6662                try: 
    67                     obj = getattr(obj, newObj) 
    68                     moduleList.append(obj
     63                    root = getattr(root, newObj) 
     64                    moduleList.append(root
    6965                except AttributeError: 
    7066                    break 
    71  
    72     moduleList.reverse() 
    73     for module in moduleList: 
    74         func = getattr(module, name, None) 
    75         if func != None: 
    76             return func 
    77  
    78     raise cperror.InternalError, "Special function %s could not be found" % repr(name) 
    79  
     67         
     68        moduleList.reverse() 
     69        for module in moduleList: 
     70            func = getattr(module, name, None) 
     71            if func != None: 
     72                return func 
     73     
     74    try: 
     75        return globals()[name] 
     76    except KeyError: 
     77        raise cperror.InternalError("Special function %s could not be found" 
     78                                    % repr(name)) 
     79 
     80 
     81def _cpLogMessage(msg, context = '', severity = 0): 
     82    """ Default method for logging messages """ 
     83     
     84    nowTuple = time.localtime(time.time()) 
     85    nowStr = '%04d/%02d/%02d %02d:%02d:%02d' % (nowTuple[:6]) 
     86    if severity == 0: 
     87        level = "INFO" 
     88    elif severity == 1: 
     89        level = "WARNING" 
     90    elif severity == 2: 
     91        level = "ERROR" 
     92    else: 
     93        level = "UNKNOWN" 
     94    try: 
     95        logToScreen = cpg.config.get('server.logToScreen') 
     96    except: 
     97        logToScreen = True 
     98    s = nowStr + ' ' + context + ' ' + level + ' ' + msg 
     99    if logToScreen: 
     100        print s 
     101    if cpg.config.get('server.logFile'): 
     102        f = open(cpg.config.get('server.logFile'), 'ab') 
     103        f.write(s + '\n') 
     104        f.close() 
     105 
     106def _cpOnError(): 
     107    """ Default _cpOnError method """ 
     108    import sys, traceback 
     109    content = "".join(traceback.format_exception(*sys.exc_info())) 
     110    cpg.response.body = [content] 
     111    cpg.response.headerMap['Content-Type'] = 'text/plain' 
     112    if cpg.response.headerMap.has_key('Content-Encoding'): 
     113        del cpg.response.headerMap['Content-Encoding'] 
     114 
     115_cpFilterList = [] 
     116 
     117# Filters that are always included 
     118from cherrypy.lib.filter import baseurlfilter, cachefilter, \ 
     119    decodingfilter, encodingfilter, gzipfilter, logdebuginfofilter, \ 
     120    staticfilter, nsgmlsfilter, tidyfilter, \ 
     121    virtualhostfilter, xmlrpcfilter 
     122 
     123from cherrypy.lib.filter.sessionfilter import sessionfilter 
     124 
     125_cachefilter = cachefilter.CacheFilter() 
     126_logdebuginfofilter = logdebuginfofilter.LogDebugInfoFilter() 
     127_nsgmlsfilter = nsgmlsfilter.NsgmlsFilter() 
     128_sessionfilter = sessionfilter.SessionFilter() 
     129_tidyfilter = tidyfilter.TidyFilter() 
     130_xmlfilter = xmlrpcfilter.XmlRpcFilter() 
     131 
     132# These are in order for a reason! 
     133 
     134_cpDefaultInputFilterList = [ 
     135    _cachefilter, 
     136    _logdebuginfofilter, 
     137    virtualhostfilter.VirtualHostFilter(), 
     138    baseurlfilter.BaseUrlFilter(), 
     139    decodingfilter.DecodingFilter(), 
     140    _sessionfilter, 
     141    staticfilter.StaticFilter(), 
     142    _nsgmlsfilter, 
     143    _tidyfilter, 
     144    _xmlfilter, 
     145
     146_cpDefaultOutputFilterList = [ 
     147    _xmlfilter, 
     148    encodingfilter.EncodingFilter(), 
     149    _tidyfilter, 
     150    _nsgmlsfilter, 
     151    _logdebuginfofilter, 
     152    gzipfilter.GzipFilter(), 
     153    _sessionfilter, 
     154    _cachefilter, 
     155
    80156 
    81157# public domain "unrepr" implementation, found on the web and then improved. 

Hosted by WebFaction

Log in as guest/cpguest to create tickets