~naglis/aio_mpv_ipc

0188d3db1b107dd1ec7ccbbce0be721bc622d6e4 — Naglis Jonaitis 1 year, 11 months ago b7f0548
Add Travis CI
A .flake8 => .flake8 +3 -0
@@ 0,0 1,3 @@
[flake8]
ignore = E203, W503
max-line-length = 88

A .travis.yml => .travis.yml +34 -0
@@ 0,0 1,34 @@
language: python
sudo: false
cache:
    apt: true
    directories:
        - $HOME/.cache/pip

matrix:
    include:
        - python: 3.7
          dist: xenial
          sudo: true
          env: {TOX_ENV: py37, COVERAGE: 1}
        - python: 3.7
          dist: xenial
          sudo: true
          env: {TOX_ENV: qa}

before_install:
  - sudo add-apt-repository ppa:mc3man/mpv-tests -y
  - sudo apt-get update -q
  - sudo apt-get install mpv

install:
    - pip install tox

script:
    - tox -e $TOX_ENV

after_success:
    - "[ ! -z $COVERAGE ] && bash <(curl -s https://codecov.io/bash) || true"

notifications:
    email: false

M aio_mpv_ipc/__init__.py => aio_mpv_ipc/__init__.py +8 -2
@@ 3,14 3,20 @@ Asynchronous MPV IPC client.
"""

from ._client import IPC_ENCODING, MpvClient, start_mpv
from .exceptions import AioMpvIPCException, IPCException

from .exceptions import (
    AioMpvIPCException,
    IPCConnectionException,
    IPCException,
    MpvStartException,
)

__version__ = "0.0.1a2"
__all__ = (
    "MpvClient",
    "start_mpv",
    "AioMpvIPCException",
    "IPCConnectionException",
    "IPCException",
    "IPC_ENCODING",
    "MpvStartException",
)

M aio_mpv_ipc/_client.py => aio_mpv_ipc/_client.py +26 -10
@@ 8,8 8,12 @@ import types
import typing
import uuid

from aio_mpv_ipc.exceptions import AioMpvIPCException, IPCException

from aio_mpv_ipc.exceptions import (
    AioMpvIPCException,
    IPCConnectionException,
    IPCException,
    MpvStartException,
)

logger = logging.getLogger("aio_mpv_ipc")



@@ 39,14 43,25 @@ async def start_mpv(
):
    if isinstance(socket_path, pathlib.Path):
        socket_path = str(socket_path.resolve(strict=False))
    if isinstance(mpv_path, pathlib.Path):
        mpv_path = str(mpv_path)
    mpv = await asyncio.create_subprocess_exec(
        mpv_path, "--input-ipc-server", socket_path, *mpv_args
    )
    logger.debug("Started mpv process with PID: %d", mpv.pid)
    try:
        yield mpv
    except IPCConnectionException as exc:
        if mpv.returncode is not None:
            raise MpvStartException(
                f"mpv exited with status code: {mpv.returncode}"
            ) from exc
        else:
            raise
    except Exception:
        logger.exception("Unexpected error occured while executing mpv")
    finally:
        if terminate:
        if terminate and mpv.returncode is None:
            mpv.terminate()
        await mpv.wait()



@@ 93,9 108,9 @@ class MpvClient:
                    self._socket_path, loop=self._loop
                )
                break
            except ConnectionRefusedError:
            except ConnectionRefusedError as exc:
                if attempt >= self._max_connect_attempts:
                    raise
                    raise IPCConnectionException("Failed to connect to mpv") from exc
                logger.debug(
                    "%d connection attempt failed, will retry after %f seconds",
                    attempt,


@@ 116,11 131,12 @@ class MpvClient:
        if self._writer is not None:
            self._writer.close()

        self._poll_task.cancel()
        try:
            await self._poll_task
        except asyncio.CancelledError:
            pass
        if self._poll_task is not None:
            self._poll_task.cancel()
            try:
                await self._poll_task
            except asyncio.CancelledError:
                pass

    async def _poll(self):
        while True:

M aio_mpv_ipc/exceptions.py => aio_mpv_ipc/exceptions.py +8 -0
@@ 2,6 2,14 @@ class AioMpvIPCException(Exception):
    """Generic exception raised by aio_mpv_ipc."""


class MpvStartException(AioMpvIPCException):
    """Raised when mpv fails to start."""


class IPCConnectionException(AioMpvIPCException):
    """Raised when IPC connection to mpv fails."""


class IPCException(AioMpvIPCException):
    """Raised when an error is return by mpv on an IPC call."""


A pytest.ini => pytest.ini +3 -0
@@ 0,0 1,3 @@
[pytest]
markers =
    mpv: marks tests which start an mpv process
\ No newline at end of file

M tests/test_client.py => tests/test_client.py +6 -4
@@ 1,20 1,21 @@
import asyncio
import contextlib
import tempfile
import distutils
import pathlib
import tempfile

import aio_mpv_ipc
import pytest


TEST_FILE_SILENCE = pathlib.Path(__file__).parent / "test_data" / "silence.ogg"

MPV_PATH = distutils.spawn.find_executable("mpv")

MPV_ARGS = [
    "--idle",  # Do not exit immediately.
    "--no-audio-display",
    "--no-input-terminal",  # Do not intercept keyboard input.
    "--no-input-cursor",
    "--no-terminal",  # No output to terminal.
    "--no-video",
    "--no-resume-playback",
    "--no-osc",


@@ 46,7 47,8 @@ async def mpv_client():
        )
        socket_path = tmp_file.name

        mpv_ctx_mgr = aio_mpv_ipc.start_mpv(socket_path, *MPV_ARGS, terminate=True)
        mpv_ctx_mgr = aio_mpv_ipc.start_mpv(
            socket_path, *MPV_ARGS, terminate=True, mpv_path=MPV_PATH)
        await exit_stack.enter_async_context(mpv_ctx_mgr)

        client = await exit_stack.enter_async_context(