Changeset 5287
- Timestamp:
- 08/28/08 09:33:28 (4 months ago)
- Files:
-
- trunk/tg/configuration.py (modified) (6 diffs)
- trunk/tg/util.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tg/configuration.py
r5286 r5287 13 13 from pylons.middleware import ErrorHandler, StatusCodeRedirect 14 14 from tg import TGApp 15 from tg.util import Bunch, get_partial_dict 15 16 from routes import Mapper 16 17 from routes.middleware import RoutesMiddleware … … 20 21 log = logging.getLogger(__name__) 21 22 22 def get_partial_dict(prefix, dictionary):23 """Given a dictionary and a prefix, return a Bunch, with just items24 that start with prefix25 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_dict42 else:43 return AttributeError44 23 45 24 class PylonsConfigWrapper(dict): 46 25 """Simple wrapper for the pylons config object that provides attribute 47 26 style access to the pylons config dictionary. 48 27 49 28 When used in TG, items with keys like "pylons.response_options" will 50 29 be available via config.pylons.response_options as well as 51 30 config['pylons.response_options']. 52 31 53 32 This class works by proxying all attribute and dictionary access to 54 33 the underlying pylons config object, which is a application local … … 57 36 config data for the app that's requesting them. 58 37 """ 59 38 60 39 def __init__(self, dict_to_wrap): 61 40 """Initialize by passing in a dictionary to be wrapped""" 62 41 self.__dict__['config_proxy'] = dict_to_wrap 63 42 64 43 def __getitem__(self, key): 65 44 return self.config_proxy.current_conf()[key] 66 45 67 46 def __setitem__(self, key, value): 68 47 self.config_proxy.current_conf()[key] = value … … 80 59 except KeyError: 81 60 get_partial_dict(key, self.config_proxy.current_conf()) 82 61 83 62 def __setattr__(self, key, value): 84 63 self.config_proxy.current_conf()[key] = value 85 64 86 65 def __delattr__(self, name): 87 66 try: … … 89 68 except KeyError: 90 69 raise AttributeError(name) 91 70 92 71 def update(self, new_dict): 93 72 self.config_proxy.current_conf().update(new_dict) 94 73 95 74 def __str__(self): 96 75 return self.config_proxy.__str__() 97 76 98 77 @property 99 78 def pylons(self): 100 79 return get_partial_dict('pylons', self.config_proxy.current_conf()) 101 80 81 102 82 #Create a config object that has attribute style lookup built in. 103 83 config = PylonsConfigWrapper(pylons_config) 104 84 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 done117 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)126 85 127 86 class AppConfig(Bunch): … … 131 90 that is NECESSARY for proper application function. 132 91 Deployment specific configuration information should go in 133 the config files (eg: d evelopment.ini or production.ini)92 the config files (eg: dvelopment.ini or production.ini) 134 93 135 94 AppConfig instances have a number of methods that are meant to be trunk/tg/util.py
r4126 r5287 47 47 if hasattr(package, "model"): 48 48 return package.model 49 50 def 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 73 class 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)