Skip to content

Commit 053d78f

Browse files
authored
Fix 6784 - playlist plugin support for current beets libraries (relative paths) (#6821)
## Description Fixes #6784 . <!-- Insert issue number here if applicable. --> I (and others) encountered an issue where playlist queries returned empty results. At first, I thought it was because I had playlist in extended m3u format. I found that the line skipping mechanism did not work correctly because the playlist file is read as bytestrings and the comparator used regular strings. Fixing that did not help (the previously unskipped lines were simply not found but did not break the search) and I then found #6784. I think the issue there is that different path normalizations are used in `beets ls` and `playlist.py`. ## To Do - [x] Documentation. (If you've added a new command-line flag, for example, find the appropriate page under `docs/` to describe it.) - [x] Changelog. (Add an entry to `docs/changelog.rst` to the bottom of one of the lists near the top of the document.) - [x] Tests. (Very much encouraged but not strictly required.)
2 parents dd681dd + a9541ba commit 053d78f

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎beetsplug/playlist.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import confuse
99

1010
import beets
11+
from beets.dbcore.pathutils import normalize_path_for_db
1112
from beets.dbcore.query import InQuery
1213
from beets.plugins import BeetsPlugin
1314
from beets.util import path_as_posix
1415

1516
if TYPE_CHECKING:
16-
from collections.abc import Iterator
17+
from collections.abc import Iterator, Sequence
1718

1819
from beets.dbcore.query import FieldQueryType
1920
from beets.library import Item, Library
@@ -26,6 +27,10 @@ def is_m3u_file(path: str) -> bool:
2627
class PlaylistQuery(InQuery[bytes]):
2728
"""Matches files listed by a playlist file."""
2829

30+
@property
31+
def subvals(self) -> Sequence[bytes]:
32+
return [normalize_path_for_db(p) for p in self.pattern]
33+
2934
def __init__(self, _, pattern: str, __) -> None:
3035
config = beets.config["playlist"]
3136

@@ -56,8 +61,9 @@ def __init__(self, _, pattern: str, __) -> None:
5661
relative_to_bytes = beets.util.bytestring_path(relative_to)
5762

5863
for line in f:
59-
if line[0] == "#":
60-
# ignore comments, and extm3u extension
64+
line = line.rstrip()
65+
if not line or line.startswith(b"#"):
66+
# ignore blank lines, comments, and extm3u extension
6167
continue
6268

6369
paths.append(

‎docs/changelog.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ Bug fixes
194194
- The initial beets setup no longer writes a database backup for every
195195
migration. Backups are now skipped when the table being migrated has no rows.
196196
:bug:`7170`
197+
- :doc:`plugins/playlist`: Match playlist entries that point inside the active
198+
library directory when paths are stored library-relative. :bug:`6784`
199+
- :doc:`plugins/playlist`: Ignore blank lines in M3U playlists.
197200

198201
For plugin developers
199202
~~~~~~~~~~~~~~~~~~~~~

‎test/plugins/test_playlist.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
from pathlib import Path
33
from shlex import quote
44

5+
import platformdirs
6+
57
import beets
68
from beets.test.helper import PluginTestCase
9+
from beetsplug.playlist import PlaylistQuery
710

811

912
class PlaylistTestCase(PluginTestCase):
@@ -16,10 +19,13 @@ class PlaylistTestCase(PluginTestCase):
1619
z_track_path = Path("x") / "y" / "z.mp3"
1720
nonexisting_track_path = Path("nonexisting.mp3")
1821

22+
def get_music_dir(self):
23+
return platformdirs.user_music_dir()
24+
1925
def setUp(self):
2026
super().setUp()
2127

22-
self.music_dir = (Path("~") / "Music").expanduser()
28+
self.music_dir = self.get_music_dir()
2329

2430
for p, title, album in [
2531
(self.c_track_path, "some item", "some album"),
@@ -61,6 +67,20 @@ def setup_test(self):
6167

6268

6369
class PlaylistQueryTest:
70+
def test_ignores_blank_lines_and_comments_in_playlist(self):
71+
track_paths = [
72+
os.fsencode(self.music_dir / self.c_track_path),
73+
os.fsencode(self.music_dir / self.f_track_path),
74+
]
75+
76+
self.absolute_playlist_path.write_bytes(
77+
b"\n#EXTM3U\n# comment\n" + b"\n".join(track_paths) + b"\n"
78+
)
79+
80+
query = PlaylistQuery(None, "absolute", None)
81+
82+
assert query.pattern == track_paths
83+
6484
def test_name_query_with_absolute_paths_in_playlist(self):
6585
q = "playlist:absolute"
6686
results = self.lib.items(q)
@@ -97,6 +117,11 @@ def setup_test(self):
97117
self.config["playlist"]["relative_to"] = "library"
98118

99119

120+
class PlaylistTestRelativeToActiveLib(PlaylistTestRelativeToLib):
121+
def get_music_dir(self):
122+
return self.lib_path
123+
124+
100125
class PlaylistTestRelativeToDir(PlaylistQueryTest, PlaylistTestCase):
101126
def setup_test(self):
102127
self.config["playlist"]["relative_to"] = str(self.music_dir)

0 commit comments

Comments
 (0)