]> kaliko git repositories - python-musicpd.git/blob - doc/source/use.rst
Add sphinx doc (closes #3)
[python-musicpd.git] / doc / source / use.rst
1 Using the client library
2 =========================
3
4 The client library can be used as follows::
5
6     client = musicpd.MPDClient()       # create client object
7     client.connect()                   # use MPD_HOST/MPD_PORT if set else
8                                        #   test ${XDG_RUNTIME_DIR}/mpd/socket for existence
9                                        #   fallback to localhost:6600
10                                        # connect support host/port argument as well
11     print client.mpd_version           # print the mpd version
12     print client.cmd('one', 2)         # print result of the command "cmd one 2"
13     client.close()                     # send the close command
14     client.disconnect()                # disconnect from the server
15
16 A list of supported commands, their arguments (as MPD currently understands
17 them), and the functions used to parse their responses can be found in
18 `doc/commands.txt`.  See the `MPD protocol documentation`_ for more
19 details.
20
21 Command lists are also supported using `command_list_ok_begin()` and
22 `command_list_end()` ::
23
24     client.command_list_ok_begin()       # start a command list
25     client.update()                      # insert the update command into the list
26     client.status()                      # insert the status command into the list
27     results = client.command_list_end()  # results will be a list with the results
28
29 Provide a 2-tuple as argument for command supporting ranges (cf. `MPD protocol documentation`_ for more details).
30 Possible ranges are: "START:END", "START:" and ":" ::
31
32     # An intelligent clear
33     # clears played track in the queue, currentsong included
34     pos = client.currentsong().get('pos', 0)
35     # the 2-tuple range object accepts str, no need to convert to int
36     client.delete((0, pos))
37     # missing end interpreted as highest value possible, pay attention still need a tuple.
38     client.delete((pos,))  # purge queue from current to the end
39
40 A notable case is the `rangeid` command allowing an empty range specified
41 as a single colon as argument (i.e. sending just ":")::
42
43     # sending "rangeid :" to clear the range, play everything
44     client.rangeid(())  # send an empty tuple
45
46 Empty start in range (i.e. ":END") are not possible and will raise a CommandError.
47
48
49 Commands may also return iterators instead of lists if `iterate` is set to
50 `True`::
51
52     client.iterate = True
53     for song in client.playlistinfo():
54         print song['file']
55
56 Each command have a *send\_<CMD>* and a *fetch\_<CMD>* variant, which allows to
57 send a MPD command and then fetch the result later.
58 This is useful for the idle command::
59
60     >>> client.send_idle()
61     # do something else or use function like select()
62     # http://docs.python.org/howto/sockets.html#non-blocking-sockets
63     # ex. select([client], [], [])
64     >>> events = client.fetch_idle()
65
66     # more complex use for example, with glib/gobject:
67     >>> def callback(source, condition):
68     >>>    changes = client.fetch_idle()
69     >>>    print changes
70     >>>    return False  # removes the IO watcher
71
72     >>> client.send_idle()
73     >>> gobject.io_add_watch(client, gobject.IO_IN, callback)
74     >>> gobject.MainLoop().run()
75
76
77 .. _MPD protocol documentation: http://www.musicpd.org/doc/protocol/