File: command_list.py

package info (click to toggle)
mopidy-mpd 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: python: 7,640; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 2,440 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from mopidy_mpd import exceptions, protocol


@protocol.commands.add("command_list_begin", list_command=False)
def command_list_begin(context):
    """
    *musicpd.org, command list section:*

        To facilitate faster adding of files etc. you can pass a list of
        commands all at once using a command list. The command list begins
        with ``command_list_begin`` or ``command_list_ok_begin`` and ends
        with ``command_list_end``.

        It does not execute any commands until the list has ended. The
        return value is whatever the return for a list of commands is. On
        success for all commands, ``OK`` is returned. If a command fails,
        no more commands are executed and the appropriate ``ACK`` error is
        returned. If ``command_list_ok_begin`` is used, ``list_OK`` is
        returned for each successful command executed in the command list.
    """
    context.dispatcher.command_list_receiving = True
    context.dispatcher.command_list_ok = False
    context.dispatcher.command_list = []


@protocol.commands.add("command_list_end", list_command=False)
def command_list_end(context):
    """See :meth:`command_list_begin()`."""
    # TODO: batch consecutive add commands
    if not context.dispatcher.command_list_receiving:
        raise exceptions.MpdUnknownCommand(command="command_list_end")
    context.dispatcher.command_list_receiving = False
    (command_list, context.dispatcher.command_list) = (
        context.dispatcher.command_list,
        [],
    )
    (command_list_ok, context.dispatcher.command_list_ok) = (
        context.dispatcher.command_list_ok,
        False,
    )
    command_list_response = []
    for index, command in enumerate(command_list):
        response = context.dispatcher.handle_request(
            command, current_command_list_index=index
        )
        command_list_response.extend(response)
        if command_list_response and command_list_response[-1].startswith(
            "ACK"
        ):
            return command_list_response
        if command_list_ok:
            command_list_response.append("list_OK")
    return command_list_response


@protocol.commands.add("command_list_ok_begin", list_command=False)
def command_list_ok_begin(context):
    """See :meth:`command_list_begin()`."""
    context.dispatcher.command_list_receiving = True
    context.dispatcher.command_list_ok = True
    context.dispatcher.command_list = []