Tarmack's home

MPD Unicode

A wrapper class for python-mpd to make it return python Unicode objects.

While the mpd protocol uses utf-8 unicode, python-mpd doesn't return python unicode objects. This makes it a bit troublesome to handle strings with unicode characters in your application. Using encode/decode every time you want to evaluate a song name or artist is, to say the least, bug prone. To deal with this I made a module that subclasses python-mpd to extend its functionality.

After I made this class, some more features I felt python-mpd missed surfaced. One of these was the idle command. This command allows you to put the connection to the server in an idle state. In this state the server will inform the client of changes that might be of interest. This saves network traffic as there is no need for polling. But arguably more important, it enables the client to react to the changes immediately giving the client a nice reactive feel to it.

Later I added some other features that the original python-mpd module lacks. These are little things that could be implemented using the infrastructure already in place, such as consume and rescan.

So here it is, a lean wrapper to make the python-mpd module do the things I missed. It should make it really easy to build extensions to mpd that support Unicode and idle mode in a pretty pythonic way. It is also used in Pythagora.


view the code or download mpdunicode.


The functions it ads:

  • idle(subsystem, [timeout])

    Puts the connection in idle mode. It returns a list with strings containing the subsystems returned by mpd. The subsystem argument is a list of subsystems to report on. The optional timeout is a float telling how many seconds to wait for something to happen.


  • noidle()

    Exits idle mode, this is actually the only command allowed in idle mode. It will return an empty list.


  • rescan()

    Starts a database update where every file is scanned as opposed to update() which will only scan new files.


  • single(state)

    Causes playback to stop after the current song. The song is repeated if repeat is enabled. State is an integer 0 or 1.


  • consume(state)

    Make mpd remove every song from the playlist after it has been played. State is an integer 0 or 1.




Valid XHTML 1.0 Transitional Valid CSS!