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