Were Identity based on generic functions (as was proposed using a very 
similar use case), one would get a very capable event system for free. ;)

Cheers,
Simon

Jeff Watkins wrote:
> There's a pending ticket for having identity fire events based on what's 
> happening. I'd like to implement this (at some point), but I think 
> having a generic event system is really powerful. I propose the following:
> 
> # Preliminary events system
> 
> from turbogears.decorator import decorator
> 
> _event_registry= dict();
> 
> def register_event_hook( event_name, hook_fn ):
>     '''
>     Add the hook function to the list of event hooks.
>     '''
>     if not event_name in _event_registry:
>         _event_registry[event_name]= []
>     _event_registry[event_name].append( hook_fn )
>    
> 
> def unregister_event_hook( event_name, hook_fn ):
>     '''
>     Remove a hook function from the event.
>     '''
>     if not event_name in _event_registry:
>         return   
>     try:
>         _event_registry[event_name].remove( hook_fn )
>     except ValueError:
>         pass
> 
> def fire_event( event_name, *args, **kwargs ):
>     if event_name is None:
>         return
>     events= _event_registry[event_name]
>     if not events:
>         return
>     for event_fn in events:
>         event_fn( *args, **kwargs )
> 
> 
> def fires_event(event_name, pre_event_name=None):
>     '''
>     Function decorator that triggers a hookable event.
>     '''
>     def entangle(fn):
>        
>         def fires_event(func, self, *args, **kwargs):
>             fire_event( pre_event_name, *args, **kwargs )
>             result= fn(self, *args, **kwargs)
>             fire_event( event_name, *args, **kwargs )
>             return result
>         
>         # Setup events in registry
>         if not event_name in _event_registry:
>             _event_registry[event_name]= []
>         if not pre_event_name in _event_registry:
>             _event_registry[pre_event_name]= []
>                
>         return decorator(fires_event)(fn)
>     return entangle
> 
> This would allow me to write the following code:
> 
> @fires_event( 'identity.validate' )
> def validate_identity( self, some, args ):
> ...
> 
> And whenever the validate_identity method is invoked, the pre- and 
> post-event handlers get called.
> 
> Furthermore, if we were to allow cancelling an event, this would allow 
> someone to write a plug-in that tracked the number of times a particular 
> visitor has attempted to log in and lock the account if too many 
> failures have occurred. And other wacky stuff.
> 
> Any thoughts?
> 
> 
> --
> Jeff Watkins
> http://newburyportion.com/
> 
> "Not everything that can be counted counts, and not everything that 
> counts can be counted."
> -- Albert Einstein
> 
> 
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears Trunk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears-trunk
-~----------~----~----~----~------~----~------~--~---

Reply via email to