Changeset 83

Show
Ignore:
Timestamp:
06/07/07 05:10:07
Author:
bbangert
Message:

* PEP 8 clean-up
* Added AddDictToEnviron? for multiple key additions at once.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • AuthKit/branches/0.4/authkit/authenticate/__init__.py

    r79 r83  
    126126        password = users.user(username)['password'] 
    127127        return digest.digest_password(realm, username, password) 
    128     # After speaking to Clark Evans who wrote the origianl code, this is the correct thing: 
     128    # After speaking to Clark Evans who wrote the origianl code, this is the  
     129    # correct thing: 
    129130    return None 
    130131 
     
    138139    users = None 
    139140    if len(authenticate_conf) < 1: 
    140         raise AuthKitConfigError('Expected at least one authenticate key, not %r'%authenticate_conf) 
     141        raise AuthKitConfigError('Expected at least one authenticate key, not' 
     142                                 ' %r'%authenticate_conf) 
    141143    if authenticate_conf.keys() == ['function']: 
    142144        function = authenticate_conf['function'] 
     
    185187    template = None 
    186188    if len(template_conf) != 1: 
    187         raise AuthKitConfigError('Expected one template entry, not %r'%(', '.join(template_conf.keys()))) 
     189        raise AuthKitConfigError('Expected one template entry, not %r' %  
     190                                 (', '.join(template_conf.keys()))) 
    188191    if template_conf.keys()[0] not in ['string', 'file', 'obj']: 
    189192        raise AuthKitConfigError("Template option can only be 'string', 'file' or 'obj'") 
     
    247250        return self.app(environ, start_response) 
    248251 
     252class AddDictToEnviron(object): 
     253    """Simple middleware which adds the values of a dict to the environ.""" 
     254    def __init__(self, app, dct): 
     255        self.app = app 
     256        self.dct = dct 
     257         
     258    def __call__(self, environ, start_response): 
     259        environ.update(self.dct) 
     260        return self.app(environ, start_response) 
     261 
    249262class RequireEnvironKey(object): 
    250     def __init__(self, app, key, missing_error='Missing the key %(key)s from the environ. Have you setup the correct middleware?'): 
     263    def __init__(self, app, key, missing_error=None): 
    251264        self.app = app 
    252265        self.key = key 
    253         self.missing_error = missing_error 
     266        self.missing_error = missing_error or \ 
     267            'Missing the key %(key)s from the environ. Have you setup the correct middleware?' 
    254268         
    255269    def __call__(self, environ, start_response): 
     
    343357        raise AuthKitConfigError( 
    344358            "Expected app_conf to be paste deploy app_conf dictionary " 
    345             "from not %r"%app_conf 
     359            "from not %r" % app_conf 
    346360        ) 
    347361     
     
    356370    # Check to see if middleware is disabled 
    357371    if asbool(all_conf.get('setup.enable', True)) == False: 
    358         warnings.warn( 
    359             "AuthKit middleware has been turned off by the config option authkit.setup.enable" 
    360         ) 
     372        warnings.warn("AuthKit middleware has been turned off by the config " 
     373                      "option authkit.setup.enable") 
    361374        return app 
    362375     
    363376    # Status Checking/Changing Middleware 
    364     intercept = [str(x).strip() for x in all_conf.get('setup.intercept','401').split(',')] 
     377    intercept = [str(x).strip() for x in \ 
     378                 all_conf.get('setup.intercept','401').split(',')] 
    365379    if not '401' in intercept: 
    366380        warnings.warn( 
    367             "AuthKit is configured via the authkit.setup.intercept option not to intercept " 
    368             "401 responses so the authentication middleware will not be triggered even if " 
    369             "a 401 Unauthenticated response is returned." 
    370         ) 
    371     app = AddToEnviron(app, 'authkit.intercept', intercept) 
     381            "AuthKit is configured via the authkit.setup.intercept option not " 
     382            "to intercept 401 responses so the authentication middleware will " 
     383            "not be triggered even if a 401 Unauthenticated response is " 
     384            "returned.") 
    372385     
    373386    methods = [method.strip() for method in all_conf['setup.method'].split(',')] 
     
    375388    for method in methods: 
    376389        if method in ['setup','config']: 
    377             raise AuthKitConfigError("The name %s is reserved cannot be used as a method name"%method) 
     390            raise AuthKitConfigError("The name %s is reserved cannot be used " 
     391                                     "as a method name" % method) 
    378392        if not available_methods.has_key(method): 
    379393            raise AuthKitConfigError( 
     
    395409            prefix=prefix_, 
    396410        ) 
    397      
    398     app = AddToEnviron(app, 'authkit.config', strip_base(all_conf, 'config.')) 
     411    app = AddDictToEnviron(app,  
     412                           {'authkit.config':strip_base(all_conf, 'config.'), 
     413                            'authkit.intercept':intercept}) 
    399414    return app            
    400415