~thekorn/zeitgeist/exclusive_clients

« back to all changes in this revision

Viewing changes to _zeitgeist/engine/extension.py

  • Committer: "Mikkel Kamstrup Erlandsen"
  • Date: 2009-12-07 21:02:38 UTC
  • Revision ID: mikkel.kamstrup@gmail.com-20091207210238-rvlswz6qsrq33i9n
Implement two hooks extensions may implement in order to filter, change, or block events as they enter or leave the engine

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
        
23
23
        Every extension has to define a list of accessible methods as
24
24
        'PUBLIC_METHODS'. The constructor of an Extension object takes the
25
 
        engine object it extends as the only argument
 
25
        engine object it extends as the only argument.
 
26
        
 
27
        In addition each extension has a set of hooks to control how events are
 
28
        inserted and retrieved from the log. These hooks can either block the
 
29
        event completely, modify it, or add additional metadata to it.
26
30
        """
27
31
        PUBLIC_METHODS = None
28
32
        
29
33
        def __init__(self, engine):
30
34
                self.engine = engine
31
35
        
 
36
        def filter_insert_event(self, event):
 
37
                """
 
38
                Hook applied to all events before they are inserted into the
 
39
                log. The returned event is progressively passed through all
 
40
                extensions before the final result is inserted.
 
41
                
 
42
                To block an event completely simply return :const:`None`.
 
43
                The event may also be modified or completely substituted for
 
44
                another event.
 
45
                
 
46
                The default implementation of this method simply returns the
 
47
                event as is.
 
48
                
 
49
                :param event: An :class:`Event <zeitgeist.datamodel.Event>`
 
50
                    instance
 
51
                :returns: The filtered event instance to insert into the log
 
52
                """
 
53
                return event
 
54
        
 
55
        def filter_get_event(self, event):
 
56
                """
 
57
                Hook applied to all events before they are returned to a client.
 
58
                The event returned from this method is progressively passed
 
59
                through all extensions before they final result is returned to
 
60
                the client.
 
61
                
 
62
                To prevent an event from ever leaving the server process simply
 
63
                return :const:`None`. The event may also be changed in place
 
64
                or fully substituted for another event.
 
65
                
 
66
                The default implementation of this method simply returns the
 
67
                event as is.
 
68
                
 
69
                :param event: An :class:`Event <zeitgeist.datamodel.Event>`
 
70
                    instance
 
71
                :returns: The filtered event instance as the client
 
72
                    should see it
 
73
                """
 
74
                return event
32
75
        
33
76
class ExtensionsCollection(object):
34
77
        """ Collection to manage all extensions """
64
107
                
65
108
        def __len__(self):
66
109
                return len(self.__extensions)
67
 
                
 
110
        
 
111
        def __iter__ (self):
 
112
                return self.__extensions.itervalues()
 
113
        
68
114
        @property
69
115
        def methods(self):
70
116
                return self.__methods