1313from config import ConfigurationManager
1414from utility_functions import path
1515
16+
1617class DuplicatePluginError (Exception ):
1718 """
1819 Raised when there is a plugin of the same name/class already instantiated.
@@ -34,7 +35,8 @@ class MissingDependency(PluginNotFound):
3435
3536class UnresolvedOrCircularDependencyError (Exception ):
3637 """
37- Raised whenever there is a circular dependency detected in the loading of of plugins.
38+ Raised whenever there is a circular dependency detected in the loading
39+ of of plugins.
3840 """
3941
4042
@@ -61,13 +63,27 @@ def __init__(self, factory, base_class=BasePlugin):
6163 self .factory = factory
6264
6365 self .plugin_dir = path .child (self .config .plugin_path )
64- sys .path .append ( self .plugin_dir .path )
65-
66- self .load_plugins ( ['core.admin_commands_plugin' ,'core.colored_names' ,'core.command_plugin' ,'core.player_manager_plugin' ,'core.starbound_config_manager' ] )
67- self .load_plugins ( self .config .config ['initial_plugins' ] )
68- self .logger .info ( "Loaded plugins:\n \n %s\n " % "\n " .join (
69- ["\t %s" % (plugin .name ) for plugin in self .plugins .itervalues ()]) )
70-
66+ sys .path .append (self .plugin_dir .path )
67+
68+ self .load_plugins (
69+ [
70+ 'core.admin_commands_plugin' ,
71+ 'core.colored_names' ,
72+ 'core.command_plugin' ,
73+ 'core.player_manager_plugin' ,
74+ 'core.starbound_config_manager'
75+ ]
76+ )
77+ self .load_plugins (self .config .config ['initial_plugins' ])
78+ self .logger .info (
79+ 'Loaded plugins:\n \n %s\n ' ,
80+ '\n ' .join (
81+ [
82+ '\t {}' .format (plugin .name )
83+ for plugin in self .plugins .itervalues ()
84+ ]
85+ )
86+ )
7187
7288 def installed_plugins (self ):
7389 """
@@ -76,17 +92,23 @@ def installed_plugins(self):
7692 :param name: None
7793 :return: Array of plugin names.
7894 """
79- installed_plugins = filter (None , [ self .get_plugin_name_from_file (f ) for f in self .plugin_dir .globChildren ("*" ) ])
80- return filter (lambda name : name != 'core' , installed_plugins ) # don't list core as a plugin
95+ installed_plugins = filter (
96+ None ,
97+ (
98+ self .get_plugin_name_from_file (f )
99+ for f in self .plugin_dir .globChildren ('*' )
100+ )
101+ )
81102
103+ # don't list core as a plugin
104+ return filter (lambda name : name != 'core' , installed_plugins )
82105
83106 def get_plugin_name_from_file (self , f ):
84107 if f .isdir ():
85108 return f .basename ()
86109 else :
87110 return
88111
89-
90112 def import_plugin (self , name ):
91113 """
92114 Import plugin that has the given name, and is a subclass of base_class.
@@ -97,18 +119,23 @@ def import_plugin(self, name):
97119 try :
98120 mod = __import__ (name , globals (), locals (), [], 0 )
99121 for _ , plugin in inspect .getmembers (mod , inspect .isclass ):
100- if issubclass (plugin , self .base_class ) and (plugin is not self .base_class ) and (plugin not in self .plugin_classes .keys ()):
122+ if (
123+ issubclass (plugin , self .base_class ) and
124+ (plugin is not self .base_class ) and
125+ (plugin not in self .plugin_classes .iterkeys ())
126+ ):
101127 plugin .config = self .config
102128 plugin .factory = self .factory
103129 plugin .active = False
104130 plugin .protocol = None
105131 plugin .plugins = {}
106- plugin .logger = logging .getLogger ('starrypy.plugins.%s' % plugin .name )
132+ plugin .logger = logging .getLogger (
133+ 'starrypy.plugins.{}' .format (plugin .name )
134+ )
107135 self .plugin_classes [plugin .name ] = plugin
108136
109137 except ImportError :
110- self .logger .critical ("Import error for %s\n " % name )
111-
138+ self .logger .critical ('Import error for %s\n ' , name )
112139
113140 def resolve_dependencies (self , dependency_hash ):
114141 """
@@ -122,26 +149,41 @@ def resolve_dependencies(self, dependency_hash):
122149
123150 try :
124151 while len (dependency_hash ) > 0 :
125- ready = [x for x , d in dependency_hash .iteritems () if len (d ) == 0 ]
126- if len (ready ) == 0 :
152+ ready = [
153+ x for x , d in dependency_hash .iteritems () if len (d ) == 0
154+ ]
155+ if ready :
127156 ex = []
128157 for n , d in dependency_hash .iteritems ():
129158 for dep in d :
130- ex .append ("Dependency of %s on %s not met\n " % (n , dep ))
159+ ex .append (
160+ 'Dependency of {} on {} not met\n ' .format (
161+ n , dep
162+ )
163+ )
131164 raise UnresolvedOrCircularDependencyError (
132- "Unresolved or circular dependencies found:\n %s" % "\n " .join (ex ))
165+ 'Unresolved or circular dependencies'
166+ ' found:\n {}' .format ('\n ' .join (ex ))
167+ )
133168 for name in ready :
134- self .plugins_waiting_to_load [name ] = self .plugin_classes [name ]
169+ self .plugins_waiting_to_load [name ] = (
170+ self .plugin_classes [name ]
171+ )
135172 self .load_order .append (name )
136- del ( dependency_hash [name ] )
173+ del dependency_hash [name ]
137174 for name , depends in dependency_hash .iteritems ():
138- plugin_set = set (self .plugins .iterkeys ()).union ( set (self .plugins_waiting_to_load .iterkeys ()) )
139- dependency_hash [name ] = dependency_hash [name ].difference (plugin_set )
175+ plugin_set = set (
176+ self .plugins .iterkeys ()
177+ ).union (
178+ set (self .plugins_waiting_to_load .iterkeys ())
179+ )
180+ dependency_hash [name ] = dependency_hash [name ].difference (
181+ plugin_set
182+ )
140183
141184 except UnresolvedOrCircularDependencyError as e :
142185 self .logger .critical (str (e ))
143186
144-
145187 def load_plugins (self , plugins_to_load ):
146188 """
147189 Loads and instantiates plugins that it is asked to.
@@ -153,40 +195,48 @@ def load_plugins(self, plugins_to_load):
153195 for plugin in plugins_to_load :
154196 self .import_plugin (plugin )
155197
156- dependencies = { plugin .name : set (plugin .depends ) for plugin in self .plugin_classes .itervalues () }
198+ dependencies = {
199+ plugin .name : set (plugin .depends )
200+ for plugin in self .plugin_classes .itervalues ()
201+ }
157202
158- self .resolve_dependencies ( dependencies .copy () )
203+ self .resolve_dependencies (dependencies .copy ())
159204
160205 new_plugins = []
161206 for plugin_name in self .load_order :
162207 if not self .plugins .get (plugin_name , False ):
163208 new_plugins .append (plugin_name )
164209
165- self .activate_plugins ( new_plugins , dependencies )
166-
210+ self .activate_plugins (new_plugins , dependencies )
167211
168212 def activate_plugins (self , plugins , dependencies ):
169- for plugin in [ self .plugins_waiting_to_load [x ] for x in plugins ] :
213+ for plugin in ( self .plugins_waiting_to_load [x ] for x in plugins ) :
170214 try :
171215 self .plugins [plugin .name ] = plugin ()
172- self .logger .debug (" Instantiated plugin '%s'" % plugin .name )
216+ self .logger .debug (' Instantiated plugin "%s"' , plugin .name )
173217 if len (plugin .depends ) > 0 :
174- for p in [ self .plugins [x ] for x in dependencies [plugin .name ] ]:
218+ plugin_deps = (
219+ self .plugins [x ] for x in dependencies [plugin .name ]
220+ )
221+ for p in plugin_deps :
175222 self .plugin_classes [plugin .name ].plugins [p .name ] = p
176223 self .plugins [plugin .name ].activate ()
177224 except FatalPluginError as e :
178- self .logger .critical ("A plugin reported a fatal error. Error: %s" , str (e ))
225+ self .logger .critical (
226+ 'A plugin reported a fatal error. Error: %s' , str (e )
227+ )
179228 raise
180229
181230 def deactivate_plugins (self ):
182- for plugin in [ self .plugins [x ] for x in reversed (self .load_order )] :
231+ for plugin in ( self .plugins [x ] for x in reversed (self .load_order )) :
183232 try :
184233 plugin .deactivate ()
185234 except FatalPluginError as e :
186- self .logger .critical ("A plugin reported a fatal error. Error: %s" , str (e ))
235+ self .logger .critical (
236+ 'A plugin reported a fatal error. Error: %s' , str (e )
237+ )
187238 raise
188239
189-
190240 def do (self , protocol , command , data ):
191241 """
192242 Runs a command across all currently loaded plugins.
@@ -211,10 +261,12 @@ def do(self, protocol, command, data):
211261 res = True
212262 return_values .append (res )
213263 except :
214- self .logger .exception ("Error in plugin %s with function %s." , str (plugin ), command )
264+ self .logger .exception (
265+ 'Error in plugin %s with function %s.' ,
266+ str (plugin ), command
267+ )
215268 return all (return_values )
216269
217-
218270 def die (self ):
219271 self .deactivate_plugins ()
220272
@@ -227,17 +279,24 @@ def route(func):
227279
228280 def wrapped_function (self , data ):
229281 name = func .__name__
230- on = " on_%s" % name
231- after = " after_%s" % name
282+ on = ' on_%s' % name
283+ after = ' after_%s' % name
232284 res = self .plugin_manager .do (self , on , data )
233285 if res :
234286 res = func (self , data )
235- d = deferLater (reactor , .01 , self .plugin_manager .do , self , after , data )
287+ d = deferLater (
288+ reactor ,
289+ .01 ,
290+ self .plugin_manager .do ,
291+ self ,
292+ after ,
293+ data
294+ )
236295 d .addErrback (print_this_defered_failure )
237296 return res
238297
239298 def print_this_defered_failure (f ):
240- logger .error (" Deferred function failure. %s" , f )
299+ logger .error (' Deferred function failure. %s' , f )
241300
242301 return wrapped_function
243302
0 commit comments