1 from os import path as os_path, listdir as os_listdir
2 from traceback import print_exc
5 from Tools.Directories import fileExists
6 from Tools.Import import my_import
7 from Plugins.Plugin import PluginDescriptor
10 class PluginComponent:
14 self.setPluginPrefix("Plugins.")
17 def setPluginPrefix(self, prefix):
20 def addPlugin(self, plugin):
21 self.pluginList.append(plugin)
22 for x in plugin.where:
23 self.plugins.setdefault(x, []).append(plugin)
24 if x == PluginDescriptor.WHERE_AUTOSTART:
27 def removePlugin(self, plugin):
28 self.pluginList.remove(plugin)
29 for x in plugin.where:
30 self.plugins[x].remove(plugin)
31 if x == PluginDescriptor.WHERE_AUTOSTART:
34 def readPluginList(self, directory):
35 """enumerates plugins"""
37 categories = os_listdir(directory)
42 directory_category = directory + c
43 if not os_path.isdir(directory_category):
45 open(directory_category + "/__init__.py", "a").close()
46 for pluginname in os_listdir(directory_category):
47 path = directory_category + "/" + pluginname
48 if os_path.isdir(path):
49 if fileExists(path + "/plugin.pyc") or fileExists(path + "/plugin.py"):
51 plugin = my_import('.'.join(["Plugins", c, pluginname, "plugin"]))
53 if not plugin.__dict__.has_key("Plugins"):
54 print "Plugin %s doesn't have 'Plugin'-call." % (pluginname)
57 plugins = plugin.Plugins(path=path)
58 except Exception, exc:
59 print "Plugin ", c + "/" + pluginname, "failed to load:", exc
60 print_exc(file=stdout)
61 print "skipping plugin."
62 self.warnings.append( (c + "/" + pluginname, str(exc)) )
65 # allow single entry not to be a list
66 if type(plugins) is not list:
73 if fileExists(path + "/keymap.xml"):
75 keymapparser.readKeymap(path + "/keymap.xml")
76 except Exception, exc:
77 print "keymap for plugin %s/%s failed to load: " % (c, pluginname), exc
78 self.warnings.append( (c + "/" + pluginname, str(exc)) )
80 # build a diff between the old list of plugins and the new one
81 # internally, the "fnc" argument will be compared with __eq__
82 plugins_added = [p for p in new_plugins if p not in self.pluginList]
83 plugins_removed = [p for p in self.pluginList if p not in new_plugins]
85 for p in plugins_removed:
88 for p in plugins_added:
91 def getPlugins(self, where):
92 """Get list of plugins in a specific category"""
94 if type(where) is not list:
98 for p in self.plugins.get(x, [ ]):
102 def getPluginsForMenu(self, menuid):
104 for p in self.getPlugins(PluginDescriptor.WHERE_SETUP):
108 def clearPluginList(self):
113 for p in self.pluginList[:]:
116 def resetWarnings(self):
119 plugins = PluginComponent()