fix cancel handling
[enigma2.git] / lib / python / Components / PluginComponent.py
1 import os
2
3 from Tools.Directories import *
4 from Screens.Menu import menuupdater
5
6 class PluginComponent:
7         def __init__(self):
8                 self.plugins = []
9                 self.setPluginPrefix("Plugins.")
10                 self.menuEntries = []
11                 
12         def setPluginPrefix(self, prefix):
13                 self.prefix = prefix
14
15         def getPluginList(self, runAutostartPlugins=False, runAutoendPlugins=False):
16                 list = []
17                 dir = os.listdir(resolveFilename(SCOPE_PLUGINS))
18                 self.menuDelete()
19                 self.menuEntries = []
20
21                 for x in dir:
22                         path = resolveFilename(SCOPE_PLUGINS, x) + "/"
23                         try:
24                                 if os.path.exists(path):
25                                         if fileExists(path + "plugin.py"):
26                                                 pluginmodule = self.prefix + x + ".plugin"
27                                                 print "trying to import " + pluginmodule
28                                                 exec "import " + pluginmodule
29                                                 plugin = eval(pluginmodule)
30                                                 plugins = plugin.getPlugins()
31                                                 try: picturepaths = plugin.getPicturePaths()
32                                                 except:
33                                                         picturepaths = []
34                                                         for p in plugins:
35                                                                 picturepaths.append("")
36                                                 try:
37                                                         for menuEntry in plugin.getMenuRegistrationList():
38                                                                 self.menuEntries.append([menuEntry, pluginmodule])
39                                                 except:
40                                                         pass
41         
42                                                 for y in range(len(plugins)):
43                                                         if len(plugins[y]) < 5:
44                                                                 list.append((path + picturepaths[y], plugins[y][0] , x, plugins[y][2], plugins[y][3], None, plugins[y][1]))
45                                                         else:
46                                                                 list.append((path + picturepaths[y], plugins[y][0] , x, plugins[y][2], plugins[y][3], plugins[y][4], plugins[y][1]))
47                                                 if runAutostartPlugins:
48                                                         try: plugin.autostart()
49                                                         except: pass
50                                                 if runAutoendPlugins:
51                                                         try: plugin.autoend()
52                                                         except: pass
53                         except:
54                                 print "Directory", path, "contains a faulty plugin"
55                 self.menuUpdate()
56                 return list
57         
58         def menuDelete(self):
59                 for menuEntry in self.menuEntries:
60                         menuupdater.delMenuItem(menuEntry[0][0], menuEntry[0][1], menuEntry[0][2], menuEntry[1], menuEntry[0][3])
61
62         def menuUpdate(self):
63                 for menuEntry in self.menuEntries:
64                         menuupdater.addMenuItem(menuEntry[0][0], menuEntry[0][1], menuEntry[0][2], menuEntry[1], menuEntry[0][3])
65         
66         def runPlugin(self, plugin, session):
67                 try:
68                         exec("import " + self.prefix + plugin[2] + ".plugin")
69                         if plugin[3] == "screen":
70                                 session.open(eval(self.prefix + plugin[2] + ".plugin." + plugin[4]), plugin[5])
71                         elif plugin[3] == "function":
72                                 eval(self.prefix + plugin[2] + ".plugin." + plugin[4])(session, plugin[5])
73                 except:
74                         print "exec of plugin failed!"
75
76 plugins = PluginComponent()