Changeset 5287

Show
Ignore:
Timestamp:
08/28/08 09:33:28 (4 months ago)
Author:
mramm
Message:

Moving Bunch and get_partial_dict to the util module from config.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tg/configuration.py

    r5286 r5287  
    1313from pylons.middleware import ErrorHandler, StatusCodeRedirect 
    1414from tg import TGApp 
     15from tg.util import Bunch, get_partial_dict 
    1516from routes import Mapper 
    1617from routes.middleware import RoutesMiddleware 
     
    2021log = logging.getLogger(__name__) 
    2122 
    22 def get_partial_dict(prefix, dictionary): 
    23     """Given a dictionary and a prefix, return a Bunch, with just items 
    24     that start with prefix 
    25      
    26     The returned dictionary will have 'prefix.' stripped so: 
    27       
    28     get_partial_dict('prefix', {'prefix.xyz':1, 'prefix.zyx':2, 'xy':3}) 
    29      
    30     would return:  
    31      
    32     {'xyz':1,'zyx':2} 
    33     """ 
    34      
    35     match = prefix + "." 
    36      
    37     new_dict = Bunch([(key.lstrip(match) ,dictionary[key])  
    38                        for key in dictionary.iterkeys()  
    39                        if key.startswith(match)])  
    40     if new_dict: 
    41         return new_dict 
    42     else:  
    43         return AttributeError 
    4423 
    4524class PylonsConfigWrapper(dict): 
    4625    """Simple wrapper for the pylons config object that provides attribute  
    4726    style access to the pylons config dictionary. 
    48      
     27 
    4928    When used in TG, items with keys like "pylons.response_options" will  
    5029    be available via config.pylons.response_options as well as  
    5130    config['pylons.response_options']. 
    52      
     31 
    5332    This class works by proxying all attribute and dictionary access to 
    5433    the underlying pylons config object, which is a application local  
     
    5736    config data for the app that's requesting them.  
    5837    """ 
    59      
     38 
    6039    def __init__(self, dict_to_wrap): 
    6140        """Initialize by passing in a dictionary to be wrapped""" 
    6241        self.__dict__['config_proxy'] = dict_to_wrap 
    63          
     42 
    6443    def __getitem__(self, key): 
    6544        return  self.config_proxy.current_conf()[key] 
    66          
     45 
    6746    def __setitem__(self, key, value): 
    6847        self.config_proxy.current_conf()[key] = value 
     
    8059            except KeyError: 
    8160                get_partial_dict(key, self.config_proxy.current_conf()) 
    82      
     61 
    8362    def __setattr__(self, key, value): 
    8463        self.config_proxy.current_conf()[key] = value 
    85      
     64 
    8665    def __delattr__(self, name): 
    8766           try: 
     
    8968           except KeyError: 
    9069               raise AttributeError(name) 
    91      
     70 
    9271    def update(self, new_dict): 
    9372        self.config_proxy.current_conf().update(new_dict) 
    94          
     73 
    9574    def __str__(self): 
    9675        return self.config_proxy.__str__() 
    97      
     76 
    9877    @property 
    9978    def pylons(self): 
    10079        return get_partial_dict('pylons', self.config_proxy.current_conf()) 
    10180 
     81 
    10282#Create a config object that has attribute style lookup built in.  
    10383config = PylonsConfigWrapper(pylons_config) 
    10484 
    105 class Bunch(dict): 
    106     """A dictionary that provides attribute-style access.""" 
    107  
    108     def __getitem__(self, key): 
    109         return  dict.__getitem__(self, key) 
    110  
    111     def __getattr__(self, name): 
    112         try: 
    113             return self[name] 
    114         except KeyError: 
    115             return get_partial_dict(name, self) 
    116             #if both getitem, and partial-dict matches fail we're done 
    117             raise AttributeError(name) 
    118  
    119     __setattr__ = dict.__setitem__ 
    120  
    121     def __delattr__(self, name): 
    122         try: 
    123             del self[name] 
    124         except KeyError: 
    125             raise AttributeError(name) 
    12685 
    12786class AppConfig(Bunch): 
     
    13190    that is NECESSARY for proper application function.   
    13291    Deployment specific configuration information should go in  
    133     the config files (eg: development.ini or production.ini) 
     92    the config files (eg: dvelopment.ini or production.ini) 
    13493     
    13594    AppConfig instances have a number of methods that are meant to be  
  • trunk/tg/util.py

    r4126 r5287  
    4747    if hasattr(package, "model"): 
    4848        return package.model 
     49         
     50def get_partial_dict(prefix, dictionary): 
     51    """Given a dictionary and a prefix, return a Bunch, with just items 
     52    that start with prefix 
     53 
     54    The returned dictionary will have 'prefix.' stripped so: 
     55 
     56    get_partial_dict('prefix', {'prefix.xyz':1, 'prefix.zyx':2, 'xy':3}) 
     57 
     58    would return:  
     59 
     60    {'xyz':1,'zyx':2} 
     61    """ 
     62 
     63    match = prefix + "." 
     64 
     65    new_dict = Bunch([(key.lstrip(match) ,dictionary[key])  
     66                       for key in dictionary.iterkeys()  
     67                       if key.startswith(match)])  
     68    if new_dict: 
     69        return new_dict 
     70    else:  
     71        return AttributeError 
     72 
     73class Bunch(dict): 
     74    """A dictionary that provides attribute-style access.""" 
     75 
     76    def __getitem__(self, key): 
     77        return  dict.__getitem__(self, key) 
     78 
     79    def __getattr__(self, name): 
     80        try: 
     81            return self[name] 
     82        except KeyError: 
     83            return get_partial_dict(name, self) 
     84            #if both getitem, and partial-dict matches fail we're done 
     85            raise AttributeError(name) 
     86 
     87    __setattr__ = dict.__setitem__ 
     88 
     89    def __delattr__(self, name): 
     90        try: 
     91            del self[name] 
     92        except KeyError: 
     93            raise AttributeError(name)