X-Git-Url: http://git.kaliko.me/?p=python-musicpd.git;a=blobdiff_plain;f=doc%2Fsource%2Fuse.rst;h=29b6e53e1cfbcea9c2f824437e25801af6caadd2;hp=cdf0ed223effa1dcf53066685a7c5dc33b05a68c;hb=010d7a7061e172a0e75f4bacce601e3025372500;hpb=0c16ca07e3ac85ab212c3444f9e5a71c4a96a406 diff --git a/doc/source/use.rst b/doc/source/use.rst index cdf0ed2..29b6e53 100644 --- a/doc/source/use.rst +++ b/doc/source/use.rst @@ -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` (use +:ref:`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 ------------- @@ -218,6 +239,10 @@ Here is a solution to use ``idle`` command with ``socket_timeout``: cli.noidle() except socket.timeout as err: print(f'{err} (timeout {cli.socket_timeout})') + except (OSError, musicpd.MPDError) as err: + print(f'{err!r}') + if cli._sock is not None: + cli.disconnect() except KeyboardInterrupt: pass @@ -237,3 +262,4 @@ couple of seconds for these commands should never occur). .. _MPD protocol documentation: http://www.musicpd.org/doc/protocol/ +.. vim: spell spelllang=en