4356456cb034c26a4312c433abebcf726c0b19b3
[enigma2.git] / lib / python / Components / PluginComponent.py
1 import os
2 import traceback
3
4 from Tools.Directories import *
5 from Tools.Import import my_import
6 from Plugins.Plugin import PluginDescriptor
7
8 class PluginComponent:
9         def __init__(self):
10                 self.plugins = {}
11                 self.pluginList = [ ]
12                 self.setPluginPrefix("Plugins.")
13                 
14         def setPluginPrefix(self, prefix):
15                 self.prefix = prefix
16         
17         def addPlugin(self, plugin):
18                 self.pluginList.append(plugin)
19                 for x in plugin.where:
20                         self.plugins.setdefault(x, []).append(plugin)
21                         if x == PluginDescriptor.WHERE_AUTOSTART:
22                                 plugin(reason=0)
23         
24         def removePlugin(self, plugin):
25                 self.pluginList.remove(plugin)
26                 for x in plugin.where:
27                         self.plugins[x].remove(plugin)
28                         if x == PluginDescriptor.WHERE_AUTOSTART:
29                                 plugin(reason=1)
30         
31         def readPluginList(self, directory):
32                 """enumerates plugins"""
33                 
34                 categories = os.listdir(directory)
35                 
36                 new_plugins = [ ]
37                 
38                 for c in categories:
39                         directory_category = directory + c
40                         if not os.path.isdir(directory_category):
41                                 continue
42                         open(directory_category + "/__init__.py", "a").close()
43                         for x in os.listdir(directory_category):
44                                 path = directory_category + "/" + x
45                                 if os.path.isdir(path):
46                                         if fileExists(path + "/plugin.pyc") or fileExists(path + "/plugin.py"):
47                                                 try:
48                                                         plugin = my_import('.'.join(["Plugins", c, x, "plugin"]))
49
50                                                         if not plugin.__dict__.has_key("Plugins"):
51                                                                 print "Plugin %s doesn't have 'Plugin'-call." % (x)
52                                                                 continue
53
54                                                         plugins = plugin.Plugins(path=path)
55                                                 except Exception, exc:
56                                                         print "Plugin ", path, "failed to load:", exc
57                                                         traceback.print_exc(file=sys.stdout)
58                                                         print "skipping plugin."
59                                                         continue
60
61                                                 # allow single entry not to be a list
62                                                 if type(plugins) is not list:
63                                                         plugins = [ plugins ]
64
65                                                 for p in plugins:
66                                                         p.updateIcon(path)
67                                                         new_plugins.append(p)
68                 
69                 # build a diff between the old list of plugins and the new one
70                 # internally, the "fnc" argument will be compared with __eq__
71                 plugins_added = [p for p in new_plugins if p not in self.pluginList]
72                 plugins_removed = [p for p in self.pluginList if p not in new_plugins]
73                 
74                 for p in plugins_removed:
75                         self.removePlugin(p)
76                 
77                 for p in plugins_added:
78                         self.addPlugin(p)
79
80         def getPlugins(self, where):
81                 """Get list of plugins in a specific category"""
82                 
83                 if type(where) is not list:
84                         where = [ where ]
85                 res = [ ]
86                 for x in where:
87                         for p in self.plugins.get(x, [ ]):
88                                 res.append(p)
89                 return res
90
91         def getPluginsForMenu(self, menuid):
92                 res = [ ]
93                 for p in self.getPlugins(PluginDescriptor.WHERE_SETUP):
94                         res += p(menuid)
95                 return res
96
97         def clearPluginList(self):
98                 self.pluginList = []
99                 self.plugins = {}
100
101         def shutdown(self):
102                 for p in self.pluginList[:]:
103                         self.removePlugin(p)
104
105 plugins = PluginComponent()