Merge remote branch 'origin/acid-burn/bug_687_fix_wrong_polish_encodings'
[vuplus_dvbapp] / lib / python / Components / PluginComponent.py
1 from os import path as os_path, listdir as os_listdir
2 from traceback import print_exc
3 from sys import stdout
4
5 from Tools.Directories import fileExists
6 from Tools.Import import my_import
7 from Plugins.Plugin import PluginDescriptor
8 import keymapparser
9
10 class PluginComponent:
11         firstRun = True
12         restartRequired = False
13         
14         def __init__(self):
15                 self.plugins = {}
16                 self.pluginList = [ ]
17                 self.setPluginPrefix("Plugins.")
18                 self.resetWarnings()
19
20         def setPluginPrefix(self, prefix):
21                 self.prefix = prefix
22
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:
29                                         plugin(reason=0)
30                 else:
31                         self.restartRequired = True
32                                 
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:
38                                 plugin(reason=1)
39
40         def readPluginList(self, directory):
41                 """enumerates plugins"""
42
43                 categories = os_listdir(directory)
44
45                 new_plugins = [ ]
46
47                 for c in categories:
48                         directory_category = directory + c
49                         if not os_path.isdir(directory_category):
50                                 continue
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"):
56                                                 try:
57                                                         plugin = my_import('.'.join(["Plugins", c, pluginname, "plugin"]))
58
59                                                         if not plugin.__dict__.has_key("Plugins"):
60                                                                 print "Plugin %s doesn't have 'Plugin'-call." % (pluginname)
61                                                                 continue
62
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)) )
69                                                         continue
70
71                                                 # allow single entry not to be a list
72                                                 if not isinstance(plugins, list):
73                                                         plugins = [ plugins ]
74
75                                                 for p in plugins:
76                                                         p.updateIcon(path)
77                                                         new_plugins.append(p)
78
79                                                 if fileExists(path + "/keymap.xml"):
80                                                         try:
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)) )
85
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]
90                 
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
96
97                 for p in plugins_removed:
98                         self.removePlugin(p)
99
100                 for p in plugins_added:
101                         self.addPlugin(p)
102                 
103                 if self.firstRun:
104                         self.firstRun = False
105
106         def getPlugins(self, where):
107                 """Get list of plugins in a specific category"""
108
109                 if not isinstance(where, list):
110                         where = [ where ]
111                 res = [ ]
112
113                 for x in where:
114                         res.extend(self.plugins.get(x, [ ]))
115
116                 return  res
117
118         def getPluginsForMenu(self, menuid):
119                 res = [ ]
120                 for p in self.getPlugins(PluginDescriptor.WHERE_MENU):
121                         res += p(menuid)
122                 return res
123
124         def clearPluginList(self):
125                 self.pluginList = []
126                 self.plugins = {}
127                 self.firstRun = True
128                 self.restartRequired = False
129
130         def shutdown(self):
131                 for p in self.pluginList[:]:
132                         self.removePlugin(p)
133
134         def resetWarnings(self):
135                 self.warnings = [ ]
136
137         def getNextWakeupTime(self):
138                 wakeup = -1
139                 for p in self.pluginList:
140                         current = p.getWakeupTime()
141                         if current > -1 and (wakeup > current or wakeup == -1):
142                                 wakeup = current
143                 return int(wakeup)
144
145 plugins = PluginComponent()