Changeset 5371
- Timestamp:
- 09/08/08 23:15:22 (4 months ago)
- Files:
-
- trunk/tg/configuration.py (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tg/configuration.py
r5363 r5371 23 23 24 24 class PylonsConfigWrapper(dict): 25 """Simple wrapper for the pylons config object that provides attribute 25 """Simple wrapper for the pylons config object that provides attribute 26 26 style access to the pylons config dictionary. 27 27 28 When used in TG, items with keys like "pylons.response_options" will 29 be available via config.pylons.response_options as well as 28 When used in TG, items with keys like "pylons.response_options" will 29 be available via config.pylons.response_options as well as 30 30 config['pylons.response_options']. 31 31 32 32 This class works by proxying all attribute and dictionary access to 33 the underlying pylons config object, which is a application local 33 the underlying pylons config object, which is a application local 34 34 proxy that allows for multiple pylons/tg2 applicatoins to live 35 in the same process simultaniously, but to always get the right 36 config data for the app that's requesting them. 35 in the same process simultaniously, but to always get the right 36 config data for the app that's requesting them. 37 37 """ 38 38 … … 48 48 49 49 def __getattr__(self, key): 50 """Tries to get the attribute off the wrapped object first, 50 """Tries to get the attribute off the wrapped object first, 51 51 if that does not work, tries dictionary lookup, and finally 52 52 tries to grab all keys that start with the attribute and 53 53 return sub-dictionary, that can be looked up. """ 54 try: 54 try: 55 55 return self.config_proxy.__getattribute__(key) 56 56 except AttributeError: … … 64 64 65 65 def __delattr__(self, name): 66 try:67 del self.config_proxy.current_conf()[name]68 except KeyError:69 raise AttributeError(name)66 try: 67 del self.config_proxy.current_conf()[name] 68 except KeyError: 69 raise AttributeError(name) 70 70 71 71 def update(self, new_dict): … … 80 80 81 81 82 #Create a config object that has attribute style lookup built in. 82 #Create a config object that has attribute style lookup built in. 83 83 config = PylonsConfigWrapper(pylons_config) 84 84 … … 86 86 class AppConfig(Bunch): 87 87 """Class to store application configuration 88 89 This class should have configuration/setup information 90 that is NECESSARY for proper application function. 91 Deployment specific configuration information should go in 88 89 This class should have configuration/setup information 90 that is NECESSARY for proper application function. 91 Deployment specific configuration information should go in 92 92 the config files (eg: dvelopment.ini or production.ini) 93 94 AppConfig instances have a number of methods that are meant to be 95 overridden by users who wish to have finer grained controll over 96 the setup of the WSGI envirnment in which their applcation is run. 97 98 This is the place to configure custom routes, transaction handling, 99 error handling, etc. 93 94 AppConfig instances have a number of methods that are meant to be 95 overridden by users who wish to have finer grained controll over 96 the setup of the WSGI envirnment in which their applcation is run. 97 98 This is the place to configure custom routes, transaction handling, 99 error handling, etc. 100 100 """ 101 101 102 102 def __init__(self): 103 103 """Creates some configuration defaults""" 104 104 105 105 #Create a few bunches we know we'll use 106 106 self.paths = Bunch() 107 107 self.render_functions = Bunch() 108 108 109 109 #Set individual defaults 110 110 self.stand_alone = True … … 114 114 self.use_legacy_renderer = True 115 115 116 116 117 117 def setup_paths(self): 118 118 root = os.path.dirname(os.path.abspath(self.package.__file__)) … … 129 129 def init_config(self, global_conf, app_conf): 130 130 """Initialize the config object. 131 131 132 132 tg.config is a proxy for pylons.config that allows attribute style 133 133 access, so it's automatically setup when we create the poylons config 134 135 Besides basic initialization, this method copies all the values 136 in base_config into the ``tg.config`` object. 137 """ 138 pylons_config.init_app(global_conf, app_conf, 134 135 Besides basic initialization, this method copies all the values 136 in base_config into the ``tg.config`` object. 137 """ 138 pylons_config.init_app(global_conf, app_conf, 139 139 package=self.package.__name__, 140 140 paths=self.paths) 141 141 config.update(self) 142 142 143 143 def setup_routes(self): 144 144 """Setup the default TG2 routes 145 145 146 146 Overide this and setup your own routes maps if you want to use routes. 147 147 """ … … 153 153 # Setup a default route for the root of object dispatch 154 154 map.connect('*url', controller='root', action='routes_placeholder') 155 155 156 156 config['routes.map'] = map 157 157 158 158 def setup_helpers_and_globals(self): 159 159 config['pylons.app_globals'] = self.package.lib.app_globals.Globals() 160 160 config['pylons.h'] = self.package.lib.helpers 161 161 162 162 def setup_sa_auth_backend(self): 163 163 defaults = {'users_table':'tg_user', … … 170 170 # values such as the User, Group or Permission classes must be 171 171 # explicitly defined. 172 config['sa_auth'] = defaults.update(config['sa_auth']) 173 172 config['sa_auth'] = defaults 173 config['sa_auth'].update(self.sa_auth) 174 174 175 def setup_mako_renderer(self): 175 176 """Setup a renderer and loader for mako templates""" … … 183 184 imports=['from webhelpers.html import escape'], 184 185 default_filters=['escape']) 185 186 186 187 self.render_functions.mako = render_mako 187 188 188 189 def setup_genshi_renderer(self): 189 190 """Setup a renderer and loader for Genshi templates""" … … 194 195 "Plug-in our i18n function to Genshi." 195 196 genshi.template.filters.insert(0, Translator(ugettext)) 196 loader = TemplateLoader(search_path=self.paths.templates, 197 loader = TemplateLoader(search_path=self.paths.templates, 197 198 auto_reload=True) 198 199 199 200 config['pylons.app_globals'].genshi_loader = loader 200 201 201 202 self.render_functions.genshi = render_genshi 202 203 203 204 def setup_jinja_renderer(self): 204 205 """Setup a renderer and loader for Jinja templates""" … … 212 213 213 214 self.render_functions.jinja = render_jinja 214 215 215 216 def setup_default_renderer(self): 216 217 """Setup template defaults in the buffed plugin 217 218 218 219 This is only used when use_legacy_renderer is set to True 219 220 And it will not depricated in the next major turbogears 220 221 And it will not depricated in the next major turbogears 221 222 release. 222 223 """ … … 224 225 config['buffet.template_engines'].pop() 225 226 template_location = '%s.templates' %self.package.__name__ 226 config.add_template_engine(self.default_renderer, 227 config.add_template_engine(self.default_renderer, 227 228 template_location, {}) 228 229 229 230 def setup_sqlalchemy(self): 230 231 """Setup SQLAlchemy database engine""" … … 234 235 # Pass the engine to initmodel, to be able to introspect tables 235 236 self.package.model.init_model(engine) 236 237 237 238 def make_load_environment(self): 238 """Returns a load_environment function 239 240 The returned load_environment function can be called to configure the 241 TurboGears runtime environment for this particular application. You 242 can do this dynamically with multiple nested TG applications if 239 """Returns a load_environment function 240 241 The returned load_environment function can be called to configure the 242 TurboGears runtime environment for this particular application. You 243 can do this dynamically with multiple nested TG applications if 243 244 nessisary.""" 244 245 245 246 def load_environment(global_conf, app_conf): 246 247 """Configure the Pylons environment via the ``pylons.config`` … … 253 254 self.setup_routes() 254 255 self.setup_helpers_and_globals() 255 256 if self.auth_backend == "sqlalchemy": 256 257 if self.auth_backend == "sqlalchemy": 257 258 self.setup_sa_auth_backend() 258 259 259 if 'genshi' in self.renderers: 260 if 'genshi' in self.renderers: 260 261 self.setup_genshi_renderer() 261 262 262 263 if 'mako' in self.renderers: 263 264 self.setup_mako_renderer() 264 265 265 266 if 'jinja' in self.renderers: 266 267 self.setup_jinja_renderer() … … 268 269 if self.use_legacy_renderer: 269 270 self.setup_default_renderer() 270 271 271 272 if self.use_sqlalchemy: 272 273 self.setup_sqlalchemy() … … 293 294 auth = self.sa_auth 294 295 295 app = make_who_middleware(app, config, auth.user_class, 296 auth.user_criterion, 297 auth.user_id_column, 296 app = make_who_middleware(app, config, auth.user_class, 297 auth.user_criterion, 298 auth.user_id_column, 298 299 auth.dbsession, 299 300 ) 300 301 return app 301 302 def add_core_middleware(self, app): 302 303 def add_core_middleware(self, app): 303 304 """Adds support for routes dispatch, sessions, and caching""" 304 305 app = RoutesMiddleware(app, config['routes.map']) … … 306 307 app = CacheMiddleware(app, config) 307 308 return app 308 309 309 310 def add_tosca_middleware(self, app): 310 311 """Configure the ToscaWidgets middleware""" 311 312 app = tw_middleware(app, { 312 'toscawidgets.framework.default_view': 313 'toscawidgets.framework.default_view': 313 314 self.default_renderer, 314 315 'toscawidgets.middleware.inject_resources': True, 315 316 }) 316 317 return app 317 318 318 319 def add_static_file_middleware(self, app): 319 320 static_app = StaticURLParser(config['pylons.paths']['static_files']) … … 334 335 def add_tm_middleware(self, app): 335 336 """Sets up the transaction managment middleware 336 337 337 338 To abort a transaction inside a TG2 app:: 338 339 339 340 import transaction 340 341 transaction.doom() 341 342 By default http error responses also roll back transactions, 343 but this behavior can be overridden by overiding 342 343 By default http error responses also roll back transactions, 344 but this behavior can be overridden by overiding 344 345 base_config.commit_veto 345 346 """ … … 349 350 def add_dbsession_remover_middleware(self, app): 350 351 """Sets up middleware that cleans up the sqlalchmy session 351 352 The default behavior of TG2 is to clean up the session on 352 353 The default behavior of TG2 is to clean up the session on 353 354 every request. Only overide this method if you know what you 354 355 are doing! 355 356 356 357 """ 357 358 def remover(environ, start_response): … … 372 373 A dictionary any special values nessisary for setting up 373 374 the base wsgi app. 374 """ 375 """ 375 376 376 377 def make_base_app(global_conf, wrap_app=None, full_stack=True, **app_conf): … … 395 396 load_environment(global_conf, app_conf) 396 397 app = TGApp() 397 if wrap_app: 398 if wrap_app: 398 399 wrap_app(app) 399 400 app = self.add_core_middleware(app) … … 418 419 app = RegistryManager(app) 419 420 420 # Static files (If running in production, and Apache or another 421 # web server is serving static files) 422 if self.serve_static: 421 # Static files (If running in production, and Apache or another 422 # web server is serving static files) 423 if self.serve_static: 423 424 app = self.add_static_file_middleware(app) 424 425 return app