append, don't overwrite __init__.py files
[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 reReadPluginList(self, directory, depth = 1):
38                 print "re-reading plugin list"
39                 self.clearPluginList()
40                 self.readPluginList(directory=directory, depth=depth)
41         
42         def readPluginList(self, directory, modules = [], depth = 1):
43                 """enumerates plugins"""
44                 
45                 directories = os.listdir(directory)
46                 
47                 for x in directories:
48                         path = directory + x + "/"
49                         if os.path.isdir(path):
50                                 if fileExists(path + "plugin.py"):
51                                         plugin = my_import('.'.join(["Plugins"] + modules + [x, "plugin"]))
52                                         
53                                         if not plugin.__dict__.has_key("Plugins"):
54                                                 print "Plugin %s doesn't have 'Plugin'-call." % (x)
55                                                 continue
56                                         
57                                         plugins = plugin.Plugins()
58                                         
59                                         # allow single entry not to be a list
60                                         if type(plugins) is not list:
61                                                 plugins = [ plugins ]
62                                         
63                                         for p in plugins:
64                                                 p.updateIcon(path)
65                                                 self.addPlugin(p);
66                                 else:
67                                         open(path + "__init__.py", "a").close()
68                                         self.readPluginList(path, modules + [x], depth - 1)
69
70         def getPlugins(self, where):
71                 """Get list of plugins in a specific category"""
72                 
73                 if type(where) is not list:
74                         where = [ where ]
75                 res = [ ]
76                 for x in where:
77                         for p in self.plugins.get(x, [ ]):
78                                 res.append(p)
79                 return res
80         
81         def clearPluginList(self):
82                 self.pluginList = []
83                 self.plugins = {}
84
85         def shutdown(self):
86                 for p in self.pluginList[:]:
87                         self.removePlugin(p)
88
89 plugins = PluginComponent()