python-mpd-server permits to bind a player to a MPD server.
You can control your player with a MPD client such as Sonata, Gmpc or a command line tool mpc. This module defines a server which manages client requests, parses a request and generates a respond. A MPD command is a class that you can override.
Current supported features are:
Clone git repository
git clone git://git.tuxfamily.org/gitroot/pympdserver/python-mpd-server.git python-mpd-server
cd python-mpd-server
python setup.py install --user
You can also browse source code.
Launch a basic mpd server
python examples/mpd_server_example.py
or go http://pympdserver.tuxfamily.org/index.html#how-to-use-it .
All python-mpd-server modules are released under the terms of GPL 3. See LICENSE for the full license.
python-mpd-server is Copyright (C) 2011-2012 kedals0@gmail.com
The GPL is Copyright (C) Free Software Foundation.
Project Page: http://pympdserver.tuxfamily.org/
An example of a basic use is available in Basic Example.
python-mpd-server library defines a default server in mpdserver module and some defaults commands in command_skel module.
To launch the server, you just have to create a Mpd object
mpd=mpdserver.Mpd(listening_port)
This simulates a dummy mpd server which works with sonata, mpc and gmpc. To bind an existing player with mpd commmand, you then have to redefine commands. For example, to bind play command with your player
class Play(mpdserver.Command):
def handle_args(self):yourplayer.play()
mpdserver.MpdRequestHandler.commands['play']=Play
This module permits to define mpd commands. Each command inherits from Command which is a command base class. There are also some specialized subclasses of Command:
MPD songs are represented by classes:
Moreover, you can map your playlist to the mpd playlist by overriding MpdPlaylist. Then when you override this class, a lot of commands are defined (for example Move, MoveId, Delete, etc.).
MPD protocol supports playlist diff (for example command command_skel.PlChanges). This feature is internally management with PlaylistHistory.
Note: ‘command’ and ‘notcommand’ commands seems to not be used by gmpc. Then, we have to implement a lot of commands with dummy respond. However, gmpc use ‘command’ command to allow user to play, pause ...
Command class is the base command class. You can define argument format by setting formatArg. Command argument can be accessed with args dictionnary.
Each command has a playlist attribute which is given by MpdRequestHandler. This playlist must implement MpdPlaylist class and by default, this one is used.
Command contains command arguments definition via Command.formatArg. You can handle them with Command.handle_args(). An argument is :
MPD command name. Command name is the lower class name. This string is used to parse a client request. You can override this classmethod to define particular commandname.
To specify command arguments format. For example,
formatArg=[("song",OptStr)]
means command accept a optionnal string argument bound to song attribute name.
This is a subclass of Command class. CommandItems is used to send items respond such as command.Status command.
This class of commands is used on mpd internal playlist.
This is a subclass of Command class. Respond songs informations for mpd clients.
Override it to adapt this command. This must return a list of MpdPlaylistSong
To create a mpd library song. This is actually not used.
MpdPlaylist is a list of song. Use it to create a mapping between your player and the fictive mpd server.
Some methods must be implemented, otherwise, NotImplementedError is raised.
To bind a playlist to this class, use overide handlePlaylist method.
This is an internal method to automatically add playlistPosition to songs in playlist.
Implement this method to bind your playlist with mpd playlist. This method should return a list of MpdPlaylistSong.
To create a mpd song which is in a playtlist.
MPD use a song id which is unique for all song in playlist and stable over time. This field is automatically generated.
To delete the song at songPosition from current playlist.
List playlist ‘playlistname’ content
Load a playlist in current playlist. Songs are added to current playlist.
To move a song at positionFrom to postionTo.
Override handle_pause() and handle_unpause() method
Without song position, list all song in current playlist. With song position argument, get song details.
Without song position, list all song in current playlist. With song position argument, get song details.
Skip to a specified point toSec in a song songPosition on the playlist
This is a simple example of how to use python-mpd-server.
#!/usr/bin/python
""" This is a simple howto example."""
import mpdserver
# Define a playid command based on mpdserver.PlayId squeleton
class PlayId(mpdserver.PlayId):
# This method is called when playid command is sent by a client
def handle_args(self,songId):print "Play a file with Id '%d'" %songId
# Define a MpdPlaylist based on mpdserver.MpdPlaylist
# This class permits to generate adapted mpd respond on playlist command.
class MpdPlaylist(mpdserver.MpdPlaylist):
playlist=[mpdserver.MpdPlaylistSong(file='file0',songId=0)]
# How to get song position from a song id in your playlist
def songIdToPosition(self,i):
for e in self.playlist:
if e.id==i : return e.playlistPosition
# Set your playlist. It must be a list a MpdPlaylistSong
def handlePlaylist(self):
return self.playlist
# Move song in your playlist
def move(self,i,j):
self.playlist[i],self.playlist[j]=self.playlist[j],self.playlist[i]
# Create a deamonized mpd server that listen on port 9999
mpd=mpdserver.MpdServerDaemon(9999)
# Register provided outputs command
mpd.requestHandler.RegisterCommand(mpdserver.Outputs)
# Register your own command implementation
mpd.requestHandler.RegisterCommand(PlayId)
# Set the user defined playlist class
mpd.requestHandler.Playlist=MpdPlaylist
#mpd.requestHandler.Playlist=mpdserver.MpdPlaylistDummy
print "Starting a mpd server on port 9999"
print "Type Ctrl+C to exit\n"
if __name__ == "__main__":
try:
while mpd.wait(1) : pass
except KeyboardInterrupt:
print "Stopping mpd server"
mpd.quit()