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