diff options
author | Anton Khirnov <wyskas@gmail.com> | 2009-02-20 11:15:51 +0100 |
---|---|---|
committer | Anton Khirnov <wyskas@gmail.com> | 2009-02-20 11:15:51 +0100 |
commit | cca3aa06f58d5ad1b1abedc5d789bb626909da70 (patch) | |
tree | b633b3b53cd12815f96112d5aa3f0f114a982971 /nephilim | |
parent | 80a77640d4a0d91c8df41c9a7b7cc1fe3ff02818 (diff) |
Wrap plugins interface into a class.
Diffstat (limited to 'nephilim')
-rw-r--r-- | nephilim/plugins/Notify.py | 2 | ||||
-rw-r--r-- | nephilim/plugins/__init__.py | 131 | ||||
-rw-r--r-- | nephilim/winMain.py | 13 |
3 files changed, 68 insertions, 78 deletions
diff --git a/nephilim/plugins/Notify.py b/nephilim/plugins/Notify.py index b3c2285..83e0f96 100644 --- a/nephilim/plugins/Notify.py +++ b/nephilim/plugins/Notify.py @@ -49,7 +49,7 @@ class winNotify(QtGui.QWidget): return self._current_priority = priority - cover = plugins.getPlugin('albumcover').getWidget().get_cover() + cover = self.winMain.plugins.getPlugin('albumcover').getWidget().get_cover() if cover: self.cover_label.setPixmap(cover.scaledToHeight(self.fontInfo().pixelSize()*4)) else: diff --git a/nephilim/plugins/__init__.py b/nephilim/plugins/__init__.py index affbf45..7ecca22 100644 --- a/nephilim/plugins/__init__.py +++ b/nephilim/plugins/__init__.py @@ -9,89 +9,76 @@ PLUGIN_CLASS=1 PLUGIN_INSTANCE=2 PLUGIN_MSG=3 +class Plugins: -class IPlaylist: - def ensureVisible(self, song_id): - raise Exception("TODO implement") + 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 loadPlugins(): - """(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 - - _plugins[className.lower()]=[mod, className, None, None] - loadPlugin(className, None) + _plugins[className.lower()]=[mod, className, None, None] + self.loadPlugin(className, None) -def setPluginMessage(name, msg): - global _plugins - try: - _plugins[name.lower()][PLUGIN_MSG]=msg - except: + def setPluginMessage(self, name, msg): + global _plugins try: - _plugins["plugin%s"%(name.lower())][PLUGIN_MODULE]=msg + _plugins[name.lower()][PLUGIN_MSG]=msg except: - pass + try: + _plugins["plugin%s"%(name.lower())][PLUGIN_MODULE]=msg + except: + pass -def getPlugin(name): - global _plugins - try: - return _plugins[name.lower()][PLUGIN_INSTANCE] - except: + def getPlugin(self, name): + global _plugins try: - return _plugins["plugin%s"%(name.lower())][PLUGIN_INSTANCE] + return _plugins[name.lower()][PLUGIN_INSTANCE] except: - return None + try: + return _plugins["plugin%s"%(name.lower())][PLUGIN_INSTANCE] + except: + return None -def loadPlugin(className, parent): - """Constructs a plugin.""" - global _plugins - entry=_plugins[className.lower()] - mod=entry[PLUGIN_MODULE] - # ensure we get the latest version - try: + def loadPlugin(self, className, parent): + """Constructs a plugin.""" + global _plugins + entry=_plugins[className.lower()] + mod=entry[PLUGIN_MODULE] + # ensure we get the latest version try: - sys.modules[mod] - reimport=True - except: - reimport=False - - if reimport: - reload(sys.modules[mod]) - else: - module=__import__(mod, globals(), locals(), className, -1) - - 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 - - module=sys.modules[mod] - _plugins[className.lower()][PLUGIN_MSG]=None - - 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] + try: + sys.modules[mod] + reimport=True + except: + reimport=False -def listImplementors(interface, loaded = True): - """Return a list of plugin-instances that implement an interface""" - global _plugins - return map(lambda plugin: plugin[PLUGIN_INSTANCE] - , filter(lambda plugin: isinstance(plugin[PLUGIN_INSTANCE], interface) - and ((loaded != None and plugin[PLUGIN_INSTANCE].loaded == loaded) or (loaded == None)), _plugins.values())) + if reimport: + reload(sys.modules[mod]) + else: + module=__import__(mod, globals(), locals(), className, -1) -def listPlugins(): - """Get the list of plugins available as { className => [mod, className, instance, msg] }.""" - global _plugins - return _plugins + 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 + + module=sys.modules[mod] + _plugins[className.lower()][PLUGIN_MSG]=None + 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] -loadPlugins() + def listPlugins(self): + """Get the list of plugins available as { className => [mod, className, instance, msg] }.""" + global _plugins + return _plugins diff --git a/nephilim/winMain.py b/nephilim/winMain.py index df5567c..c576e25 100644 --- a/nephilim/winMain.py +++ b/nephilim/winMain.py @@ -28,6 +28,8 @@ class winMain(QtGui.QMainWindow): " MPD object" mpclient = None + " plugins interface" + plugins = None " Statusbar objects" statuslabel = None @@ -86,9 +88,10 @@ class winMain(QtGui.QMainWindow): self.addToolBar(QtCore.Qt.TopToolBarArea, menu_toolbar) showWinSettings = False # are there new plugins? - for k, entry in plugins.listPlugins().iteritems(): + self.plugins = plugins.Plugins() + for k, entry in self.plugins.listPlugins().iteritems(): # load the plugin - plugin=plugins.loadPlugin(entry[plugins.PLUGIN_CLASS], self) + plugin = self.plugins.loadPlugin(entry[plugins.PLUGIN_CLASS], self) if plugin: if self.settings.value(plugin.getName() + '/load') == None: showWinSettings = True @@ -97,7 +100,7 @@ class winMain(QtGui.QMainWindow): try: plugin.load() except Exception, e: - plugins.setPluginMessage(plugin.getName(), "Exception while loading %s: %s"%(plugin.getName(), str(e))) + self.plugins.setPluginMessage(plugin.getName(), "Exception while loading %s: %s"%(plugin.getName(), str(e))) showWinSettings=True self.updateLayoutMenu() @@ -131,7 +134,7 @@ class winMain(QtGui.QMainWindow): def quit(self): # unload all plugins - for entry in plugins.listPlugins().values(): + for entry in self.plugins.listPlugins().values(): p=entry[plugins.PLUGIN_INSTANCE] if p and p.isLoaded(): p.unload() @@ -236,7 +239,7 @@ class winMain(QtGui.QMainWindow): doEvents def enableAll(self, value): - for k,entry in plugins.listPlugins().iteritems(): + for k,entry in self.plugins.listPlugins().iteritems(): try: plugin=entry[plugins.PLUGIN_INSTANCE] plugin.o.setEnabled(value) |