re-add autostart plugins
[enigma2.git] / lib / python / Components / PluginComponent.py
1 import os
2
3 from Tools.Directories import *
4 from Plugins.Plugin import PluginDescriptor
5
6 def my_import(name):
7         mod = __import__(name)
8         components = name.split('.')
9         for comp in components[1:]:
10                 mod = getattr(mod, comp)
11         return mod
12
13 class PluginComponent:
14         def __init__(self):
15                 self.plugins = {}
16                 self.pluginList = [ ]
17                 self.setPluginPrefix("Plugins.")
18                 
19         def setPluginPrefix(self, prefix):
20                 self.prefix = prefix
21         
22         def addPlugin(self, plugin):
23                 self.pluginList.append(plugin)
24                 for x in plugin.where:
25                         self.plugins.setdefault(x, []).append(plugin)
26                         if x == PluginDescriptor.WHERE_AUTOSTART:
27                                 plugin(reason=0)
28         
29         def removePlugin(self, plugin):
30                 self.pluginList.remove(plugin)
31                 for x in plugin.where:
32                         self.plugins[x].remove(plugin)
33                         if x == PluginDescriptor.WHERE_AUTOSTART:
34                                 plugin(reason=1)
35         
36         def readPluginList(self):
37                 """enumerates plugins"""
38
39                 directories = os.listdir(resolveFilename(SCOPE_PLUGINS))
40                 
41                 for x in directories:
42                         path = resolveFilename(SCOPE_PLUGINS, x) + "/"
43                         if os.path.exists(path):
44                                 if fileExists(path + "plugin.py"):
45                                         plugin = my_import('.'.join(("Plugins", x, "plugin")))
46                                         
47                                         if not plugin.__dict__.has_key("Plugins"):
48                                                 print "Plugin %s doesn't have 'Plugin'-call." % (x)
49                                                 continue
50                                         
51                                         print "plugin", plugin
52                                         plugins = plugin.Plugins()
53                                         
54                                         # allow single entry not to be a list
55                                         if type(plugins) is not list:
56                                                 plugins = [ plugins ]
57                                         
58                                         for p in plugins:
59                                                 self.addPlugin(p);
60
61         def getPlugins(self, where):
62                 """Get list of plugins in a specific category"""
63                 
64                 if type(where) is not list:
65                         where = [ where ]
66                 res = [ ]
67                 for x in where:
68                         for p in self.plugins.get(x, [ ]):
69                                 res.append(p)
70                 return res
71
72         def shutdown(self):
73                 for p in self.pluginList[:]:
74                         self.removePlugin(p)
75
76 plugins = PluginComponent()