@@ -52,6 +52,7 @@ def __init__(self, factory, base_class=BasePlugin):
5252
5353 :param base_class: The base class to use while searching for plugins.
5454 """
55+ self .packets = {}
5556 self .plugins = {}
5657 self .plugin_classes = {}
5758 self .plugins_waiting_to_load = {}
@@ -216,6 +217,7 @@ def activate_plugins(self, plugins, dependencies):
216217 for p in plugin_deps :
217218 self .plugin_classes [plugin .name ].plugins [p .name ] = p
218219 self .plugins [plugin .name ].activate ()
220+ self .map_plugin_packets (plugin )
219221 except FatalPluginError as e :
220222 self .logger .critical (
221223 'A plugin reported a fatal error. Error: %s' , str (e )
@@ -231,8 +233,9 @@ def deactivate_plugins(self):
231233 'A plugin reported a fatal error. Error: %s' , str (e )
232234 )
233235 raise
236+ self .de_map_plugin_packets (plugin )
234237
235- def do (self , protocol , command , data ):
238+ def do (self , protocol , when , data ):
236239 """
237240 Runs a command across all currently loaded plugins.
238241
@@ -247,27 +250,45 @@ def do(self, protocol, command, data):
247250 return True
248251
249252 return_values = []
250- plugins = (
251- plugin for plugin in self .plugins .itervalues ()
252- if plugin .active and data .id in plugin .override_packets
253- )
254- for plugin in plugins :
253+ packets = self .packets .get (data .id , {}).get (when , {}).itervalues ()
254+ for plugin , packet_method in packets :
255255 try :
256256 plugin .protocol = protocol
257- res = getattr ( plugin , command , lambda _ : True ) (data )
257+ res = packet_method (data )
258258 if res is None :
259259 res = True
260260 return_values .append (res )
261261 except :
262262 self .logger .exception (
263263 'Error in plugin %s with function %s.' ,
264- str (plugin ), command
264+ str (plugin ), packet_method . __name__
265265 )
266266 return all (return_values )
267267
268268 def die (self ):
269269 self .deactivate_plugins ()
270270
271+ def map_plugin_packets (self , plugin ):
272+ """
273+ Maps plugin overridden packets ready to use in do method.
274+ """
275+ for packet_id , when_dict in plugin .override_packets .iteritems ():
276+ for when , packet_method in when_dict .iteritems ():
277+ self .packets .setdefault (
278+ packet_id , {}
279+ ).setdefault (
280+ when , {}
281+ )[plugin .name ] = (plugin , packet_method )
282+
283+ def de_map_plugin_packets (self , plugin ):
284+ """
285+ Removes plugin overridden packets method from packets dictionary.
286+ """
287+ for packet_id , when_dict in self .packets .iteritems ():
288+ for when , plugins in when_dict .iteritems ():
289+ if plugin .name in plugins :
290+ plugins .pop (plugin .name )
291+
271292
272293def route (func ):
273294 """
@@ -276,18 +297,15 @@ def route(func):
276297 logger = logging .getLogger ('starrypy.plugin_manager.route' )
277298
278299 def wrapped_function (self , data ):
279- name = func .__name__
280- on = 'on_%s' % name
281- after = 'after_%s' % name
282- res = self .plugin_manager .do (self , on , data )
300+ res = self .plugin_manager .do (self , 'on' , data )
283301 if res :
284302 res = func (self , data )
285303 d = deferLater (
286304 reactor ,
287305 .01 ,
288306 self .plugin_manager .do ,
289307 self ,
290- after ,
308+ ' after' ,
291309 data
292310 )
293311 d .addErrback (print_this_defered_failure )
0 commit comments