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:
12 restartRequired = False
17 self.setPluginPrefix("Plugins.")
20 def setPluginPrefix(self, prefix):
23 def addPlugin(self, plugin):
24 if self.firstRun or plugin.needsRestart is False:
25 self.pluginList.append(plugin)
26 for x in plugin.where:
27 self.plugins.setdefault(x, []).append(plugin)
28 if x == PluginDescriptor.WHERE_AUTOSTART:
31 self.restartRequired = True
33 def removePlugin(self, plugin):
34 self.pluginList.remove(plugin)
35 for x in plugin.where:
36 self.plugins[x].remove(plugin)
37 if x == PluginDescriptor.WHERE_AUTOSTART:
40 def readPluginList(self, directory):
41 """enumerates plugins"""
43 categories = os_listdir(directory)
48 directory_category = directory + c
49 if not os_path.isdir(directory_category):
51 open(directory_category + "/__init__.py", "a").close()
52 for pluginname in os_listdir(directory_category):
53 path = directory_category + "/" + pluginname
54 if os_path.isdir(path):
55 if fileExists(path + "/plugin.pyc") or fileExists(path + "/plugin.pyo") or fileExists(path + "/plugin.py"):
57 plugin = my_import('.'.join(["Plugins", c, pluginname, "plugin"]))
59 if not plugin.__dict__.has_key("Plugins"):
60 print "Plugin %s doesn't have 'Plugin'-call." % (pluginname)
63 plugins = plugin.Plugins(path=path)
64 except Exception, exc:
65 print "Plugin ", c + "/" + pluginname, "failed to load:", exc
66 print_exc(file=stdout)
67 print "skipping plugin."
68 self.warnings.append( (c + "/" + pluginname, str(exc)) )
71 # allow single entry not to be a list
72 if not isinstance(plugins, list):
79 if fileExists(path + "/keymap.xml"):
81 keymapparser.readKeymap(path + "/keymap.xml")
82 except Exception, exc:
83 print "keymap for plugin %s/%s failed to load: " % (c, pluginname), exc
84 self.warnings.append( (c + "/" + pluginname, str(exc)) )
86 # build a diff between the old list of plugins and the new one
87 # internally, the "fnc" argument will be compared with __eq__
88 plugins_added = [p for p in new_plugins if p not in self.pluginList]
89 plugins_removed = [p for p in self.pluginList if not p.internal and p not in new_plugins]
91 #ignore already installed but reloaded plugins
92 for p in plugins_removed:
93 for pa in plugins_added:
94 if pa.name == p.name and pa.where == p.where:
95 pa.needsRestart = False
97 for p in plugins_removed:
100 for p in plugins_added:
104 self.firstRun = False
106 def getPlugins(self, where):
107 """Get list of plugins in a specific category"""
109 if not isinstance(where, list):
114 res.extend(self.plugins.get(x, [ ]))
118 def getPluginsForMenu(self, menuid):
120 for p in self.getPlugins(PluginDescriptor.WHERE_MENU):
124 def clearPluginList(self):
128 self.restartRequired = False
131 for p in self.pluginList[:]:
134 def resetWarnings(self):
137 def getNextWakeupTime(self):
139 for p in self.pluginList:
140 current = p.getWakeupTime()
141 if current > -1 and (wakeup > current or wakeup == -1):
145 plugins = PluginComponent()