6
6
from typing import (
7
7
Callable ,
8
8
Dict ,
9
- Iterable ,
10
9
Iterator ,
11
10
List ,
12
11
NewType ,
@@ -36,7 +35,7 @@ class Pluggable:
36
35
37
36
valid_events : Optional [Set [EventIdenifier ]] = None
38
37
39
- def __init__ (self , * , plugins : Sequence [PluginArgument ] = None ):
38
+ def __init__ (self , * , plugins : Sequence [PluginArgument ] = [] ):
40
39
"""Initialize a `Pluggable`.
41
40
42
41
:param plugins: Plugins which should be attached to this `Pluggable`.
@@ -50,14 +49,13 @@ def __init__(self, *, plugins: Sequence[PluginArgument] = None):
50
49
self ._event_queue : Queue = Queue ()
51
50
self ._processing_events = False
52
51
53
- if plugins is not None :
54
- for plugin in plugins :
55
- if isclass (plugin ):
56
- # instantiate plugin
57
- plugin = plugin ()
52
+ for plugin in plugins :
53
+ if isclass (plugin ):
54
+ # instantiate plugin
55
+ plugin = plugin ()
58
56
59
- plugin = cast ("BasePlugin" , plugin )
60
- plugin .attach_to (self )
57
+ plugin = cast ("BasePlugin" , plugin )
58
+ plugin .attach_to (self )
61
59
62
60
@property
63
61
def plugins (self ):
@@ -72,7 +70,7 @@ def validate_event(self, *events: EventIdenifier):
72
70
73
71
if self .valid_events is not None :
74
72
if event not in self .valid_events :
75
- raise RuntimeError (f"Event '{ event } ' not recognized (available { self .valid_events } ) " )
73
+ raise RuntimeError (f"Event '{ event } ' not recognized. Available: { ', ' . join ( self .valid_events ) } " )
76
74
return event
77
75
78
76
def register_hook (self , func : Callable , * events : EventIdenifier ):
@@ -92,31 +90,23 @@ def register_hook(self, func: Callable, *events: EventIdenifier):
92
90
self ._hook_handles [event ][handle .id ] = handle
93
91
return handle
94
92
95
- def dispatch (self , event : EventIdenifier , * args , ** kwargs ) -> dict :
93
+ def dispatch (self , event : EventIdenifier , * args , ** kwargs ) -> None :
96
94
"""Call all functions hooked to a certain event."""
97
95
self .validate_event (event )
98
96
99
- events_return_value : dict = {}
100
- self ._event_queue .put ((event , args , kwargs , events_return_value ))
97
+ self ._event_queue .put ((event , args , kwargs ))
101
98
102
99
if not self ._processing_events :
103
100
self ._processing_events = True
104
101
105
102
while not self ._event_queue .empty ():
106
- event , args , kwargs , combined_return_values = self ._event_queue .get ()
103
+ event , args , kwargs = self ._event_queue .get ()
107
104
108
105
for hook in self ._hook_handles [event ].values ():
109
- returned = hook (* args , ** kwargs )
110
-
111
- if returned is not None :
112
- combined_return_values .update (returned )
106
+ hook (* args , ** kwargs )
113
107
114
108
self ._processing_events = False
115
109
116
- # this dict may be empty and will be complete once all events have been
117
- # processed
118
- return events_return_value
119
-
120
110
def remove_hook (self , handle : "HookHandle" ):
121
111
"""Remove a hook handle from this instance."""
122
112
for event in handle .events :
@@ -146,6 +136,10 @@ def id(self) -> HookHandleId:
146
136
"""Return the id of this `HookHandle`."""
147
137
return self ._id
148
138
139
+ @property
140
+ def func_name (self ):
141
+ return self ._func .__qualname__
142
+
149
143
@property
150
144
def events (self ) -> Iterator [EventIdenifier ]:
151
145
"""Return iterator of events whis `HookHandle` is registered for."""
@@ -165,7 +159,7 @@ def __call__(self, *args, **kw):
165
159
for name in kw .keys ():
166
160
if name not in sig .parameters :
167
161
raise TypeError (
168
- f"Hook callback { self ._func . __qualname__ } () does not accept keyword argument '{ name } '"
162
+ f"Hook callback { self .func_name } () does not accept keyword argument '{ name } '"
169
163
) from err
170
164
171
165
raise err
@@ -174,10 +168,6 @@ def __call__(self, *args, **kw):
174
168
class BasePlugin :
175
169
"""Base class for all plugins."""
176
170
177
- provided_events : Optional [Set [EventIdenifier ]] = None
178
-
179
- dependencies : Iterable [Type ["BasePlugin" ]] = ()
180
-
181
171
def __init__ (self ):
182
172
"""Initialize the base plugin."""
183
173
self ._hook_handles : List [HookHandle ] = []
@@ -190,23 +180,6 @@ def attach_to(self, pluggable: Pluggable):
190
180
191
181
self ._pluggable = pluggable
192
182
193
- for dep in self .dependencies :
194
- dep_satisfied = False
195
-
196
- for plugin in pluggable .plugins :
197
- if isinstance (plugin , dep ):
198
- # there is already a plugin which satisfies this dependency
199
- dep_satisfied = True
200
- break
201
-
202
- if not dep_satisfied :
203
- # create a plugin of this type and attach it to the trainer
204
- dep_plugin = dep ()
205
- dep_plugin .attach_to (pluggable )
206
-
207
- if self .provided_events is not None and pluggable .valid_events is not None :
208
- pluggable .valid_events = pluggable .valid_events | self .provided_events
209
-
210
183
pluggable .append_plugin (self )
211
184
212
185
# go through all attributes
0 commit comments