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