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

Changeset 521

Show
Ignore:
Timestamp:
08/08/05 13:35:03
Author:
fumanchu
Message:

1. Partial fix for ticket #242 (ability to specify attributes in a config file).
2. Moved modules, attributes functions from server.py to _cputil.py.
3. A couple of newline fixes for unrelated modules.

Files:

Legend:

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

    r517 r521  
    250250        sys.path.remove(path)    
    251251 
     252 
    252253# public domain "unrepr" implementation, found on the web and then improved. 
    253254import compiler 
     
    258259    return p.getChildren()[1].getChildren()[0].getChildren()[1] 
    259260 
     261 
    260262class UnknownType(Exception): 
    261     pass 
    262     # initilize the built in filters  
     263     
     264    # initialize the built-in filters  
    263265    for n in xrange(len(_cpDefaultInputFilterList)): 
    264266        try: 
     
    272274        except: 
    273275            pass 
    274   
     276 
     277 
    275278class Builder: 
    276  
     279     
    277280    def build(self, o): 
    278         m = getattr(self, 'build_'+o.__class__.__name__, None) 
     281        m = getattr(self, 'build_' + o.__class__.__name__, None) 
    279282        if m is None: 
    280283            raise UnknownType(o.__class__.__name__) 
    281284        return m(o) 
    282  
     285     
    283286    def build_List(self, o): 
    284287        return map(self.build, o.getChildren()) 
    285  
     288     
    286289    def build_Const(self, o): 
    287290        return o.value 
    288  
     291     
    289292    def build_Dict(self, o): 
    290293        d = {} 
     
    293296            d[el] = i.next() 
    294297        return d 
    295  
     298     
    296299    def build_Tuple(self, o): 
    297300        return tuple(self.build_List(o)) 
    298  
     301     
    299302    def build_Name(self, o): 
    300303        if o.name == 'None': 
    301304            return None 
    302         elif o.name == 'True': 
     305        if o.name == 'True': 
    303306            return True 
    304         elif o.name == 'False': 
     307        if o.name == 'False': 
    305308            return False 
     309         
     310        # See if the Name is a package or module 
     311        try: 
     312            return modules(o.name) 
     313        except ImportError: 
     314            pass 
     315         
    306316        raise UnknownType(o.name) 
    307  
     317     
    308318    def build_Add(self, o): 
    309319        real, imag = map(self.build_Const, o.getChildren()) 
     
    315325            raise UnknownType('Add') 
    316326        return real+imag 
     327     
     328    def build_Getattr(self, o): 
     329        parent = self.build(o.expr) 
     330        return getattr(parent, o.attrname) 
     331 
    317332 
    318333def unrepr(s): 
     
    322337        return Builder().build(getObj(s)) 
    323338    except: 
    324         raise cherrypy.WrongUnreprValue, repr(s) 
     339        raise #cherrypy.WrongUnreprValue, repr(s) 
     340 
     341def modules(modulePath): 
     342    """Load a module and retrieve a reference to that module.""" 
     343    try: 
     344        mod = sys.modules[modulePath] 
     345        if mod is None: 
     346            raise KeyError 
     347    except KeyError: 
     348        # The last [''] is important. 
     349        mod = __import__(modulePath, globals(), locals(), ['']) 
     350    return mod 
     351 
     352def attributes(fullAttributeName): 
     353    """Load a module and retrieve an attribute of that module.""" 
     354     
     355    # Parse out the path, module, and attribute 
     356    lastDot = fullAttributeName.rfind(u".") 
     357    attrName = fullAttributeName[lastDot + 1:] 
     358    modPath = fullAttributeName[:lastDot] 
     359     
     360    aMod = modules(modPath) 
     361    # Let an AttributeError propagate outward. 
     362    try: 
     363        attr = getattr(aMod, attrName) 
     364    except AttributeError: 
     365        raise AttributeError("'%s' object has no attribute '%s'" 
     366                             % (modPath, attrName)) 
     367     
     368    # Return a reference to the attribute. 
     369    return attr 
  • trunk/cherrypy/server.py

    r517 r521  
    143143        serverClass = cherrypy.config.get("server.class", None) 
    144144    if serverClass and isinstance(serverClass, basestring): 
    145         serverClass = attributes(serverClass) 
     145        serverClass = cherrypy._cputil.attributes(serverClass) 
    146146    if serverClass is None: 
    147147        import _cpwsgi 
     
    172172        stop() 
    173173 
    174 def modules(modulePath): 
    175     """Load a module and retrieve a reference to that module.""" 
    176     try: 
    177         aMod = sys.modules[modulePath] 
    178         if aMod is None: 
    179             raise KeyError 
    180     except KeyError: 
    181         # The last [''] is important. 
    182         aMod = __import__(modulePath, globals(), locals(), ['']) 
    183     return aMod 
    184  
    185 def attributes(fullAttributeName): 
    186     """Load a module and retrieve an attribute of that module.""" 
    187      
    188     # Parse out the path, module, and attribute 
    189     lastDot = fullAttributeName.rfind(u".") 
    190     attrName = fullAttributeName[lastDot + 1:] 
    191     modPath = fullAttributeName[:lastDot] 
    192      
    193     aMod = modules(modPath) 
    194     # Let an AttributeError propagate outward. 
    195     try: 
    196         anAttr = getattr(aMod, attrName) 
    197     except AttributeError: 
    198         raise AttributeError("'%s' object has no attribute '%s'" 
    199                              % (modPath, attrName)) 
    200      
    201     # Return a reference to the attribute. 
    202     return anAttr 
    203  
    204174 
    205175seen_threads = {} 

Hosted by WebFaction

Log in as guest/cpguest to create tickets