summaryrefslogtreecommitdiff
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
parenteaccbb78851ecfcaa315e7398755571741aaad26 (diff)
Simplify loading 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
-rw-r--r--nephilim/winMain.py29
-rw-r--r--nephilim/winSettings.py57
11 files changed, 89 insertions, 133 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
diff --git a/nephilim/winMain.py b/nephilim/winMain.py
index c576e25..51da107 100644
--- a/nephilim/winMain.py
+++ b/nephilim/winMain.py
@@ -88,20 +88,12 @@ class winMain(QtGui.QMainWindow):
self.addToolBar(QtCore.Qt.TopToolBarArea, menu_toolbar)
showWinSettings = False # are there new plugins?
- self.plugins = plugins.Plugins()
- for k, entry in self.plugins.listPlugins().iteritems():
- # load the plugin
- plugin = self.plugins.loadPlugin(entry[plugins.PLUGIN_CLASS], self)
- if plugin:
- if self.settings.value(plugin.getName() + '/load') == None:
- showWinSettings = True
- if self.settings.value(plugin.getName() + '/load', QVariant(True)).toBool():
- # load new plugins by default
- try:
- plugin.load()
- except Exception, e:
- self.plugins.setPluginMessage(plugin.getName(), "Exception while loading %s: %s"%(plugin.getName(), str(e)))
- showWinSettings=True
+ self.plugins = plugins.Plugins(self)
+ for plugin in self.plugins.plugins():
+ if self.settings.value(plugin.getName() + '/load') == None:
+ showWinSettings = True
+ if self.settings.value(plugin.getName() + '/load', QVariant(True)).toBool():
+ self.plugins.load(plugin.getName())
self.updateLayoutMenu()
self.setDockOptions(QtGui.QMainWindow.AllowNestedDocks \
@@ -134,10 +126,8 @@ class winMain(QtGui.QMainWindow):
def quit(self):
# unload all plugins
- for entry in self.plugins.listPlugins().values():
- p=entry[plugins.PLUGIN_INSTANCE]
- if p and p.isLoaded():
- p.unload()
+ for plugin in self.plugins.loaded_plugins():
+ plugin.unload()
self.settings.setValue('geometry', QVariant(self.saveGeometry()))
self.settings.sync()
@@ -239,9 +229,8 @@ class winMain(QtGui.QMainWindow):
doEvents
def enableAll(self, value):
- for k,entry in self.plugins.listPlugins().iteritems():
+ for plugin in self.plugins.loaded_plugins():
try:
- plugin=entry[plugins.PLUGIN_INSTANCE]
plugin.o.setEnabled(value)
except:
pass
diff --git a/nephilim/winSettings.py b/nephilim/winSettings.py
index c2aaab8..a1994e4 100644
--- a/nephilim/winSettings.py
+++ b/nephilim/winSettings.py
@@ -88,13 +88,11 @@ class winSettings(QtGui.QWidget):
self.lstPlugins = QtGui.QListWidget(self)
tabWidget.addTab(self.lstPlugins, 'plugins')
- for k,entry in self.winMain.plugins.listPlugins().iteritems():
- plugin=entry[plugins.PLUGIN_INSTANCE]
- if plugin:
- wg = plugin.get_settings_widget()
- if wg:
- self.settings_wg.append(wg)
- tabWidget.addTab(self.settings_wg[-1], plugin.getName())
+ for plugin in self.winMain.plugins.loaded_plugins():
+ wg = plugin.get_settings_widget()
+ if wg:
+ self.settings_wg.append(wg)
+ tabWidget.addTab(self.settings_wg[-1], plugin.getName())
self.fillList()
self.setLayout(QtGui.QVBoxLayout())
@@ -117,29 +115,18 @@ class winSettings(QtGui.QWidget):
def fillList(self):
self.lstPlugins.clear()
- for k,entry in self.winMain.plugins.listPlugins().iteritems():
- plugin=entry[plugins.PLUGIN_INSTANCE]
- if plugin:
- if entry[plugins.PLUGIN_MSG]:
- item=QtGui.QListWidgetItem("%s\t%s"%(entry[plugins.PLUGIN_CLASS], entry[plugins.PLUGIN_MSG]))
- item.setCheckState(QtCore.Qt.Unchecked)
- item.setTextColor(QtCore.Qt.red)
- else:
- item=QtGui.QListWidgetItem("%s\t%s"%(entry[plugins.PLUGIN_CLASS], plugin.getInfo()))
- if plugin.isLoaded():
- item.setCheckState(QtCore.Qt.Checked)
- else:
- item.setCheckState(QtCore.Qt.Unchecked)
-
- if self.settings.value(plugin.getName() + '/load') == None:
- # load new plugins by default
- item.setTextColor(QtCore.Qt.blue)
- self.settings.setValue(plugin.getName() + '/load', QtCore.QVariant(True))
-
+ for plugin in self.winMain.plugins.plugins():
+ item = QtGui.QListWidgetItem("%s\t%s"%(plugin.getName(), plugin.getInfo()))
+ if plugin.isLoaded():
+ item.setCheckState(QtCore.Qt.Checked)
else:
- item=QtGui.QListWidgetItem("%s\t%s"%(entry[plugins.PLUGIN_CLASS], entry[plugins.PLUGIN_MSG]))
item.setCheckState(QtCore.Qt.Unchecked)
- item.setTextColor(QtCore.Qt.red)
+
+ if self.settings.value(plugin.getName() + '/load') == None:
+ # load new plugins by default
+ item.setTextColor(QtCore.Qt.blue)
+ self.settings.setValue(plugin.getName() + '/load', QtCore.QVariant(True))
+
self.lstPlugins.addItem(item)
def center(self):
@@ -156,20 +143,16 @@ class winSettings(QtGui.QWidget):
def onlstPluginItemChanged(self, item):
# check here if we have to load or unload the plugin!
toload = (item.checkState() == QtCore.Qt.Checked)
- className=str(item.text()[0:str(item.text()).find('\t')])
+ name=str(item.text()[0:str(item.text()).find('\t')])
if toload:
# refresh the plugin file
- plugin=self.winMain.plugins.loadPlugin(className, self.winMain)
- if plugin:
- plugin.load()
+ self.winMain.plugins.load(name)
self.fillList()
self.winMain.restoreLayout()
else:
- plugin=self.winMain.plugins.getPlugin(className)
- if plugin:
- plugin.unload()
- if plugin:
- self.settings.setValue(plugin.getName() + '/load', QtCore.QVariant(toload))
+ self.winMain.plugins.unload(name)
+ self.settings.setValue(name + '/load', QtCore.QVariant(toload))
+
def closeEvent(self, event):
map(lambda entry: entry[plugins.PLUGIN_INSTANCE] and entry[plugins.PLUGIN_INSTANCE].resetSettingCache(), self.winMain.plugins.listPlugins().values())
self.settings_wg = None