honor horizontal alignment also when a mark is present
[enigma2.git] / lib / python / Components / PluginComponent.py
1 import os
2
3 from Tools.Directories import *
4 from Tools.Import import my_import
5 from Plugins.Plugin import PluginDescriptor
6
7 class PluginComponent:
8         def __init__(self):
9                 self.plugins = {}
10                 self.pluginList = [ ]
11                 self.setPluginPrefix("Plugins.")
12                 
13         def setPluginPrefix(self, prefix):
14                 self.prefix = prefix
15         
16         def addPlugin(self, plugin):
17                 self.pluginList.append(plugin)
18                 for x in plugin.where:
19                         self.plugins.setdefault(x, []).append(plugin)
20                         if x == PluginDescriptor.WHERE_AUTOSTART:
21                                 plugin(reason=0)
22         
23         def removePlugin(self, plugin):
24                 self.pluginList.remove(plugin)
25                 for x in plugin.where:
26                         self.plugins[x].remove(plugin)
27                         if x == PluginDescriptor.WHERE_AUTOSTART:
28                                 plugin(reason=1)
29         
30         def readPluginList(self, directory):
31                 """enumerates plugins"""
32                 
33                 categories = os.listdir(directory)
34                 
35                 new_plugins = [ ]
36                 
37                 for c in categories:
38                         directory_category = directory + c
39                         if not os.path.isdir(directory_category):
40                                 continue
41                         open(directory_category + "/__init__.py", "a").close()
42                         for x in os.listdir(directory_category):
43                                 path = directory_category + "/" + x
44                                 if os.path.isdir(path):
45                                         if fileExists(path + "/plugin.pyc") or fileExists(path + "/plugin.py"):
46                                                 plugin = my_import('.'.join(["Plugins", c, 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(path=path)
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                                                         new_plugins.append(p)
61                 
62                 # build a diff between the old list of plugins and the new one
63                 # internally, the "fnc" argument will be compared with __eq__
64                 plugins_added = [p for p in new_plugins if p not in self.pluginList]
65                 plugins_removed = [p for p in self.pluginList if p not in new_plugins]
66                 
67                 for p in plugins_removed:
68                         self.removePlugin(p)
69                 
70                 for p in plugins_added:
71                         self.addPlugin(p)
72
73         def getPlugins(self, where):
74                 """Get list of plugins in a specific category"""
75                 
76                 if type(where) is not list:
77                         where = [ where ]
78                 res = [ ]
79                 for x in where:
80                         for p in self.plugins.get(x, [ ]):
81                                 res.append(p)
82                 return res
83         
84         def clearPluginList(self):
85                 self.pluginList = []
86                 self.plugins = {}
87
88         def shutdown(self):
89                 for p in self.pluginList[:]:
90                         self.removePlugin(p)
91
92 plugins = PluginComponent()