properly unload old plugins and load new 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         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):
38                 """enumerates plugins"""
39                 
40                 categories = os.listdir(directory)
41                 
42                 new_plugins = [ ]
43                 
44                 for c in categories:
45                         directory_category = directory + c
46                         if not os.path.isdir(directory_category):
47                                 continue
48                         open(directory_category + "/__init__.py", "a").close()
49                         for x in os.listdir(directory_category):
50                                 path = directory_category + "/" + x + "/"
51                                 if os.path.isdir(path):
52                                         if fileExists(path + "plugin.py"):
53                                                 plugin = my_import('.'.join(["Plugins", c, x, "plugin"]))
54
55                                                 if not plugin.__dict__.has_key("Plugins"):
56                                                         print "Plugin %s doesn't have 'Plugin'-call." % (x)
57                                                         continue
58
59                                                 plugins = plugin.Plugins()
60
61                                                 # allow single entry not to be a list
62                                                 if type(plugins) is not list:
63                                                         plugins = [ plugins ]
64
65                                                 for p in plugins:
66                                                         p.updateIcon(path)
67                                                         new_plugins.append(p)
68                 
69                 # build a diff between the old list of plugins and the new one
70                 # internally, the "fnc" argument will be compared with __eq__
71                 plugins_added = [p for p in new_plugins if p not in self.pluginList]
72                 plugins_removed = [p for p in self.pluginList if p not in new_plugins]
73                 
74                 for p in plugins_removed:
75                         self.removePlugin(p)
76                 
77                 for p in plugins_added:
78                         self.addPlugin(p)
79
80         def getPlugins(self, where):
81                 """Get list of plugins in a specific category"""
82                 
83                 if type(where) is not list:
84                         where = [ where ]
85                 res = [ ]
86                 for x in where:
87                         for p in self.plugins.get(x, [ ]):
88                                 res.append(p)
89                 return res
90         
91         def clearPluginList(self):
92                 self.pluginList = []
93                 self.plugins = {}
94
95         def shutdown(self):
96                 for p in self.pluginList[:]:
97                         self.removePlugin(p)
98
99 plugins = PluginComponent()