Library: move folding patter selection to the widget itself
[nephilim.git] / nephilim / plugins / __init__.py
blob71ee2c4b43e1144f2bc567718cac5f0f9fdf038a
2 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
4 # Nephilim is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # Nephilim is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 import sys
20 import logging
22 __all__ = ['AlbumCover', 'Filebrowser', 'Library', 'Lyrics', 'Notify', 'PlayControl',
23 'Playlist', 'Songinfo', 'Systray']
25 class Plugins:
26 _plugins = None
27 parent = None
28 mpclient = None
30 def __init__(self, parent, mpclient):
31 """load all modules in the plugins directory."""
32 self._plugins = {}
33 self.parent = parent
34 self.mpclient = mpclient
36 for name in __all__:
37 self.__init_plugin(name)
39 def __init_plugin(self, name):
40 try:
41 if name in sys.modules:
42 reload(sys.modules[name])
43 module = sys.modules[name]
44 else:
45 module = __import__(name, globals(), locals(), [], 1)
46 except (SyntaxError, ImportError), e:
47 logging.error('Failed to initialize plugin %s: %s.'%(name, e))
48 return False
50 self._plugins[name] = eval('module.%s(self.parent, self.mpclient, \'%s\')'%(name, name))
51 return True
53 def plugin(self, name):
54 """Returns a plugin with the specified name. It is illegal to access
55 any functions if the plugin is unloaded."""
56 return self._plugins[name] if name in self._plugins else None
58 def load(self, name):
59 """Loads an unloaded plugin."""
60 if not name in self._plugins:
61 if not self.__init_plugin(name):
62 return False
64 self._plugins[name].load()
65 return True
67 def unload(self, name):
68 """Unloads a loaded plugin."""
69 if name in self._plugins:
70 if self._plugins[name].loaded:
71 self._plugins[name].unload()
73 def plugins(self):
74 """Returns all available plugins. It is illegal to access any functions
75 if the plugin is unloaded."""
76 return self._plugins.values()
78 def loaded_plugins(self):
79 """Returns all loaded plugins."""
80 list = []
81 for plugin in self._plugins.values():
82 if plugin.loaded:
83 list.append(plugin)
84 return list