summaryrefslogtreecommitdiff
path: root/nephilim/plugins/__init__.py
blob: faaaf591ec476aea39c5f72b1cec3503817b1c89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import sys
import logging

__all__ = ['AlbumCover', 'Filebrowser', 'Library', 'Lyrics', 'Notify', 'PlayControl',
           'Playlist', 'Systray']

class Plugins:
    _plugins = None
    parent   = None

    def __init__(self, parent):
        """load all modules in the plugins directory."""
        self._plugins = {}
        self.parent   = parent

        for name in __all__:
            self.init_plugin(name)

    def setPluginMessage(self, name, msg):
        try:
            self._plugins[name.lower()][PLUGIN_MSG]=msg
        except:
            try:
                self._plugins["plugin%s"%(name.lower())][PLUGIN_MODULE]=msg
            except:
                pass

    def plugin(self, name):
        return self._plugins[name] if name in self._plugins else None

    def init_plugin(self, name):
        try:
            if name in sys.modules:
                reload(sys.modules[name])
                module = sys.modules[name]
            else:
                module = __import__(name, globals(), locals(), [], 1)
        except SyntaxError:
            logging.error('Failed to initialize plugin %s.'%name)
            return False

        self._plugins[name] = eval('module.%s(self.parent)'%name)
        return True

    def load(self, name):
        if not name in self._plugins:
            if not self.init_plugin(name):
                return False

        self._plugins[name].load()
        return True

    def unload(self, name):
        if name in self._plugins:
            if self._plugins[name].isLoaded():
                self._plugins[name].unload()

    def plugins(self):
        return self._plugins.values()

    def loaded_plugins(self):
        list = []
        for plugin in self._plugins.values():
            if plugin.isLoaded():
                list.append(plugin)
        return list