~naglis/aio_mpv_ipc

288c388039c210cb3f85a2a4bb68ab4bf2cfd270 — Naglis Jonaitis 1 year, 4 months ago 6beba02
Add a few more commands
1 files changed, 103 insertions(+), 13 deletions(-)

M aio_mpv_ipc/__main__.py
M aio_mpv_ipc/__main__.py => aio_mpv_ipc/__main__.py +103 -13
@@ 1,9 1,36 @@
import argparse
import asyncio
import inspect
import pathlib

import aio_mpv_ipc


async def has_chapters(client: aio_mpv_ipc.MpvClient) -> bool:
    try:
        await client.ipc("get_property", "chapter")
    except aio_mpv_ipc.IPCException as e:
        if e.response.get("error") == "property unavailable":
            return False
        raise
    else:
        return True


async def prev_chapter_or_item(client: aio_mpv_ipc.MpvClient, args: argparse.Namespace):
    if await has_chapters(client):
        await client.ipc("add", "chapter", "-1")
    else:
        await client.ipc("playlist-prev", args.mode)


async def next_chapter_or_item(client: aio_mpv_ipc.MpvClient, args: argparse.Namespace):
    if await has_chapters(client):
        await client.ipc("add", "chapter", "1")
    else:
        await client.ipc("playlist-next", args.mode)


COMMANDS = [
    ("play", "unpause playback", [], ["set_property", "pause", False]),
    ("pause", "pause playback", [], ["set_property", "pause", True]),


@@ 36,15 63,77 @@ COMMANDS = [
    (
        "prev",
        "play previous item in the playlist",
        [(["--mode"], {"default": "weak", "choices": ["weak", "force"]})],
        [
            (
                ["--mode"],
                {
                    "default": "weak",
                    "choices": ["weak", "force"],
                    "help": "In weak mode, if the first file on the playlist is "
                    "currently played, do nothing. In force mode, terminate "
                    "playback if the first file is being played. Default: %(default)s",
                },
            )
        ],
        lambda args: ["playlist-prev", args.mode],
    ),
    (
        "next",
        "play next item in the playlist",
        [(["--mode"], {"default": "weak", "choices": ["weak", "force"]})],
        [
            (
                ["--mode"],
                {
                    "default": "weak",
                    "choices": ["weak", "force"],
                    "help": "In weak mode, if the last file on the playlist is "
                    "currently played, do nothing. In force mode, terminate "
                    "playback if there are no more files on the playlist. "
                    "Default: %(default)s",
                },
            )
        ],
        lambda args: ["playlist-next", args.mode],
    ),
    ("prev-chapter", "play the previous chapter", [], ["add", "chapter", "-1"],),
    ("next-chapter", "play next chapter", [], ["add", "chapter", "1"],),
    (
        "prev-chapter-or-item",
        "play previous chapter (if available), "
        "otherwise play the previous item in the playlist",
        [
            (
                ["--mode"],
                {
                    "default": "weak",
                    "choices": ["weak", "force"],
                    "help": "In weak mode, if the first file on the playlist is "
                    "currently played, do nothing. In force mode, terminate "
                    "playback if the first file is being played. Default: %(default)s",
                },
            )
        ],
        prev_chapter_or_item,
    ),
    (
        "next-chapter-or-item",
        "play next chapter (if available), "
        "otherwise play the next item in the playlist",
        [
            (
                ["--mode"],
                {
                    "default": "weak",
                    "choices": ["weak", "force"],
                    "help": "In weak mode, if the last file on the playlist is "
                    "currently played, do nothing. In force mode, terminate "
                    "playback if there are no more files on the playlist. "
                    "Default: %(default)s",
                },
            )
        ],
        next_chapter_or_item,
    ),
    (
        "loadfile",
        "load a given file and play it",


@@ 73,16 162,17 @@ COMMANDS = [
]


def _make_args_func(result):
    def func(args):
        return result
def _make_coro(args_or_func):
    async def coro(client: aio_mpv_ipc.MpvClient, args: argparse.Namespace):
        args = args_or_func(args) if callable(args_or_func) else args_or_func
        await client.ipc(*args)

    return func
    return coro


async def run(args: argparse.Namespace):
    async with aio_mpv_ipc.MpvClient(args.socket_path) as client:
        await client.ipc(*args.ipc_args_getter(args))
        await args.coro(client, args)


def main():


@@ 96,21 186,21 @@ def main():
    parser.add_argument("--debug", help="enable debug mode", action="store_true")
    subparsers = parser.add_subparsers()

    for name, help_, extra_args, args_or_func in COMMANDS:
    for name, help_, extra_args, args_or_coro in COMMANDS:
        subparser = subparsers.add_parser(name, help=help_)
        for args, kwargs in extra_args:
            subparser.add_argument(*args, **kwargs)
        subparser.set_defaults(
            ipc_args_getter=(
                args_or_func
                if callable(args_or_func)
                else _make_args_func(args_or_func)
            coro=(
                args_or_coro
                if inspect.iscoroutinefunction(args_or_coro)
                else _make_coro(args_or_coro)
            )
        )

    args = parser.parse_args()

    if hasattr(args, "ipc_args_getter"):
    if hasattr(args, "coro"):
        asyncio.run(run(args), debug=args.debug)
    else:
        parser.print_help()