File: __init__.py

package info (click to toggle)
beets 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,016 kB
  • sloc: python: 46,429; javascript: 8,018; xml: 334; sh: 261; makefile: 125
file content (1628 lines) | stat: -rw-r--r-- 56,435 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""A clone of the Music Player Daemon (MPD) that plays music from a
Beets library. Attempts to implement a compatible protocol to allow
use of the wide range of MPD clients.
"""

import inspect
import math
import random
import re
import socket
import sys
import time
import traceback
from string import Template
from typing import TYPE_CHECKING

import beets
import beets.ui
from beets import dbcore, logging
from beets.library import Item
from beets.plugins import BeetsPlugin
from beets.util import as_string, bluelet
from beetsplug._utils import vfs

if TYPE_CHECKING:
    from beets.dbcore.query import Query

log = logging.getLogger(__name__)


try:
    from . import gstplayer
except ImportError as e:
    raise ImportError(
        "Gstreamer Python bindings not found."
        ' Install "gstreamer1.0" and "python-gi" or similar package to use BPD.'
    ) from e

PROTOCOL_VERSION = "0.16.0"
BUFSIZE = 1024

HELLO = f"OK MPD {PROTOCOL_VERSION}"
CLIST_BEGIN = "command_list_begin"
CLIST_VERBOSE_BEGIN = "command_list_ok_begin"
CLIST_END = "command_list_end"
RESP_OK = "OK"
RESP_CLIST_VERBOSE = "list_OK"
RESP_ERR = "ACK"

NEWLINE = "\n"

ERROR_NOT_LIST = 1
ERROR_ARG = 2
ERROR_PASSWORD = 3
ERROR_PERMISSION = 4
ERROR_UNKNOWN = 5
ERROR_NO_EXIST = 50
ERROR_PLAYLIST_MAX = 51
ERROR_SYSTEM = 52
ERROR_PLAYLIST_LOAD = 53
ERROR_UPDATE_ALREADY = 54
ERROR_PLAYER_SYNC = 55
ERROR_EXIST = 56

VOLUME_MIN = 0
VOLUME_MAX = 100

SAFE_COMMANDS = (
    # Commands that are available when unauthenticated.
    "close",
    "commands",
    "notcommands",
    "password",
    "ping",
)

# List of subsystems/events used by the `idle` command.
SUBSYSTEMS = [
    "update",
    "player",
    "mixer",
    "options",
    "playlist",
    "database",
    # Related to unsupported commands:
    "stored_playlist",
    "output",
    "subscription",
    "sticker",
    "message",
    "partition",
]


# Error-handling, exceptions, parameter parsing.


class BPDError(Exception):
    """An error that should be exposed to the client to the BPD
    server.
    """

    def __init__(self, code, message, cmd_name="", index=0):
        self.code = code
        self.message = message
        self.cmd_name = cmd_name
        self.index = index

    template = Template("$resp [$code@$index] {$cmd_name} $message")

    def response(self):
        """Returns a string to be used as the response code for the
        erring command.
        """
        return self.template.substitute(
            {
                "resp": RESP_ERR,
                "code": self.code,
                "index": self.index,
                "cmd_name": self.cmd_name,
                "message": self.message,
            }
        )


def make_bpd_error(s_code, s_message):
    """Create a BPDError subclass for a static code and message."""

    class NewBPDError(BPDError):
        code = s_code
        message = s_message
        cmd_name = ""
        index = 0

        def __init__(self):
            pass

    return NewBPDError


ArgumentTypeError = make_bpd_error(ERROR_ARG, "invalid type for argument")
ArgumentIndexError = make_bpd_error(ERROR_ARG, "argument out of range")
ArgumentNotFoundError = make_bpd_error(ERROR_NO_EXIST, "argument not found")


def cast_arg(t, val):
    """Attempts to call t on val, raising a ArgumentTypeError
    on ValueError.

    If 't' is the special string 'intbool', attempts to cast first
    to an int and then to a bool (i.e., 1=True, 0=False).
    """
    if t == "intbool":
        return cast_arg(bool, cast_arg(int, val))
    else:
        try:
            return t(val)
        except ValueError:
            raise ArgumentTypeError()


class BPDCloseError(Exception):
    """Raised by a command invocation to indicate that the connection
    should be closed.
    """


class BPDIdleError(Exception):
    """Raised by a command to indicate the client wants to enter the idle state
    and should be notified when a relevant event happens.
    """

    def __init__(self, subsystems):
        super().__init__()
        self.subsystems = set(subsystems)


# Generic server infrastructure, implementing the basic protocol.


class BaseServer:
    """A MPD-compatible music player server.

    The functions with the `cmd_` prefix are invoked in response to
    client commands. For instance, if the client says `status`,
    `cmd_status` will be invoked. The arguments to the client's commands
    are used as function arguments following the connection issuing the
    command. The functions may send data on the connection. They may
    also raise BPDError exceptions to report errors.

    This is a generic superclass and doesn't support many commands.
    """

    def __init__(self, host, port, password, ctrl_port, log, ctrl_host=None):
        """Create a new server bound to address `host` and listening
        on port `port`. If `password` is given, it is required to do
        anything significant on the server.
        A separate control socket is established listening to `ctrl_host` on
        port `ctrl_port` which is used to forward notifications from the player
        and can be sent debug commands (e.g. using netcat).
        """
        self.host, self.port, self.password = host, port, password
        self.ctrl_host, self.ctrl_port = ctrl_host or host, ctrl_port
        self.ctrl_sock = None
        self._log = log

        # Default server values.
        self.random = False
        self.repeat = False
        self.consume = False
        self.single = False
        self.volume = VOLUME_MAX
        self.crossfade = 0
        self.mixrampdb = 0.0
        self.mixrampdelay = float("nan")
        self.replay_gain_mode = "off"
        self.playlist = []
        self.playlist_version = 0
        self.current_index = -1
        self.paused = False
        self.error = None

        # Current connections
        self.connections = set()

        # Object for random numbers generation
        self.random_obj = random.Random()

    def connect(self, conn):
        """A new client has connected."""
        self.connections.add(conn)

    def disconnect(self, conn):
        """Client has disconnected; clean up residual state."""
        self.connections.remove(conn)

    def run(self):
        """Block and start listening for connections from clients. An
        interrupt (^C) closes the server.
        """
        self.startup_time = time.time()

        def start():
            yield bluelet.spawn(
                bluelet.server(
                    self.ctrl_host,
                    self.ctrl_port,
                    ControlConnection.handler(self),
                )
            )
            yield bluelet.server(
                self.host, self.port, MPDConnection.handler(self)
            )

        bluelet.run(start())

    def dispatch_events(self):
        """If any clients have idle events ready, send them."""
        # We need a copy of `self.connections` here since clients might
        # disconnect once we try and send to them, changing `self.connections`.
        for conn in list(self.connections):
            yield bluelet.spawn(conn.send_notifications())

    def _ctrl_send(self, message):
        """Send some data over the control socket.
        If it's our first time, open the socket. The message should be a
        string without a terminal newline.
        """
        if not self.ctrl_sock:
            self.ctrl_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.ctrl_sock.connect((self.ctrl_host, self.ctrl_port))
        self.ctrl_sock.sendall((f"{message}\n").encode("utf-8"))

    def _send_event(self, event):
        """Notify subscribed connections of an event."""
        for conn in self.connections:
            conn.notify(event)

    def _item_info(self, item):
        """An abstract method that should response lines containing a
        single song's metadata.
        """
        raise NotImplementedError

    def _item_id(self, item):
        """An abstract method returning the integer id for an item."""
        raise NotImplementedError

    def _id_to_index(self, track_id):
        """Searches the playlist for a song with the given id and
        returns its index in the playlist.
        """
        track_id = cast_arg(int, track_id)
        for index, track in enumerate(self.playlist):
            if self._item_id(track) == track_id:
                return index
        # Loop finished with no track found.
        raise ArgumentNotFoundError()

    def _random_idx(self):
        """Returns a random index different from the current one.
        If there are no songs in the playlist it returns -1.
        If there is only one song in the playlist it returns 0.
        """
        if len(self.playlist) < 2:
            return len(self.playlist) - 1
        new_index = self.random_obj.randint(0, len(self.playlist) - 1)
        while new_index == self.current_index:
            new_index = self.random_obj.randint(0, len(self.playlist) - 1)
        return new_index

    def _succ_idx(self):
        """Returns the index for the next song to play.
        It also considers random, single and repeat flags.
        No boundaries are checked.
        """
        if self.repeat and self.single:
            return self.current_index
        if self.random:
            return self._random_idx()
        return self.current_index + 1

    def _prev_idx(self):
        """Returns the index for the previous song to play.
        It also considers random and repeat flags.
        No boundaries are checked.
        """
        if self.repeat and self.single:
            return self.current_index
        if self.random:
            return self._random_idx()
        return self.current_index - 1

    def cmd_ping(self, conn):
        """Succeeds."""
        pass

    def cmd_idle(self, conn, *subsystems):
        subsystems = subsystems or SUBSYSTEMS
        for system in subsystems:
            if system not in SUBSYSTEMS:
                raise BPDError(ERROR_ARG, f"Unrecognised idle event: {system}")
        raise BPDIdleError(subsystems)  # put the connection into idle mode

    def cmd_kill(self, conn):
        """Exits the server process."""
        sys.exit(0)

    def cmd_close(self, conn):
        """Closes the connection."""
        raise BPDCloseError()

    def cmd_password(self, conn, password):
        """Attempts password authentication."""
        if password == self.password:
            conn.authenticated = True
        else:
            conn.authenticated = False
            raise BPDError(ERROR_PASSWORD, "incorrect password")

    def cmd_commands(self, conn):
        """Lists the commands available to the user."""
        if self.password and not conn.authenticated:
            # Not authenticated. Show limited list of commands.
            for cmd in SAFE_COMMANDS:
                yield f"command: {cmd}"

        else:
            # Authenticated. Show all commands.
            for func in dir(self):
                if func.startswith("cmd_"):
                    yield f"command: {func[4:]}"

    def cmd_notcommands(self, conn):
        """Lists all unavailable commands."""
        if self.password and not conn.authenticated:
            # Not authenticated. Show privileged commands.
            for func in dir(self):
                if func.startswith("cmd_"):
                    cmd = func[4:]
                    if cmd not in SAFE_COMMANDS:
                        yield f"command: {cmd}"

        else:
            # Authenticated. No commands are unavailable.
            pass

    def cmd_status(self, conn):
        """Returns some status information for use with an
        implementation of cmd_status.

        Gives a list of response-lines for: volume, repeat, random,
        playlist, playlistlength, and xfade.
        """
        yield (
            f"repeat: {int(self.repeat)}",
            f"random: {int(self.random)}",
            f"consume: {int(self.consume)}",
            f"single: {int(self.single)}",
            f"playlist: {self.playlist_version}",
            f"playlistlength: {len(self.playlist)}",
            f"mixrampdb: {self.mixrampdb}",
        )

        if self.volume > 0:
            yield f"volume: {self.volume}"

        if not math.isnan(self.mixrampdelay):
            yield f"mixrampdelay: {self.mixrampdelay}"
        if self.crossfade > 0:
            yield f"xfade: {self.crossfade}"

        if self.current_index == -1:
            state = "stop"
        elif self.paused:
            state = "pause"
        else:
            state = "play"
        yield f"state: {state}"

        if self.current_index != -1:  # i.e., paused or playing
            current_id = self._item_id(self.playlist[self.current_index])
            yield f"song: {self.current_index}"
            yield f"songid: {current_id}"
            if len(self.playlist) > self.current_index + 1:
                # If there's a next song, report its index too.
                next_id = self._item_id(self.playlist[self.current_index + 1])
                yield f"nextsong: {self.current_index + 1}"
                yield f"nextsongid: {next_id}"

        if self.error:
            yield f"error: {self.error}"

    def cmd_clearerror(self, conn):
        """Removes the persistent error state of the server. This
        error is set when a problem arises not in response to a
        command (for instance, when playing a file).
        """
        self.error = None

    def cmd_random(self, conn, state):
        """Set or unset random (shuffle) mode."""
        self.random = cast_arg("intbool", state)
        self._send_event("options")

    def cmd_repeat(self, conn, state):
        """Set or unset repeat mode."""
        self.repeat = cast_arg("intbool", state)
        self._send_event("options")

    def cmd_consume(self, conn, state):
        """Set or unset consume mode."""
        self.consume = cast_arg("intbool", state)
        self._send_event("options")

    def cmd_single(self, conn, state):
        """Set or unset single mode."""
        # TODO support oneshot in addition to 0 and 1 [MPD 0.20]
        self.single = cast_arg("intbool", state)
        self._send_event("options")

    def cmd_setvol(self, conn, vol):
        """Set the player's volume level (0-100)."""
        vol = cast_arg(int, vol)
        if vol < VOLUME_MIN or vol > VOLUME_MAX:
            raise BPDError(ERROR_ARG, "volume out of range")
        self.volume = vol
        self._send_event("mixer")

    def cmd_volume(self, conn, vol_delta):
        """Deprecated command to change the volume by a relative amount."""
        vol_delta = cast_arg(int, vol_delta)
        return self.cmd_setvol(conn, self.volume + vol_delta)

    def cmd_crossfade(self, conn, crossfade):
        """Set the number of seconds of crossfading."""
        crossfade = cast_arg(int, crossfade)
        if crossfade < 0:
            raise BPDError(ERROR_ARG, "crossfade time must be nonnegative")
        self._log.warning("crossfade is not implemented in bpd")
        self.crossfade = crossfade
        self._send_event("options")

    def cmd_mixrampdb(self, conn, db):
        """Set the mixramp normalised max volume in dB."""
        db = cast_arg(float, db)
        if db > 0:
            raise BPDError(ERROR_ARG, "mixrampdb time must be negative")
        self._log.warning("mixramp is not implemented in bpd")
        self.mixrampdb = db
        self._send_event("options")

    def cmd_mixrampdelay(self, conn, delay):
        """Set the mixramp delay in seconds."""
        delay = cast_arg(float, delay)
        if delay < 0:
            raise BPDError(ERROR_ARG, "mixrampdelay time must be nonnegative")
        self._log.warning("mixramp is not implemented in bpd")
        self.mixrampdelay = delay
        self._send_event("options")

    def cmd_replay_gain_mode(self, conn, mode):
        """Set the replay gain mode."""
        if mode not in ["off", "track", "album", "auto"]:
            raise BPDError(ERROR_ARG, "Unrecognised replay gain mode")
        self._log.warning("replay gain is not implemented in bpd")
        self.replay_gain_mode = mode
        self._send_event("options")

    def cmd_replay_gain_status(self, conn):
        """Get the replaygain mode."""
        yield f"replay_gain_mode: {self.replay_gain_mode}"

    def cmd_clear(self, conn):
        """Clear the playlist."""
        self.playlist = []
        self.playlist_version += 1
        self.cmd_stop(conn)
        self._send_event("playlist")

    def cmd_delete(self, conn, index):
        """Remove the song at index from the playlist."""
        index = cast_arg(int, index)
        try:
            del self.playlist[index]
        except IndexError:
            raise ArgumentIndexError()
        self.playlist_version += 1

        if self.current_index == index:  # Deleted playing song.
            self.cmd_stop(conn)
        elif index < self.current_index:  # Deleted before playing.
            # Shift playing index down.
            self.current_index -= 1
        self._send_event("playlist")

    def cmd_deleteid(self, conn, track_id):
        self.cmd_delete(conn, self._id_to_index(track_id))

    def cmd_move(self, conn, idx_from, idx_to):
        """Move a track in the playlist."""
        idx_from = cast_arg(int, idx_from)
        idx_to = cast_arg(int, idx_to)
        try:
            track = self.playlist.pop(idx_from)
            self.playlist.insert(idx_to, track)
        except IndexError:
            raise ArgumentIndexError()

        # Update currently-playing song.
        if idx_from == self.current_index:
            self.current_index = idx_to
        elif idx_from < self.current_index <= idx_to:
            self.current_index -= 1
        elif idx_from > self.current_index >= idx_to:
            self.current_index += 1

        self.playlist_version += 1
        self._send_event("playlist")

    def cmd_moveid(self, conn, idx_from, idx_to):
        idx_from = self._id_to_index(idx_from)
        return self.cmd_move(conn, idx_from, idx_to)

    def cmd_swap(self, conn, i, j):
        """Swaps two tracks in the playlist."""
        i = cast_arg(int, i)
        j = cast_arg(int, j)
        try:
            track_i = self.playlist[i]
            track_j = self.playlist[j]
        except IndexError:
            raise ArgumentIndexError()

        self.playlist[j] = track_i
        self.playlist[i] = track_j

        # Update currently-playing song.
        if self.current_index == i:
            self.current_index = j
        elif self.current_index == j:
            self.current_index = i

        self.playlist_version += 1
        self._send_event("playlist")

    def cmd_swapid(self, conn, i_id, j_id):
        i = self._id_to_index(i_id)
        j = self._id_to_index(j_id)
        return self.cmd_swap(conn, i, j)

    def cmd_urlhandlers(self, conn):
        """Indicates supported URL schemes. None by default."""
        pass

    def cmd_playlistinfo(self, conn, index=None):
        """Gives metadata information about the entire playlist or a
        single track, given by its index.
        """
        if index is None:
            for track in self.playlist:
                yield self._item_info(track)
        else:
            indices = self._parse_range(index, accept_single_number=True)
            try:
                tracks = [self.playlist[i] for i in indices]
            except IndexError:
                raise ArgumentIndexError()
            for track in tracks:
                yield self._item_info(track)

    def cmd_playlistid(self, conn, track_id=None):
        if track_id is not None:
            track_id = cast_arg(int, track_id)
            track_id = self._id_to_index(track_id)
        return self.cmd_playlistinfo(conn, track_id)

    def cmd_plchanges(self, conn, version):
        """Sends playlist changes since the given version.

        This is a "fake" implementation that ignores the version and
        just returns the entire playlist (rather like version=0). This
        seems to satisfy many clients.
        """
        return self.cmd_playlistinfo(conn)

    def cmd_plchangesposid(self, conn, version):
        """Like plchanges, but only sends position and id.

        Also a dummy implementation.
        """
        for idx, track in enumerate(self.playlist):
            yield f"cpos: {idx}"
            yield f"Id: {track.id}"

    def cmd_currentsong(self, conn):
        """Sends information about the currently-playing song."""
        if self.current_index != -1:  # -1 means stopped.
            track = self.playlist[self.current_index]
            yield self._item_info(track)

    def cmd_next(self, conn):
        """Advance to the next song in the playlist."""
        old_index = self.current_index
        self.current_index = self._succ_idx()
        if self.consume:
            # TODO how does consume interact with single+repeat?
            self.playlist.pop(old_index)
            if self.current_index > old_index:
                self.current_index -= 1
            self.playlist_version += 1
            self._send_event("playlist")
        if self.current_index >= len(self.playlist):
            # Fallen off the end. Move to stopped state or loop.
            if self.repeat:
                self.current_index = -1
                return self.cmd_play(conn)
            return self.cmd_stop(conn)
        elif self.single and not self.repeat:
            return self.cmd_stop(conn)
        else:
            return self.cmd_play(conn)

    def cmd_previous(self, conn):
        """Step back to the last song."""
        old_index = self.current_index
        self.current_index = self._prev_idx()
        if self.consume:
            self.playlist.pop(old_index)
        if self.current_index < 0:
            if self.repeat:
                self.current_index = len(self.playlist) - 1
            else:
                self.current_index = 0
        return self.cmd_play(conn)

    def cmd_pause(self, conn, state=None):
        """Set the pause state playback."""
        if state is None:
            self.paused = not self.paused  # Toggle.
        else:
            self.paused = cast_arg("intbool", state)
        self._send_event("player")

    def cmd_play(self, conn, index=-1):
        """Begin playback, possibly at a specified playlist index."""
        index = cast_arg(int, index)

        if index < -1 or index >= len(self.playlist):
            raise ArgumentIndexError()

        if index == -1:  # No index specified: start where we are.
            if not self.playlist:  # Empty playlist: stop immediately.
                return self.cmd_stop(conn)
            if self.current_index == -1:  # No current song.
                self.current_index = 0  # Start at the beginning.
            # If we have a current song, just stay there.

        else:  # Start with the specified index.
            self.current_index = index

        self.paused = False
        self._send_event("player")

    def cmd_playid(self, conn, track_id=0):
        track_id = cast_arg(int, track_id)
        if track_id == -1:
            index = -1
        else:
            index = self._id_to_index(track_id)
        return self.cmd_play(conn, index)

    def cmd_stop(self, conn):
        """Stop playback."""
        self.current_index = -1
        self.paused = False
        self._send_event("player")

    def cmd_seek(self, conn, index, pos):
        """Seek to a specified point in a specified song."""
        index = cast_arg(int, index)
        if index < 0 or index >= len(self.playlist):
            raise ArgumentIndexError()
        self.current_index = index
        self._send_event("player")

    def cmd_seekid(self, conn, track_id, pos):
        index = self._id_to_index(track_id)
        return self.cmd_seek(conn, index, pos)

    # Additions to the MPD protocol.

    def cmd_crash(self, conn):
        """Deliberately trigger a TypeError for testing purposes.
        We want to test that the server properly responds with ERROR_SYSTEM
        without crashing, and that this is not treated as ERROR_ARG (since it
        is caused by a programming error, not a protocol error).
        """
        raise TypeError


class Connection:
    """A connection between a client and the server."""

    def __init__(self, server, sock):
        """Create a new connection for the accepted socket `client`."""
        self.server = server
        self.sock = sock
        self.address = ":".join(map(str, sock.sock.getpeername()))

    def debug(self, message, kind=" "):
        """Log a debug message about this connection."""
        self.server._log.debug("{}[{.address}]: {}", kind, self, message)

    def run(self):
        pass

    def send(self, lines):
        """Send lines, which which is either a single string or an
        iterable consisting of strings, to the client. A newline is
        added after every string. Returns a Bluelet event that sends
        the data.
        """
        if isinstance(lines, str):
            lines = [lines]
        out = NEWLINE.join(lines) + NEWLINE
        for line in out.split(NEWLINE)[:-1]:
            self.debug(line, kind=">")
        if isinstance(out, str):
            out = out.encode("utf-8")
        return self.sock.sendall(out)

    @classmethod
    def handler(cls, server):
        def _handle(sock):
            """Creates a new `Connection` and runs it."""
            return cls(server, sock).run()

        return _handle


class MPDConnection(Connection):
    """A connection that receives commands from an MPD-compatible client."""

    def __init__(self, server, sock):
        """Create a new connection for the accepted socket `client`."""
        super().__init__(server, sock)
        self.authenticated = False
        self.notifications = set()
        self.idle_subscriptions = set()

    def do_command(self, command):
        """A coroutine that runs the given command and sends an
        appropriate response."""
        try:
            yield bluelet.call(command.run(self))
        except BPDError as e:
            # Send the error.
            yield self.send(e.response())
        else:
            # Send success code.
            yield self.send(RESP_OK)

    def disconnect(self):
        """The connection has closed for any reason."""
        self.server.disconnect(self)
        self.debug("disconnected", kind="*")

    def notify(self, event):
        """Queue up an event for sending to this client."""
        self.notifications.add(event)

    def send_notifications(self, force_close_idle=False):
        """Send the client any queued events now."""
        pending = self.notifications.intersection(self.idle_subscriptions)
        try:
            for event in pending:
                yield self.send(f"changed: {event}")
            if pending or force_close_idle:
                self.idle_subscriptions = set()
                self.notifications = self.notifications.difference(pending)
                yield self.send(RESP_OK)
        except bluelet.SocketClosedError:
            self.disconnect()  # Client disappeared.

    def run(self):
        """Send a greeting to the client and begin processing commands
        as they arrive.
        """
        self.debug("connected", kind="*")
        self.server.connect(self)
        yield self.send(HELLO)

        clist = None  # Initially, no command list is being constructed.
        while True:
            line = yield self.sock.readline()
            if not line:
                self.disconnect()  # Client disappeared.
                break
            line = line.strip()
            if not line:
                err = BPDError(ERROR_UNKNOWN, "No command given")
                yield self.send(err.response())
                self.disconnect()  # Client sent a blank line.
                break
            line = line.decode("utf8")  # MPD protocol uses UTF-8.
            for line in line.split(NEWLINE):
                self.debug(line, kind="<")

            if self.idle_subscriptions:
                # The connection is in idle mode.
                if line == "noidle":
                    yield bluelet.call(self.send_notifications(True))
                else:
                    err = BPDError(
                        ERROR_UNKNOWN, f"Got command while idle: {line}"
                    )
                    yield self.send(err.response())
                    break
                continue
            if line == "noidle":
                # When not in idle, this command sends no response.
                continue

            if clist is not None:
                # Command list already opened.
                if line == CLIST_END:
                    yield bluelet.call(self.do_command(clist))
                    clist = None  # Clear the command list.
                    yield bluelet.call(self.server.dispatch_events())
                else:
                    clist.append(Command(line))

            elif line == CLIST_BEGIN or line == CLIST_VERBOSE_BEGIN:
                # Begin a command list.
                clist = CommandList([], line == CLIST_VERBOSE_BEGIN)

            else:
                # Ordinary command.
                try:
                    yield bluelet.call(self.do_command(Command(line)))
                except BPDCloseError:
                    # Command indicates that the conn should close.
                    self.sock.close()
                    self.disconnect()  # Client explicitly closed.
                    return
                except BPDIdleError as e:
                    self.idle_subscriptions = e.subsystems
                    self.debug(f"awaiting: {' '.join(e.subsystems)}", kind="z")
                yield bluelet.call(self.server.dispatch_events())


class ControlConnection(Connection):
    """A connection used to control BPD for debugging and internal events."""

    def __init__(self, server, sock):
        """Create a new connection for the accepted socket `client`."""
        super().__init__(server, sock)

    def debug(self, message, kind=" "):
        self.server._log.debug("CTRL {}[{.address}]: {}", kind, self, message)

    def run(self):
        """Listen for control commands and delegate to `ctrl_*` methods."""
        self.debug("connected", kind="*")
        while True:
            line = yield self.sock.readline()
            if not line:
                break  # Client disappeared.
            line = line.strip()
            if not line:
                break  # Client sent a blank line.
            line = line.decode("utf8")  # Protocol uses UTF-8.
            for line in line.split(NEWLINE):
                self.debug(line, kind="<")
            command = Command(line)
            try:
                func = command.delegate("ctrl_", self)
                yield bluelet.call(func(*command.args))
            except (AttributeError, TypeError) as e:
                yield self.send(f"ERROR: {e.args[0]}")
            except Exception:
                yield self.send(
                    ["ERROR: server error", traceback.format_exc().rstrip()]
                )

    def ctrl_play_finished(self):
        """Callback from the player signalling a song finished playing."""
        yield bluelet.call(self.server.dispatch_events())

    def ctrl_profile(self):
        """Memory profiling for debugging."""
        from guppy import hpy

        heap = hpy().heap()
        yield self.send(heap)

    def ctrl_nickname(self, oldlabel, newlabel):
        """Rename a client in the log messages."""
        for c in self.server.connections:
            if c.address == oldlabel:
                c.address = newlabel
                break
        else:
            yield self.send(f"ERROR: no such client: {oldlabel}")


class Command:
    """A command issued by the client for processing by the server."""

    command_re = re.compile(r"^([^ \t]+)[ \t]*")
    arg_re = re.compile(r'"((?:\\"|[^"])+)"|([^ \t"]+)')

    def __init__(self, s):
        """Creates a new `Command` from the given string, `s`, parsing
        the string for command name and arguments.
        """
        command_match = self.command_re.match(s)
        self.name = command_match.group(1)

        self.args = []
        arg_matches = self.arg_re.findall(s[command_match.end() :])
        for match in arg_matches:
            if match[0]:
                # Quoted argument.
                arg = match[0]
                arg = arg.replace('\\"', '"').replace("\\\\", "\\")
            else:
                # Unquoted argument.
                arg = match[1]
            self.args.append(arg)

    def delegate(self, prefix, target, extra_args=0):
        """Get the target method that corresponds to this command.
        The `prefix` is prepended to the command name and then the resulting
        name is used to search `target` for a method with a compatible number
        of arguments.
        """
        # Attempt to get correct command function.
        func_name = f"{prefix}{self.name}"
        if not hasattr(target, func_name):
            raise AttributeError(f'unknown command "{self.name}"')
        func = getattr(target, func_name)

        argspec = inspect.getfullargspec(func)

        # Check that `func` is able to handle the number of arguments sent
        # by the client (so we can raise ERROR_ARG instead of ERROR_SYSTEM).
        # Maximum accepted arguments: argspec includes "self".
        max_args = len(argspec.args) - 1 - extra_args
        # Minimum accepted arguments: some arguments might be optional.
        min_args = max_args
        if argspec.defaults:
            min_args -= len(argspec.defaults)
        wrong_num = (len(self.args) > max_args) or (len(self.args) < min_args)
        # If the command accepts a variable number of arguments skip the check.
        if wrong_num and not argspec.varargs:
            raise TypeError(
                f'wrong number of arguments for "{self.name}"',
                self.name,
            )

        return func

    def run(self, conn):
        """A coroutine that executes the command on the given
        connection.
        """
        try:
            # `conn` is an extra argument to all cmd handlers.
            func = self.delegate("cmd_", conn.server, extra_args=1)
        except AttributeError as e:
            raise BPDError(ERROR_UNKNOWN, e.args[0])
        except TypeError as e:
            raise BPDError(ERROR_ARG, e.args[0], self.name)

        # Ensure we have permission for this command.
        if (
            conn.server.password
            and not conn.authenticated
            and self.name not in SAFE_COMMANDS
        ):
            raise BPDError(ERROR_PERMISSION, "insufficient privileges")

        try:
            args = [conn] + self.args
            results = func(*args)
            if results:
                for data in results:
                    yield conn.send(data)

        except BPDError as e:
            # An exposed error. Set the command name and then let
            # the Connection handle it.
            e.cmd_name = self.name
            raise e

        except BPDCloseError:
            # An indication that the connection should close. Send
            # it on the Connection.
            raise

        except BPDIdleError:
            raise

        except Exception:
            # An "unintentional" error. Hide it from the client.
            conn.server._log.error("{}", traceback.format_exc())
            raise BPDError(ERROR_SYSTEM, "server error", self.name)


class CommandList(list[Command]):
    """A list of commands issued by the client for processing by the
    server. May be verbose, in which case the response is delimited, or
    not. Should be a list of `Command` objects.
    """

    def __init__(self, sequence=None, verbose=False):
        """Create a new `CommandList` from the given sequence of
        `Command`s. If `verbose`, this is a verbose command list.
        """
        if sequence:
            for item in sequence:
                self.append(item)
        self.verbose = verbose

    def run(self, conn):
        """Coroutine executing all the commands in this list."""
        for i, command in enumerate(self):
            try:
                yield bluelet.call(command.run(conn))
            except BPDError as e:
                # If the command failed, stop executing.
                e.index = i  # Give the error the correct index.
                raise e

            # Otherwise, possibly send the output delimiter if we're in a
            # verbose ("OK") command list.
            if self.verbose:
                yield conn.send(RESP_CLIST_VERBOSE)


# A subclass of the basic, protocol-handling server that actually plays
# music.


class Server(BaseServer):
    """An MPD-compatible server using GStreamer to play audio and beets
    to store its library.
    """

    def __init__(self, library, host, port, password, ctrl_port, log):
        log.info("Starting server...")
        super().__init__(host, port, password, ctrl_port, log)
        self.lib = library
        self.player = gstplayer.GstPlayer(self.play_finished)
        self.cmd_update(None)
        log.info("Server ready and listening on {}:{}", host, port)
        log.debug("Listening for control signals on {}:{}", host, ctrl_port)

    def run(self):
        self.player.run()
        super().run()

    def play_finished(self):
        """A callback invoked every time our player finishes a track."""
        self.cmd_next(None)
        self._ctrl_send("play_finished")

    # Metadata helper functions.

    def _item_info(self, item):
        info_lines = [
            f"file: {as_string(item.destination(relative_to_libdir=True))}",
            f"Time: {int(item.length)}",
            "duration: {item.length:.3f}",
            f"Id: {item.id}",
        ]

        try:
            pos = self._id_to_index(item.id)
            info_lines.append(f"Pos: {pos}")
        except ArgumentNotFoundError:
            # Don't include position if not in playlist.
            pass

        for tagtype, field in self.tagtype_map.items():
            info_lines.append(f"{tagtype}: {getattr(item, field)}")

        return info_lines

    def _parse_range(self, items, accept_single_number=False):
        """Convert a range of positions to a list of item info.
        MPD specifies ranges as START:STOP (endpoint excluded) for some
        commands. Sometimes a single number can be provided instead.
        """
        try:
            start, stop = str(items).split(":", 1)
        except ValueError:
            if accept_single_number:
                return [cast_arg(int, items)]
            raise BPDError(ERROR_ARG, "bad range syntax")
        start = cast_arg(int, start)
        stop = cast_arg(int, stop)
        return range(start, stop)

    def _item_id(self, item):
        return item.id

    # Database updating.

    def cmd_update(self, conn, path="/"):
        """Updates the catalog to reflect the current database state."""
        # Path is ignored. Also, the real MPD does this asynchronously;
        # this is done inline.
        self._log.debug("Building directory tree...")
        self.tree = vfs.libtree(self.lib)
        self._log.debug("Finished building directory tree.")
        self.updated_time = time.time()
        self._send_event("update")
        self._send_event("database")

    # Path (directory tree) browsing.

    def _resolve_path(self, path):
        """Returns a VFS node or an item ID located at the path given.
        If the path does not exist, raises a
        """
        components = path.split("/")
        node = self.tree

        for component in components:
            if not component:
                continue

            if isinstance(node, int):
                # We're trying to descend into a file node.
                raise ArgumentNotFoundError()

            if component in node.files:
                node = node.files[component]
            elif component in node.dirs:
                node = node.dirs[component]
            else:
                raise ArgumentNotFoundError()

        return node

    def _path_join(self, p1, p2):
        """Smashes together two BPD paths."""
        out = f"{p1}/{p2}"
        return out.replace("//", "/").replace("//", "/")

    def cmd_lsinfo(self, conn, path="/"):
        """Sends info on all the items in the path."""
        node = self._resolve_path(path)
        if isinstance(node, int):
            # Trying to list a track.
            raise BPDError(ERROR_ARG, "this is not a directory")
        else:
            for name, itemid in iter(sorted(node.files.items())):
                item = self.lib.get_item(itemid)
                yield self._item_info(item)
            for name, _ in iter(sorted(node.dirs.items())):
                dirpath = self._path_join(path, name)
                if dirpath.startswith("/"):
                    # Strip leading slash (libmpc rejects this).
                    dirpath = dirpath[1:]
                yield f"directory: {dirpath}"

    def _listall(self, basepath, node, info=False):
        """Helper function for recursive listing. If info, show
        tracks' complete info; otherwise, just show items' paths.
        """
        if isinstance(node, int):
            # List a single file.
            if info:
                item = self.lib.get_item(node)
                yield self._item_info(item)
            else:
                yield f"file: {basepath}"
        else:
            # List a directory. Recurse into both directories and files.
            for name, itemid in sorted(node.files.items()):
                newpath = self._path_join(basepath, name)
                # "yield from"
                yield from self._listall(newpath, itemid, info)
            for name, subdir in sorted(node.dirs.items()):
                newpath = self._path_join(basepath, name)
                yield f"directory: {newpath}"
                yield from self._listall(newpath, subdir, info)

    def cmd_listall(self, conn, path="/"):
        """Send the paths all items in the directory, recursively."""
        return self._listall(path, self._resolve_path(path), False)

    def cmd_listallinfo(self, conn, path="/"):
        """Send info on all the items in the directory, recursively."""
        return self._listall(path, self._resolve_path(path), True)

    # Playlist manipulation.

    def _all_items(self, node):
        """Generator yielding all items under a VFS node."""
        if isinstance(node, int):
            # Could be more efficient if we built up all the IDs and
            # then issued a single SELECT.
            yield self.lib.get_item(node)
        else:
            # Recurse into a directory.
            for name, itemid in sorted(node.files.items()):
                # "yield from"
                yield from self._all_items(itemid)
            for name, subdir in sorted(node.dirs.items()):
                yield from self._all_items(subdir)

    def _add(self, path, send_id=False):
        """Adds a track or directory to the playlist, specified by the
        path. If `send_id`, write each item's id to the client.
        """
        for item in self._all_items(self._resolve_path(path)):
            self.playlist.append(item)
            if send_id:
                yield f"Id: {item.id}"
        self.playlist_version += 1
        self._send_event("playlist")

    def cmd_add(self, conn, path):
        """Adds a track or directory to the playlist, specified by a
        path.
        """
        return self._add(path, False)

    def cmd_addid(self, conn, path):
        """Same as `cmd_add` but sends an id back to the client."""
        return self._add(path, True)

    # Server info.

    def cmd_status(self, conn):
        yield from super().cmd_status(conn)
        if self.current_index > -1:
            item = self.playlist[self.current_index]

            yield (
                f"bitrate: {item.bitrate / 1000}",
                f"audio: {item.samplerate}:{item.bitdepth}:{item.channels}",
            )

            (pos, total) = self.player.time()
            yield (
                f"time: {int(pos)}:{int(total)}",
                "elapsed: " + f"{pos:.3f}",
                "duration: " + f"{total:.3f}",
            )

        # Also missing 'updating_db'.

    def cmd_stats(self, conn):
        """Sends some statistics about the library."""
        with self.lib.transaction() as tx:
            statement = (
                "SELECT COUNT(DISTINCT artist), "
                "COUNT(DISTINCT album), "
                "COUNT(id), "
                "SUM(length) "
                "FROM items"
            )
            artists, albums, songs, totaltime = tx.query(statement)[0]

        yield (
            f"artists: {artists}",
            f"albums: {albums}",
            f"songs: {songs}",
            f"uptime: {int(time.time() - self.startup_time)}",
            "playtime: 0",  # Missing.
            f"db_playtime: {int(totaltime)}",
            f"db_update: {int(self.updated_time)}",
        )

    def cmd_decoders(self, conn):
        """Send list of supported decoders and formats."""
        decoders = self.player.get_decoders()
        for name, (mimes, exts) in decoders.items():
            yield f"plugin: {name}"
            for ext in exts:
                yield f"suffix: {ext}"
            for mime in mimes:
                yield f"mime_type: {mime}"

    # Searching.

    tagtype_map = {
        "Artist": "artist",
        "ArtistSort": "artist_sort",
        "Album": "album",
        "Title": "title",
        "Track": "track",
        "AlbumArtist": "albumartist",
        "AlbumArtistSort": "albumartist_sort",
        "Label": "label",
        "Genre": "genre",
        "Date": "year",
        "OriginalDate": "original_year",
        "Composer": "composer",
        "Disc": "disc",
        "Comment": "comments",
        "MUSICBRAINZ_TRACKID": "mb_trackid",
        "MUSICBRAINZ_ALBUMID": "mb_albumid",
        "MUSICBRAINZ_ARTISTID": "mb_artistid",
        "MUSICBRAINZ_ALBUMARTISTID": "mb_albumartistid",
        "MUSICBRAINZ_RELEASETRACKID": "mb_releasetrackid",
    }

    def cmd_tagtypes(self, conn):
        """Returns a list of the metadata (tag) fields available for
        searching.
        """
        for tag in self.tagtype_map:
            yield f"tagtype: {tag}"

    def _tagtype_lookup(self, tag):
        """Uses `tagtype_map` to look up the beets column name for an
        MPD tagtype (or throw an appropriate exception). Returns both
        the canonical name of the MPD tagtype and the beets column
        name.
        """
        for test_tag, key in self.tagtype_map.items():
            # Match case-insensitively.
            if test_tag.lower() == tag.lower():
                return test_tag, key
        raise BPDError(ERROR_UNKNOWN, "no such tagtype")

    def _metadata_query(self, query_type, kv, allow_any_query: bool = False):
        """Helper function returns a query object that will find items
        according to the library query type provided and the key-value
        pairs specified. The any_query_type is used for queries of
        type "any"; if None, then an error is thrown.
        """
        if kv:  # At least one key-value pair.
            queries: list[Query] = []
            # Iterate pairwise over the arguments.
            it = iter(kv)
            for tag, value in zip(it, it):
                if tag.lower() == "any":
                    if allow_any_query:
                        queries.append(
                            Item.any_writable_media_field_query(
                                query_type, value
                            )
                        )
                    else:
                        raise BPDError(ERROR_UNKNOWN, "no such tagtype")
                else:
                    _, key = self._tagtype_lookup(tag)
                    queries.append(Item.field_query(key, value, query_type))
            return dbcore.query.AndQuery(queries)
        else:  # No key-value pairs.
            return dbcore.query.TrueQuery()

    def cmd_search(self, conn, *kv):
        """Perform a substring match for items."""
        query = self._metadata_query(
            dbcore.query.SubstringQuery, kv, allow_any_query=True
        )
        for item in self.lib.items(query):
            yield self._item_info(item)

    def cmd_find(self, conn, *kv):
        """Perform an exact match for items."""
        query = self._metadata_query(dbcore.query.MatchQuery, kv)
        for item in self.lib.items(query):
            yield self._item_info(item)

    def cmd_list(self, conn, show_tag, *kv):
        """List distinct metadata values for show_tag, possibly
        filtered by matching match_tag to match_term.
        """
        show_tag_canon, show_key = self._tagtype_lookup(show_tag)
        if len(kv) == 1:
            if show_tag_canon == "Album":
                # If no tag was given, assume artist. This is because MPD
                # supports a short version of this command for fetching the
                # albums belonging to a particular artist, and some clients
                # rely on this behaviour (e.g. MPDroid, M.A.L.P.).
                kv = ("Artist", kv[0])
            else:
                raise BPDError(ERROR_ARG, 'should be "Album" for 3 arguments')
        elif len(kv) % 2 != 0:
            raise BPDError(ERROR_ARG, "Incorrect number of filter arguments")
        query = self._metadata_query(dbcore.query.MatchQuery, kv)

        clause, subvals = query.clause()
        statement = (
            f"SELECT DISTINCT {show_key}"
            f" FROM items WHERE {clause}"
            f" ORDER BY {show_key}"
        )
        self._log.debug(statement)
        with self.lib.transaction() as tx:
            rows = tx.query(statement, subvals)

        for row in rows:
            if not row[0]:
                # Skip any empty values of the field.
                continue
            yield f"{show_tag_canon}: {row[0]}"

    def cmd_count(self, conn, tag, value):
        """Returns the number and total time of songs matching the
        tag/value query.
        """
        _, key = self._tagtype_lookup(tag)
        songs = 0
        playtime = 0.0
        for item in self.lib.items(
            Item.field_query(key, value, dbcore.query.MatchQuery)
        ):
            songs += 1
            playtime += item.length
        yield f"songs: {songs}"
        yield f"playtime: {int(playtime)}"

    # Persistent playlist manipulation. In MPD this is an optional feature so
    # these dummy implementations match MPD's behaviour with the feature off.

    def cmd_listplaylist(self, conn, playlist):
        raise BPDError(ERROR_NO_EXIST, "No such playlist")

    def cmd_listplaylistinfo(self, conn, playlist):
        raise BPDError(ERROR_NO_EXIST, "No such playlist")

    def cmd_listplaylists(self, conn):
        raise BPDError(ERROR_UNKNOWN, "Stored playlists are disabled")

    def cmd_load(self, conn, playlist):
        raise BPDError(ERROR_NO_EXIST, "Stored playlists are disabled")

    def cmd_playlistadd(self, conn, playlist, uri):
        raise BPDError(ERROR_UNKNOWN, "Stored playlists are disabled")

    def cmd_playlistclear(self, conn, playlist):
        raise BPDError(ERROR_UNKNOWN, "Stored playlists are disabled")

    def cmd_playlistdelete(self, conn, playlist, index):
        raise BPDError(ERROR_UNKNOWN, "Stored playlists are disabled")

    def cmd_playlistmove(self, conn, playlist, from_index, to_index):
        raise BPDError(ERROR_UNKNOWN, "Stored playlists are disabled")

    def cmd_rename(self, conn, playlist, new_name):
        raise BPDError(ERROR_UNKNOWN, "Stored playlists are disabled")

    def cmd_rm(self, conn, playlist):
        raise BPDError(ERROR_UNKNOWN, "Stored playlists are disabled")

    def cmd_save(self, conn, playlist):
        raise BPDError(ERROR_UNKNOWN, "Stored playlists are disabled")

    # "Outputs." Just a dummy implementation because we don't control
    # any outputs.

    def cmd_outputs(self, conn):
        """List the available outputs."""
        yield (
            "outputid: 0",
            "outputname: gstreamer",
            "outputenabled: 1",
        )

    def cmd_enableoutput(self, conn, output_id):
        output_id = cast_arg(int, output_id)
        if output_id != 0:
            raise ArgumentIndexError()

    def cmd_disableoutput(self, conn, output_id):
        output_id = cast_arg(int, output_id)
        if output_id == 0:
            raise BPDError(ERROR_ARG, "cannot disable this output")
        else:
            raise ArgumentIndexError()

    # Playback control. The functions below hook into the
    # half-implementations provided by the base class. Together, they're
    # enough to implement all normal playback functionality.

    def cmd_play(self, conn, index=-1):
        new_index = index != -1 and index != self.current_index
        was_paused = self.paused
        super().cmd_play(conn, index)

        if self.current_index > -1:  # Not stopped.
            if was_paused and not new_index:
                # Just unpause.
                self.player.play()
            else:
                self.player.play_file(self.playlist[self.current_index].path)

    def cmd_pause(self, conn, state=None):
        super().cmd_pause(conn, state)
        if self.paused:
            self.player.pause()
        elif self.player.playing:
            self.player.play()

    def cmd_stop(self, conn):
        super().cmd_stop(conn)
        self.player.stop()

    def cmd_seek(self, conn, index, pos):
        """Seeks to the specified position in the specified song."""
        index = cast_arg(int, index)
        pos = cast_arg(float, pos)
        super().cmd_seek(conn, index, pos)
        self.player.seek(pos)

    # Volume control.

    def cmd_setvol(self, conn, vol):
        vol = cast_arg(int, vol)
        super().cmd_setvol(conn, vol)
        self.player.volume = float(vol) / 100


# Beets plugin hooks.


class BPDPlugin(BeetsPlugin):
    """Provides the "beet bpd" command for running a music player
    server.
    """

    def __init__(self):
        super().__init__()
        self.config.add(
            {
                "host": "",
                "port": 6600,
                "control_port": 6601,
                "password": "",
                "volume": VOLUME_MAX,
            }
        )
        self.config["password"].redact = True

    def start_bpd(self, lib, host, port, password, volume, ctrl_port):
        """Starts a BPD server."""
        server = Server(lib, host, port, password, ctrl_port, self._log)
        server.cmd_setvol(None, volume)
        server.run()

    def commands(self):
        cmd = beets.ui.Subcommand(
            "bpd", help="run an MPD-compatible music player server"
        )

        def func(lib, opts, args):
            host = self.config["host"].as_str()
            host = args.pop(0) if args else host
            port = args.pop(0) if args else self.config["port"].get(int)
            if args:
                ctrl_port = args.pop(0)
            else:
                ctrl_port = self.config["control_port"].get(int)
            if args:
                raise beets.ui.UserError("too many arguments")
            password = self.config["password"].as_str()
            volume = self.config["volume"].get(int)
            self.start_bpd(
                lib, host, int(port), password, volume, int(ctrl_port)
            )

        cmd.func = func
        return [cmd]