comparison MoinMoin/events/__init__.py @ 2064:5cc8f6714297

Add more event types. Return a list of messages generated by event handlers to caller.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Thu, 31 May 2007 01:00:27 +0200
parents 7547652ffb6f
children afa784d8d58d
comparison
equal deleted inserted replaced
2063:5d4d32869629 2064:5cc8f6714297
3 MoinMoin - event (notification) framework 3 MoinMoin - event (notification) framework
4 4
5 This code abstracts event handling in MoinMoin, 5 This code abstracts event handling in MoinMoin,
6 currently for notifications. It implements the observer pattern. 6 currently for notifications. It implements the observer pattern.
7 7
8 @copyright: 2007 by Karol Nowak <grywacz@gmail.com> 8 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
9 @license: GNU GPL, see COPYING for details. 9 @license: GNU GPL, see COPYING for details.
10 """ 10 """
11 11
12 from MoinMoin import wikiutil 12 from MoinMoin import wikiutil
13 from MoinMoin.util import pysupport 13 from MoinMoin.util import pysupport
14 14
18 # Create a list of extension actions from the package directory 18 # Create a list of extension actions from the package directory
19 modules = pysupport.getPackageModules(__file__) 19 modules = pysupport.getPackageModules(__file__)
20 20
21 class Observable: 21 class Observable:
22 """A class handling information common to all events.""" 22 """A class handling information common to all events."""
23 pass 23
24 def __init__(self, request):
25 self.request = request
24 26
25 class PageEvent(Observable): 27 class PageEvent(Observable):
26 """An event related to a page change""" 28 """An event related to a page change"""
27
28 def __init__(self, request, page):
29 self.request = request
30 self.page = page
31 29
32 class PageChangedEvent(PageEvent): 30 class PageChangedEvent(PageEvent):
33 pass 31 def __init__(self, request, page, comment, trivial):
32 Observable.__init__(self, request)
33 self.page = page
34 self.comment = comment
35 self.trivial = trivial
36
34 class PageRenamedEvent(PageEvent): 37 class PageRenamedEvent(PageEvent):
35 pass 38 pass
36 class PageDeletedEvent(PageEvent): 39 class PageDeletedEvent(PageEvent):
37 pass 40 pass
38 41 class FileAttachedEvent(PageEvent):
42 pass
43
44 class PageRevertedEvent(PageEvent):
45 def __init__(self, request, pagename, previous, current):
46 Observable.__init__(self, request)
47 self.pagename = pagename
48 self.previous = previous
49 self.current = current
50
51 class SubscribedToPageEvent(PageEvent):
52 def __init__(self, request, pagename, username):
53 Observable.__init__(self, request)
54 self.pagename = pagename
55 self.username = username
56
39 def register_handlers(cfg): 57 def register_handlers(cfg):
40 """Create a list of available event handlers. 58 """Create a list of available event handlers.
41 59
42 Each handler is a handle() function defined in an plugin, 60 Each handler is a handle() function defined in an plugin,
43 pretty much like in case of actions. 61 pretty much like in case of actions.
55 if handler is not None: 73 if handler is not None:
56 event_handlers.append(handler) 74 event_handlers.append(handler)
57 75
58 def send_event(event): 76 def send_event(event):
59 """Function called from outside to process an event""" 77 """Function called from outside to process an event"""
78
79 # A list of messages generated by event handlers, passed back to caller
80 msg = []
60 81
61 # Find all available event handlers 82 # Find all available event handlers
62 if event_handlers is None: 83 if event_handlers is None:
63 register_handlers(event.request.cfg) 84 register_handlers(event.request.cfg)
64 85
65 # Try to handle the event with each available handler (for now) 86 # Try to handle the event with each available handler (for now)
66 for handle in event_handlers: 87 for handle in event_handlers:
67 handle(event) 88 retval = handle(event)
89 if isinstance(retval, unicode):
90 msg.append(retval)
91
92 return msg