Changeset 5371

Show
Ignore:
Timestamp:
09/08/08 23:15:22 (4 months ago)
Author:
percious
Message:

configuration now handles sa_auth properly.
dict.update does not return the updated dictionary... it always returns None.

Files:

Legend:

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

    r5363 r5371  
    2323 
    2424class PylonsConfigWrapper(dict): 
    25     """Simple wrapper for the pylons config object that provides attribute  
     25    """Simple wrapper for the pylons config object that provides attribute 
    2626    style access to the pylons config dictionary. 
    2727 
    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 
    3030    config['pylons.response_options']. 
    3131 
    3232    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 
    3434    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. 
    3737    """ 
    3838 
     
    4848 
    4949    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, 
    5151        if that does not work, tries dictionary lookup, and finally 
    5252        tries to grab all keys that start with the attribute and 
    5353        return sub-dictionary, that can be looked up. """ 
    54         try:  
     54        try: 
    5555            return self.config_proxy.__getattribute__(key) 
    5656        except AttributeError: 
     
    6464 
    6565    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) 
    7070 
    7171    def update(self, new_dict): 
     
    8080 
    8181 
    82 #Create a config object that has attribute style lookup built in.  
     82#Create a config object that has attribute style lookup built in. 
    8383config = PylonsConfigWrapper(pylons_config) 
    8484 
     
    8686class AppConfig(Bunch): 
    8787    """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 
    9292    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. 
    100100    """ 
    101      
     101 
    102102    def __init__(self): 
    103103        """Creates some configuration defaults""" 
    104          
     104 
    105105        #Create a few bunches we know we'll use 
    106106        self.paths = Bunch() 
    107107        self.render_functions = Bunch() 
    108          
     108 
    109109        #Set individual defaults 
    110110        self.stand_alone = True 
     
    114114        self.use_legacy_renderer = True 
    115115 
    116      
     116 
    117117    def setup_paths(self): 
    118118        root = os.path.dirname(os.path.abspath(self.package.__file__)) 
     
    129129    def init_config(self, global_conf, app_conf): 
    130130        """Initialize the config object. 
    131          
     131 
    132132        tg.config is a proxy for pylons.config that allows attribute style 
    133133        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, 
    139139                        package=self.package.__name__, 
    140140                        paths=self.paths) 
    141141        config.update(self) 
    142                          
     142 
    143143    def setup_routes(self): 
    144144        """Setup the default TG2 routes 
    145          
     145 
    146146        Overide this and setup your own routes maps if you want to use routes. 
    147147        """ 
     
    153153        # Setup a default route for the root of object dispatch 
    154154        map.connect('*url', controller='root', action='routes_placeholder') 
    155          
     155 
    156156        config['routes.map'] = map 
    157      
     157 
    158158    def setup_helpers_and_globals(self): 
    159159        config['pylons.app_globals'] = self.package.lib.app_globals.Globals() 
    160160        config['pylons.h'] = self.package.lib.helpers 
    161      
     161 
    162162    def setup_sa_auth_backend(self): 
    163163        defaults = {'users_table':'tg_user', 
     
    170170        # values such as the User, Group or Permission classes must be 
    171171        # 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 
    174175    def setup_mako_renderer(self): 
    175176        """Setup a renderer and loader for mako templates""" 
     
    183184            imports=['from webhelpers.html import escape'], 
    184185            default_filters=['escape']) 
    185          
     186 
    186187        self.render_functions.mako = render_mako 
    187          
     188 
    188189    def setup_genshi_renderer(self): 
    189190        """Setup a renderer and loader for Genshi templates""" 
     
    194195            "Plug-in our i18n function to Genshi." 
    195196            genshi.template.filters.insert(0, Translator(ugettext)) 
    196         loader = TemplateLoader(search_path=self.paths.templates,  
     197        loader = TemplateLoader(search_path=self.paths.templates, 
    197198                                auto_reload=True) 
    198                                  
     199 
    199200        config['pylons.app_globals'].genshi_loader = loader 
    200201 
    201202        self.render_functions.genshi = render_genshi 
    202      
     203 
    203204    def setup_jinja_renderer(self): 
    204205        """Setup a renderer and loader for Jinja templates""" 
     
    212213 
    213214        self.render_functions.jinja = render_jinja 
    214      
     215 
    215216    def setup_default_renderer(self): 
    216217        """Setup template defaults in the buffed plugin 
    217          
     218 
    218219        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 
    221222        release. 
    222223        """ 
     
    224225        config['buffet.template_engines'].pop() 
    225226        template_location = '%s.templates' %self.package.__name__ 
    226         config.add_template_engine(self.default_renderer,  
     227        config.add_template_engine(self.default_renderer, 
    227228                                   template_location,  {}) 
    228      
     229 
    229230    def setup_sqlalchemy(self): 
    230231        """Setup SQLAlchemy database engine""" 
     
    234235        # Pass the engine to initmodel, to be able to introspect tables 
    235236        self.package.model.init_model(engine) 
    236          
     237 
    237238    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 
    243244        nessisary.""" 
    244          
     245 
    245246        def load_environment(global_conf, app_conf): 
    246247            """Configure the Pylons environment via the ``pylons.config`` 
     
    253254            self.setup_routes() 
    254255            self.setup_helpers_and_globals() 
    255              
    256             if self.auth_backend == "sqlalchemy":  
     256 
     257            if self.auth_backend == "sqlalchemy": 
    257258                self.setup_sa_auth_backend() 
    258259 
    259             if 'genshi' in self.renderers:  
     260            if 'genshi' in self.renderers: 
    260261                self.setup_genshi_renderer() 
    261262 
    262263            if 'mako' in self.renderers: 
    263264                self.setup_mako_renderer() 
    264              
     265 
    265266            if 'jinja' in self.renderers: 
    266267                self.setup_jinja_renderer() 
     
    268269            if self.use_legacy_renderer: 
    269270                self.setup_default_renderer() 
    270              
     271 
    271272            if self.use_sqlalchemy: 
    272273                self.setup_sqlalchemy() 
     
    293294        auth = self.sa_auth 
    294295 
    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, 
    298299                                  auth.dbsession, 
    299300                                  ) 
    300301        return app 
    301      
    302     def add_core_middleware(self, app):     
     302 
     303    def add_core_middleware(self, app): 
    303304        """Adds support for routes dispatch, sessions, and caching""" 
    304305        app = RoutesMiddleware(app, config['routes.map']) 
     
    306307        app = CacheMiddleware(app, config) 
    307308        return app 
    308      
     309 
    309310    def add_tosca_middleware(self, app): 
    310311        """Configure the ToscaWidgets middleware""" 
    311312        app = tw_middleware(app, { 
    312             'toscawidgets.framework.default_view':  
     313            'toscawidgets.framework.default_view': 
    313314            self.default_renderer, 
    314315            'toscawidgets.middleware.inject_resources': True, 
    315316            }) 
    316317        return app 
    317      
     318 
    318319    def add_static_file_middleware(self, app): 
    319320        static_app = StaticURLParser(config['pylons.paths']['static_files']) 
     
    334335    def add_tm_middleware(self, app): 
    335336        """Sets up the transaction managment middleware 
    336          
     337 
    337338        To abort a transaction inside a TG2 app:: 
    338          
     339 
    339340          import transaction 
    340341          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 
    344345        base_config.commit_veto 
    345346        """ 
     
    349350    def add_dbsession_remover_middleware(self, app): 
    350351        """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 
    353354        every request.  Only overide this method if you know what you 
    354355        are doing! 
    355          
     356 
    356357        """ 
    357358        def remover(environ, start_response): 
     
    372373            A dictionary any special values nessisary for setting up 
    373374            the base wsgi app. 
    374         """                   
     375        """ 
    375376 
    376377        def make_base_app(global_conf, wrap_app=None, full_stack=True, **app_conf): 
     
    395396            load_environment(global_conf, app_conf) 
    396397            app = TGApp() 
    397             if wrap_app:  
     398            if wrap_app: 
    398399                wrap_app(app) 
    399400            app = self.add_core_middleware(app) 
     
    418419            app = RegistryManager(app) 
    419420 
    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: 
    423424                app = self.add_static_file_middleware(app) 
    424425            return app