summaryrefslogtreecommitdiff
path: root/nephilim/plugins
diff options
context:
space:
mode:
authorAnton Khirnov <wyskas@gmail.com>2009-02-20 17:15:48 +0100
committerAnton Khirnov <wyskas@gmail.com>2009-02-20 17:15:48 +0100
commitdfa8df621e4bc9c68215db76ae2a09bc16fe6a04 (patch)
tree4f9543d1326fa3b9dea7e3268e11b3acc64e6ca6 /nephilim/plugins
parenteaccbb78851ecfcaa315e7398755571741aaad26 (diff)
Simplify loading plugins.
Diffstat (limited to 'nephilim/plugins')
-rw-r--r--nephilim/plugins/AlbumCover.py6
-rw-r--r--nephilim/plugins/Filebrowser.py2
-rw-r--r--nephilim/plugins/Library.py2
-rw-r--r--nephilim/plugins/Lyrics.py2
-rw-r--r--nephilim/plugins/Notify.py13
-rw-r--r--nephilim/plugins/PlayControl.py2
-rw-r--r--nephilim/plugins/Playlist.py2
-rw-r--r--nephilim/plugins/Systray.py2
-rw-r--r--nephilim/plugins/__init__.py105
9 files changed, 60 insertions, 76 deletions
diff --git a/nephilim/plugins/AlbumCover.py b/nephilim/plugins/AlbumCover.py
index 426df96..a858d25 100644
--- a/nephilim/plugins/AlbumCover.py
+++ b/nephilim/plugins/AlbumCover.py
@@ -172,7 +172,7 @@ class wgAlbumCover(QtGui.QLabel):
logging.info("Failed to create cover file")
return False
-class pluginAlbumCover(Plugin):
+class AlbumCover(Plugin):
o = None
DEFAULTS = {'coverdir' : '$musicdir/$songdir', 'covername' : '.cover_mpclient_$artist_$album',
'action0' : 1, 'action1' : 1}
@@ -200,8 +200,8 @@ class pluginAlbumCover(Plugin):
" albumcover.downloadto: where to download album covers from internet to. This string can contain the normal tags of the current playing song, plus $music_dir and $cover.\n" \
" albumcover.files: comma separated list of filenames (without extension)to be considered an album cover. Extensions jpg, jpeg, png, gif and bmp are used.\n"
- def getWidget(self):
- return self.o
+ def get_cover(self):
+ return self.o.get_cover()
def _getDockWidget(self):
return self._createDock(self.o)
diff --git a/nephilim/plugins/Filebrowser.py b/nephilim/plugins/Filebrowser.py
index 0a2908d..cb85a28 100644
--- a/nephilim/plugins/Filebrowser.py
+++ b/nephilim/plugins/Filebrowser.py
@@ -5,7 +5,7 @@ import os
from ..clPlugin import Plugin
from ..misc import ORGNAME, APPNAME
-class pluginFilebrowser(Plugin):
+class Filebrowser(Plugin):
view = None
model = None
diff --git a/nephilim/plugins/Library.py b/nephilim/plugins/Library.py
index 60490d9..24fab35 100644
--- a/nephilim/plugins/Library.py
+++ b/nephilim/plugins/Library.py
@@ -4,7 +4,7 @@ from PyQt4.QtCore import QVariant
from ..clPlugin import Plugin
from ..misc import ORGNAME, APPNAME
-class pluginLibrary(Plugin):
+class Library(Plugin):
o=None
DEFAULTS = {'modes' : 'artist\n'\
'artist/album\n'\
diff --git a/nephilim/plugins/Lyrics.py b/nephilim/plugins/Lyrics.py
index 1983f2a..5a9c98e 100644
--- a/nephilim/plugins/Lyrics.py
+++ b/nephilim/plugins/Lyrics.py
@@ -32,7 +32,7 @@ class wgLyrics(QtGui.QWidget):
if lyrics:
self.txtView.insertPlainText(lyrics)
-class pluginLyrics(Plugin):
+class Lyrics(Plugin):
o = None
DEFAULTS = {'sites' : ['lyricwiki'], 'lyricdir' : '$musicdir/$songdir',
'lyricname' : '.lyric_mpclient_$artist_$album_$song'}
diff --git a/nephilim/plugins/Notify.py b/nephilim/plugins/Notify.py
index 83e0f96..f42805e 100644
--- a/nephilim/plugins/Notify.py
+++ b/nephilim/plugins/Notify.py
@@ -49,11 +49,12 @@ class winNotify(QtGui.QWidget):
return
self._current_priority = priority
- cover = self.winMain.plugins.getPlugin('albumcover').getWidget().get_cover()
- if cover:
- self.cover_label.setPixmap(cover.scaledToHeight(self.fontInfo().pixelSize()*4))
- else:
- self.cover_label.clear()
+ self.cover_label.clear()
+ ac = self.winMain.plugins.plugin('AlbumCover')
+ if ac:
+ cover = ac.get_cover()
+ if cover:
+ self.cover_label.setPixmap(cover.scaledToHeight(self.fontInfo().pixelSize()*4))
self.text_label.setText(text)
if self._timerID:
@@ -83,7 +84,7 @@ class winNotify(QtGui.QWidget):
self.hide()
self.update()
-class pluginNotify(Plugin):
+class Notify(Plugin):
o=None
DEFAULTS = {'songformat' : '$track - $artist - $title ($album) [$length]',
'timer' : 3}
diff --git a/nephilim/plugins/PlayControl.py b/nephilim/plugins/PlayControl.py
index 5b47627..e8dda8c 100644
--- a/nephilim/plugins/PlayControl.py
+++ b/nephilim/plugins/PlayControl.py
@@ -141,7 +141,7 @@ class wgPlayControl(QtGui.QToolBar):
except:
pass
-class pluginPlayControl(Plugin):
+class PlayControl(Plugin):
o=None
DEFAULTS = {'queue' : ''}
def __init__(self, winMain):
diff --git a/nephilim/plugins/Playlist.py b/nephilim/plugins/Playlist.py
index 2258701..230486c 100644
--- a/nephilim/plugins/Playlist.py
+++ b/nephilim/plugins/Playlist.py
@@ -5,7 +5,7 @@ from ..clPlugin import Plugin
# Dependencies:
# playcontrol
-class pluginPlaylist(Plugin):
+class Playlist(Plugin):
o = None
DEFAULTS = {'columns': ['track', 'title', 'artist',
'date', 'album', 'length']}
diff --git a/nephilim/plugins/Systray.py b/nephilim/plugins/Systray.py
index 49a1414..3a773f1 100644
--- a/nephilim/plugins/Systray.py
+++ b/nephilim/plugins/Systray.py
@@ -4,7 +4,7 @@ from PyQt4.QtCore import QVariant
from ..clPlugin import Plugin
from ..misc import sec2min, ORGNAME, APPNAME, appIcon
-class pluginSystray(Plugin):
+class Systray(Plugin):
DEFAULTS = {'format': '$track - $title by $artist on $album ($length)'}
o = None
format = None
diff --git a/nephilim/plugins/__init__.py b/nephilim/plugins/__init__.py
index 7ecca22..faaaf59 100644
--- a/nephilim/plugins/__init__.py
+++ b/nephilim/plugins/__init__.py
@@ -2,83 +2,66 @@ import os
import sys
import logging
-# { className => [module, className, instance, msg] }
-_plugins=None
-PLUGIN_MODULE=0
-PLUGIN_CLASS=1
-PLUGIN_INSTANCE=2
-PLUGIN_MSG=3
+__all__ = ['AlbumCover', 'Filebrowser', 'Library', 'Lyrics', 'Notify', 'PlayControl',
+ 'Playlist', 'Systray']
class Plugins:
+ _plugins = None
+ parent = None
- def __init__(self):
- """(Re)load all modules in the plugins directory."""
- global _plugins
- _plugins={}
- for file in os.listdir('nephilim/plugins'):
- if file[-3:]=='.py' and file!='__init__.py':
- name=file[:-3] # name without ext
- mod='nephilim.plugins.%s'%(name) # mod name
- className='plugin%s'%(name) # classname
+ def __init__(self, parent):
+ """load all modules in the plugins directory."""
+ self._plugins = {}
+ self.parent = parent
- _plugins[className.lower()]=[mod, className, None, None]
- self.loadPlugin(className, None)
+ for name in __all__:
+ self.init_plugin(name)
def setPluginMessage(self, name, msg):
- global _plugins
try:
- _plugins[name.lower()][PLUGIN_MSG]=msg
+ self._plugins[name.lower()][PLUGIN_MSG]=msg
except:
try:
- _plugins["plugin%s"%(name.lower())][PLUGIN_MODULE]=msg
+ self._plugins["plugin%s"%(name.lower())][PLUGIN_MODULE]=msg
except:
pass
- def getPlugin(self, name):
- global _plugins
- try:
- return _plugins[name.lower()][PLUGIN_INSTANCE]
- except:
- try:
- return _plugins["plugin%s"%(name.lower())][PLUGIN_INSTANCE]
- except:
- return None
+ def plugin(self, name):
+ return self._plugins[name] if name in self._plugins else None
- def loadPlugin(self, className, parent):
- """Constructs a plugin."""
- global _plugins
- entry=_plugins[className.lower()]
- mod=entry[PLUGIN_MODULE]
- # ensure we get the latest version
+ def init_plugin(self, name):
try:
- try:
- sys.modules[mod]
- reimport=True
- except:
- reimport=False
-
- if reimport:
- reload(sys.modules[mod])
+ if name in sys.modules:
+ reload(sys.modules[name])
+ module = sys.modules[name]
else:
- module=__import__(mod, globals(), locals(), className, -1)
+ 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
- except Exception, e:
- _plugins[className.lower()][PLUGIN_MSG]=str(e)
- _plugins[className.lower()][PLUGIN_INSTANCE]=None
- logging.warning("Failed to load plugin %s: %s %s"%(className, str(type(e)), str(e)))
- return None
+ self._plugins[name].load()
+ return True
- module=sys.modules[mod]
- _plugins[className.lower()][PLUGIN_MSG]=None
+ def unload(self, name):
+ if name in self._plugins:
+ if self._plugins[name].isLoaded():
+ self._plugins[name].unload()
- if parent:
- # instantiate the plugin
- _plugins[className.lower()][PLUGIN_INSTANCE]=module.__dict__[className](parent)
- else:
- _plugins[className.lower()][PLUGIN_INSTANCE]=None
- return _plugins[className.lower()][PLUGIN_INSTANCE]
+ def plugins(self):
+ return self._plugins.values()
- def listPlugins(self):
- """Get the list of plugins available as { className => [mod, className, instance, msg] }."""
- global _plugins
- return _plugins
+ def loaded_plugins(self):
+ list = []
+ for plugin in self._plugins.values():
+ if plugin.isLoaded():
+ list.append(plugin)
+ return list