diff MoinMoin/events/__init__.py @ 2307:fe37834deb3d

Move a list of event handlers and subscribable events to config.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Fri, 22 Jun 2007 17:15:26 +0200
parents cf4c4a3d6085
children a85d51778eda
line wrap: on
line diff
--- a/MoinMoin/events/__init__.py	Thu Jun 21 23:40:21 2007 +0200
+++ b/MoinMoin/events/__init__.py	Fri Jun 22 17:15:26 2007 +0200
@@ -13,17 +13,9 @@
 from MoinMoin.util import pysupport
 from MoinMoin.wikiutil import PluginAttributeError
 
-
-# A list of available event handlers
-_event_handlers = None
-
-# A list of event types that user can subscribe to
-_subscribable_events = None
-
 # Create a list of extension actions from the package directory
 modules = pysupport.getPackageModules(__file__)
 
-
 class Event(object):
     """A class handling information common to all events."""
     def __init__(self, request):
@@ -128,17 +120,16 @@
         Event.__init__(self, request)
         self.user = user
         
-def _register_handlers(cfg):
+def get_handlers(cfg):
     """Create a list of available event handlers.
     
     Each handler is a handle() function defined in an plugin,
     pretty much like in case of actions.
     
-    TODO: maybe make it less dumb? ;-)"""
+    TODO: maybe make it less dumb? ;-)
     
-    global _event_handlers
-
-    _event_handlers = []
+    """
+    event_handlers = []
     names = wikiutil.getPlugins("events", cfg)
 
     for name in names:
@@ -148,7 +139,9 @@
             handler = None
         
         if handler is not None:
-            _event_handlers.append(handler)
+            event_handlers.append(handler)
+            
+    return event_handlers
 
 
 def send_event(event):
@@ -156,13 +149,14 @@
    
     # A list of messages generated by event handlers, passed back to caller
     msg = []
+    cfg = event.request.cfg
     
     # Find all available event handlers
-    if _event_handlers is None:
-        _register_handlers(event.request.cfg)
+    if cfg.event_handlers is None:
+        register_handlers(cfg)
     
     # Try to handle the event with each available handler (for now)
-    for handle in _event_handlers:
+    for handle in cfg.event_handlers:
         retval = handle(event)
         if isinstance(retval, unicode):
             msg.append(retval)
@@ -175,15 +169,11 @@
     @return: A list of user-visible events described by dictionaries
     @rtype: dict
     """
-    global _subscribable_events
     defs = globals()
+    subscribable_events = {}    
     
-    if not _subscribable_events:
-        _subscribable_events = {}
-        
-        for ev in defs.values():
-            if type(ev) is type and issubclass(ev, Event) and ev.__dict__.has_key("description"):
-                _subscribable_events[ev.__name__] = {'desc': ev.description,
-                                                     'superuser': ev.req_superuser}
-    
-    return _subscribable_events
+    for ev in defs.values():
+        if type(ev) is type and issubclass(ev, Event) and ev.__dict__.has_key("description"):
+            subscribable_events[ev.__name__] = {'desc': ev.description,
+                                                 'superuser': ev.req_superuser}
+    return subscribable_events