]> kaliko git repositories - python-musicpd.git/blobdiff - doc/source/use.rst
Document context manager
[python-musicpd.git] / doc / source / use.rst
index a071084fb1650c8095634d9ecafb3bf23196ca19..29b6e53e1cfbcea9c2f824437e25801af6caadd2 100644 (file)
@@ -24,6 +24,8 @@ them), and the functions used to parse their responses see :ref:`commands`.
 
 See the `MPD protocol documentation`_ for more details.
 
+.. _environment_variables:
+
 Environment variables
 ---------------------
 
@@ -38,14 +40,33 @@ The client honors the following environment variables:
   * ``MPD_TIMEOUT`` timeout for connecting to MPD and waiting for MPD’s response in seconds
   * ``XDG_RUNTIME_DIR`` path to look for potential socket: ``${XDG_RUNTIME_DIR}/mpd/socket``
 
-Defaults settings
------------------
+.. _default_settings:
+
+Default settings
+----------------
 
   * If ``MPD_HOST`` is not set, then look for a socket in ``${XDG_RUNTIME_DIR}/mpd/socket``
   * If there is no socket use ``localhost``
   * If ``MPD_PORT`` is not set, then use ``6600``
   * If ``MPD_TIMEOUT`` is not set, then uses :py:obj:`musicpd.CONNECTION_TIMEOUT`
 
+
+Context manager
+---------------
+
+Calling MPDClient in a context manager :py:obj:`musicpd.MPDClient.connect` is
+transparently called with :ref:`default setting<default_settings>` (use
+:ref:`environment variables<environment_variables>` to override defaults).
+Leaving the context manager :py:obj:`musicpd.MPDClient.disconnect` is called.
+
+.. code-block:: python
+
+    import os
+    os.environ['MPD_HOST'] = 'mpdhost'
+    with MPDClient() as c:
+        c.status()
+        c.next()
+
 Command lists
 -------------