changeset 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 84056f004725
children d6333a20bc06
files MoinMoin/config/multiconfig.py MoinMoin/events/__init__.py MoinMoin/events/jabbernotify.py
diffstat 3 files changed, 33 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/config/multiconfig.py	Thu Jun 21 23:40:21 2007 +0200
+++ b/MoinMoin/config/multiconfig.py	Fri Jun 22 17:15:26 2007 +0200
@@ -14,6 +14,7 @@
 
 from MoinMoin import config, error, util, wikiutil
 import MoinMoin.auth as authmodule
+import MoinMoin.events as events
 from MoinMoin import session
 from MoinMoin.packages import packLine
 from MoinMoin.security import AccessControlList
@@ -446,6 +447,7 @@
     show_version = False
     siteid = 'default'
     stylesheets = [] # list of tuples (media, csshref) to insert after theme css, before user css
+    _subscribable_events = None # A list of event types that user can subscribe to
     subscribed_pages_default = [] # preload user subscribed pages with this page list
     subscribed_events_default = [] # preload user subscribed events with this list
     superuser = [] # list of unicode user names that have super powers :)
@@ -700,6 +702,10 @@
 
         if self.url_prefix_local is None:
             self.url_prefix_local = self.url_prefix_static
+            
+        # Register a list of available event handlers - this has to stay at the
+        # end, because loading plugins depends on having a config object
+        self.event_handlers = events.get_handlers(self)
 
 
     def load_meta_dict(self):
@@ -718,6 +724,15 @@
         return property(getter)
     iwid = make_iwid_property("_iwid")
     iwid_full = make_iwid_property("_iwid_full")
+    
+    # lazily load a list of events a user can subscribe to
+    def make_subscribable_events_prop():
+        def getter(self):
+            if getattr(self, "_subscribable_events", None) is None:
+                self._subscribable_events = events.get_subscribable_events()
+            return getattr(self, "_subscribable_events")
+        return property(getter)
+    subscribable_events = make_subscribable_events_prop()
 
     def load_IWID(self):
         """ Loads the InterWikiID of this instance. It is used to identify the instance
--- 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
--- a/MoinMoin/events/jabbernotify.py	Thu Jun 21 23:40:21 2007 +0200
+++ b/MoinMoin/events/jabbernotify.py	Fri Jun 22 17:15:26 2007 +0200
@@ -23,7 +23,7 @@
     # Check for desired event type and if notification bot is configured
     if not cfg.jabber_enabled:
         return
-    
+
     if isinstance(event, ev.PageChangedEvent):
         return handle_page_changed(event)
     elif isinstance(event, ev.JabberIDSetEvent) or isinstance(event, ev.JabberIDUnsetEvent):
@@ -92,14 +92,12 @@
         
 def handle_page_changed(event):
     """ Handles events related to page changes """
-    
     request = event.request
     server = request.cfg.notification_server
     page = event.page
     
     subscribers = page.getSubscribers(request, return_users=1, trivial=event.trivial)    
     _filter_subscriber_list(event, subscribers)
-
     page_change("page_changed", request, page, subscribers, revisions=page.getRevList(), comment=event.comment)
     
 
@@ -112,7 +110,6 @@
     
     subscribers = page.getSubscribers(request, return_users=1)
     _filter_subscriber_list(event, subscribers)
-    
     page_change("page_deleted", request, page, subscribers)