@@ -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 = {}
@@ -65,6 +66,7 @@ def __init__(self, factory, base_class=BasePlugin):
6566 self .plugin_dir = path .child (self .config .plugin_path )
6667 sys .path .append (self .plugin_dir .path )
6768
69+ def prepare (self ):
6870 self .load_plugins (
6971 [
7072 'core.admin_commands_plugin' ,
@@ -87,23 +89,18 @@ def __init__(self, factory, base_class=BasePlugin):
8789
8890 def installed_plugins (self ):
8991 """
90- Get list of all plugins in the plugin_dir.
91-
92- :param name: None
93- :return: Array of plugin names.
92+ Returns list of all plugins in the plugin_dir.
9493 """
95- installed_plugins = filter (
96- None ,
94+ return filter (
95+ lambda name : not ( name is None or name == 'core' ) ,
9796 (
98- self .get_plugin_name_from_file (f )
99- for f in self .plugin_dir .globChildren ('*' )
97+ self .get_plugin_name_from_file (plugin_file )
98+ for plugin_file in self .plugin_dir .globChildren ('*' )
10099 )
101100 )
102101
103- # don't list core as a plugin
104- return filter (lambda name : name != 'core' , installed_plugins )
105-
106- def get_plugin_name_from_file (self , f ):
102+ @staticmethod
103+ def get_plugin_name_from_file (f ):
107104 if f .isdir ():
108105 return f .basename ()
109106 else :
@@ -114,7 +111,6 @@ def import_plugin(self, name):
114111 Import plugin that has the given name, and is a subclass of base_class.
115112
116113 :param name: The name of the plugin to import.
117- :return: None
118114 """
119115 try :
120116 mod = __import__ (name , globals (), locals (), [], 0 )
@@ -221,6 +217,7 @@ def activate_plugins(self, plugins, dependencies):
221217 for p in plugin_deps :
222218 self .plugin_classes [plugin .name ].plugins [p .name ] = p
223219 self .plugins [plugin .name ].activate ()
220+ self .map_plugin_packets (plugin )
224221 except FatalPluginError as e :
225222 self .logger .critical (
226223 'A plugin reported a fatal error. Error: %s' , str (e )
@@ -236,8 +233,9 @@ def deactivate_plugins(self):
236233 'A plugin reported a fatal error. Error: %s' , str (e )
237234 )
238235 raise
236+ self .de_map_plugin_packets (plugin )
239237
240- def do (self , protocol , command , data ):
238+ def do (self , protocol , when , data ):
241239 """
242240 Runs a command across all currently loaded plugins.
243241
@@ -248,28 +246,52 @@ def do(self, protocol, command, data):
248246 :return: Whether or not all plugins returned True or None.
249247 :rtype: bool
250248 """
251- return_values = []
252249 if protocol is None :
253250 return True
254- for plugin in self .plugins .itervalues ():
251+
252+ return_values = []
253+ packets = self .packets .get (data .id , {}).get (when , {}).itervalues ()
254+ for plugin , packet_method in packets :
255255 try :
256- if not plugin .active :
257- continue
258256 plugin .protocol = protocol
259- res = getattr (plugin , command , lambda _ : True )(data )
260- if res is None :
257+ res = packet_method (data )
258+ if res is False :
259+ return False
260+ elif res is None :
261261 res = True
262+
262263 return_values .append (res )
263264 except :
264265 self .logger .exception (
265266 'Error in plugin %s with function %s.' ,
266- str (plugin ), command
267+ str (plugin ), packet_method . __name__
267268 )
268269 return all (return_values )
269270
270271 def die (self ):
271272 self .deactivate_plugins ()
272273
274+ def map_plugin_packets (self , plugin ):
275+ """
276+ Maps plugin overridden packets ready to use in do method.
277+ """
278+ for packet_id , when_dict in plugin .overridden_methods .iteritems ():
279+ for when , packet_method in when_dict .iteritems ():
280+ self .packets .setdefault (
281+ packet_id , {}
282+ ).setdefault (
283+ when , {}
284+ )[plugin .name ] = (plugin , packet_method )
285+
286+ def de_map_plugin_packets (self , plugin ):
287+ """
288+ Removes plugin overridden packets method from packets dictionary.
289+ """
290+ for packet_id , when_dict in self .packets .iteritems ():
291+ for when , plugins in when_dict .iteritems ():
292+ if plugin .name in plugins :
293+ plugins .pop (plugin .name )
294+
273295
274296def route (func ):
275297 """
@@ -278,18 +300,15 @@ def route(func):
278300 logger = logging .getLogger ('starrypy.plugin_manager.route' )
279301
280302 def wrapped_function (self , data ):
281- name = func .__name__
282- on = 'on_%s' % name
283- after = 'after_%s' % name
284- res = self .plugin_manager .do (self , on , data )
303+ res = self .plugin_manager .do (self , 'on' , data )
285304 if res :
286305 res = func (self , data )
287306 d = deferLater (
288307 reactor ,
289308 .01 ,
290309 self .plugin_manager .do ,
291310 self ,
292- after ,
311+ ' after' ,
293312 data
294313 )
295314 d .addErrback (print_this_defered_failure )
0 commit comments