Python plugins

MPX supports plugins written in Python.

If you want to write a plugin for MPX, you can start with the dummy plugin.

Required files

The required files for a plugin are:

  • __init__.py : main plugin file
  • plugin_name.mpx-plugin : description of the plugin (Not hard-required but you should add it)

Description

File .mpx-plugin

[MPX Plugin]
Loader=python
Module=dummy
IAge= Interface Age, for the moment, we're at version 1
Name= Name of the plugin
Authors =Author of the plugin
Copyright=Copyright © 2008 Your name
Website=Website of the plugin

(Format blatantly copied from RhythmBox)

File __init__.py

You need a class in it which derives from mpx.Plugin:

import mpx

class MyPluginClass(mpx.Plugin):

   def __init__(self, id, player, mcs):

      """ Initializing """
 
      self.id = id # the plugin's id, an long long integer
      self.player = player # the player object
      self.mcs = mcs # the MCS configuration backend

Then you need an activate() and deactivate() method. The activate() method gets MPX::Player and Mcs::Mcs (the configuration backend) exported; through Player, the plugin can acquire references to other objects like the Library (high-level database backend).

Plugins can connect to signal of Player (or other objects); connections happen via GObject/PyGTK/PyGObject or sigc signals. The Python GObject for Player can be acquired via player.gobj():

   def activate(self):

      self.conn1 = self.player.gobj().connect("new-track", self.on_new_track)

In the deactivate() method, the plugin should release all references and disconnect from eventual signal connections:

   def deactivate(self):
  
      self.player.disconnect(self.conn1)

Now let's also check our example method on_new_track.

   def on_new_track(self):
     
      m = self.player.get_metadata() # Gets the metadata of the current track, including coverart
    
      # Let's print the title of the current track
      if m[mpx.AttributeId.TITLE]:
         print m[mpx.AttributeId.TITLE].get()

(The check for TITLE is neccessary because metadata is not guaranteed to always contain all metadata fields, and in some cases a field might be not there at all.)

Here's the entire module all in one:

import mpx

class MyPluginClass(mpx.Plugin):

   def __init__(self, id, player, mcs):

      """ Initializing """
 
      self.id = id # the plugin's id, an long long integer
      self.player = player # the player object
      self.mcs = mcs # the MCS configuration backend

   def activate(self):

      self.conn1 = self.player.gobj().connect("new-track", self.on_new_track)

   def deactivate(self):
  
      self.player.disconnect(self.conn1)

   def on_new_track(self):
     
      m = self.player.get_metadata() # Gets the metadata of the current track, including coverart
    
      # Let's print the title of the current track
      if m[mpx.AttributeId.TITLE]:
         print m[mpx.AttributeId.TITLE].get()

Further reading MPXPythonClasses? MPXPythonDataTypes