recursively search for plugins
[vuplus_dvbapp] / 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                                         self.readPluginList(path, modules + [x], depth - 1)
63
64         def getPlugins(self, where):
65                 """Get list of plugins in a specific category"""
66                 
67                 if type(where) is not list:
68                         where = [ where ]
69                 res = [ ]
70                 for x in where:
71                         for p in self.plugins.get(x, [ ]):
72                                 res.append(p)
73                 return res
74
75         def shutdown(self):
76                 for p in self.pluginList[:]:
77                         self.removePlugin(p)
78
79 plugins = PluginComponent()