some changes
[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         print name
8         mod = __import__(name)
9         components = name.split('.')
10         for comp in components[1:]:
11                 mod = getattr(mod, comp)
12         return mod
13
14 class PluginComponent:
15         def __init__(self):
16                 self.plugins = {}
17                 self.pluginList = [ ]
18                 self.setPluginPrefix("Plugins.")
19                 
20         def setPluginPrefix(self, prefix):
21                 self.prefix = prefix
22         
23         def addPlugin(self, plugin):
24                 self.pluginList.append(plugin)
25                 for x in plugin.where:
26                         self.plugins.setdefault(x, []).append(plugin)
27                         if x == PluginDescriptor.WHERE_AUTOSTART:
28                                 plugin(reason=0)
29         
30         def removePlugin(self, plugin):
31                 self.pluginList.remove(plugin)
32                 for x in plugin.where:
33                         self.plugins[x].remove(plugin)
34                         if x == PluginDescriptor.WHERE_AUTOSTART:
35                                 plugin(reason=1)
36         
37         def readPluginList(self, directory, modules = [], depth = 1):
38                 """enumerates plugins"""
39                 
40                 directories = os.listdir(directory)
41                 
42                 for x in directories:
43                         path = directory + x + "/"
44                         if os.path.isdir(path):
45                                 if fileExists(path + "plugin.py"):
46                                         plugin = my_import('.'.join(["Plugins"] + modules + [x, "plugin"]))
47                                         
48                                         if not plugin.__dict__.has_key("Plugins"):
49                                                 print "Plugin %s doesn't have 'Plugin'-call." % (x)
50                                                 continue
51                                         
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                                                 p.updateIcon(path)
60                                                 self.addPlugin(p);
61                                 else:
62                                         open(path + "__init__.py", "w").close()
63                                         self.readPluginList(path, modules + [x], depth - 1)
64
65         def getPlugins(self, where):
66                 """Get list of plugins in a specific category"""
67                 
68                 if type(where) is not list:
69                         where = [ where ]
70                 res = [ ]
71                 for x in where:
72                         for p in self.plugins.get(x, [ ]):
73                                 res.append(p)
74                 return res
75         
76         def clearPluginList(self):
77                 self.pluginList = []
78
79         def shutdown(self):
80                 for p in self.pluginList[:]:
81                         self.removePlugin(p)
82
83 plugins = PluginComponent()