-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Playlist plugin #3145
Merged
Merged
Playlist plugin #3145
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a80a07f
playlist: Add playlist plugin
Holzhaus 19b92e1
playlist: Improve speed in PlaylistQuery class
Holzhaus cc501be
docs: Add documentation for the playlist plugin
Holzhaus d78bade
docs: Add playlist plugin to the changelog
Holzhaus 0988a2a
test: Add test suite for the playlist plugin
Holzhaus f9f2fa0
playlist: Restructure playlist reading code and add error handling
Holzhaus d52dcdd
test: Add playlist testcases for nonexisting playlists
Holzhaus 34cdeee
docs: Reword documentation of playlist plugin's relative_to option
Holzhaus d4039be
test: Get rid of TemporaryDirectory to restore Python 2.7 compatibility
Holzhaus 32b6df0
test: Don't use unix-only paths in playlist plugin testcase
Holzhaus 055f2d3
playlist: Also catch IOErrors to restore Python 2.7 compatiblity
Holzhaus 31c687c
test: Fix playlist plugin path handling for Windows compatibility
Holzhaus d6022e2
test: Ensure path quoting in playlist tests
Holzhaus 4f1a468
playlist: Restore case sensitivity in col_clause method
Holzhaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# -*- coding: utf-8 -*- | ||
# This file is part of beets. | ||
# | ||
# 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. | ||
|
||
import os | ||
import fnmatch | ||
import beets | ||
|
||
|
||
class PlaylistQuery(beets.dbcore.FieldQuery): | ||
"""Matches files listed by a playlist file. | ||
""" | ||
def __init__(self, field, pattern, fast=True): | ||
super(PlaylistQuery, self).__init__(field, pattern, fast) | ||
config = beets.config['playlist'] | ||
|
||
# Get the full path to the playlist | ||
playlist_paths = ( | ||
pattern, | ||
os.path.abspath(os.path.join( | ||
config['playlist_dir'].as_filename(), | ||
'{0}.m3u'.format(pattern), | ||
)), | ||
) | ||
|
||
self.paths = [] | ||
for playlist_path in playlist_paths: | ||
if not fnmatch.fnmatch(playlist_path, '*.[mM]3[uU]'): | ||
# This is not am M3U playlist, skip this candidate | ||
continue | ||
|
||
try: | ||
f = open(beets.util.syspath(playlist_path), mode='rb') | ||
except (OSError, IOError): | ||
continue | ||
|
||
if config['relative_to'].get() == 'library': | ||
relative_to = beets.config['directory'].as_filename() | ||
elif config['relative_to'].get() == 'playlist': | ||
relative_to = os.path.dirname(playlist_path) | ||
else: | ||
relative_to = config['relative_to'].as_filename() | ||
relative_to = beets.util.bytestring_path(relative_to) | ||
|
||
for line in f: | ||
if line[0] == '#': | ||
# ignore comments, and extm3u extension | ||
continue | ||
|
||
self.paths.append(beets.util.normpath( | ||
os.path.join(relative_to, line.rstrip()) | ||
)) | ||
f.close() | ||
break | ||
|
||
def col_clause(self): | ||
if not self.paths: | ||
# Playlist is empty | ||
return '0', () | ||
clause = 'path IN ({0})'.format(', '.join('?' for path in self.paths)) | ||
return clause, (beets.library.BLOB_TYPE(p) for p in self.paths) | ||
|
||
def match(self, item): | ||
return item.path in self.paths | ||
|
||
|
||
class PlaylistType(beets.dbcore.types.String): | ||
"""Custom type for playlist query. | ||
""" | ||
query = PlaylistQuery | ||
|
||
|
||
class PlaylistPlugin(beets.plugins.BeetsPlugin): | ||
item_types = {'playlist': PlaylistType()} | ||
|
||
def __init__(self): | ||
super(PlaylistPlugin, self).__init__() | ||
self.config.add({ | ||
'playlist_dir': '.', | ||
'relative_to': 'library', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
Smart Playlist Plugin | ||
===================== | ||
|
||
``playlist`` is a plugin to use playlists in m3u format. | ||
|
||
To use it, enable the ``playlist`` plugin in your configuration | ||
(see :ref:`using-plugins`). | ||
Then configure your playlists like this:: | ||
|
||
playlist: | ||
relative_to: ~/Music | ||
playlist_dir: ~/.mpd/playlists | ||
|
||
It is possible to query the library based on a playlist by speicifying its | ||
absolute path:: | ||
|
||
$ beet ls playlist:/path/to/someplaylist.m3u | ||
|
||
The plugin also supports referencing playlists by name. The playlist is then | ||
seached in the playlist_dir and the ".m3u" extension is appended to the | ||
name:: | ||
|
||
$ beet ls playlist:anotherplaylist | ||
|
||
Configuration | ||
------------- | ||
|
||
To configure the plugin, make a ``smartplaylist:`` section in your | ||
configuration file. In addition to the ``playlists`` described above, the | ||
other configuration options are: | ||
|
||
- **playlist_dir**: Where to read playlist files from. | ||
Default: The current working directory (i.e., ``'.'``). | ||
- **relative_to**: Interpret paths in the playlist files relative to a base | ||
directory. Instead of setting it to a fixed path, it is also possible to | ||
set it to ``playlist`` to use the playlist's parent directory or to | ||
``library`` to use the library directory. | ||
Default: ``library`` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# -*- coding: utf-8 -*- | ||
# This file is part of beets. | ||
# Copyright 2016, Thomas Scholtes. | ||
# | ||
# 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. | ||
|
||
from __future__ import division, absolute_import, print_function | ||
from six.moves import shlex_quote | ||
|
||
import os | ||
import shutil | ||
import tempfile | ||
import unittest | ||
|
||
from test import _common | ||
from test import helper | ||
|
||
import beets | ||
|
||
|
||
class PlaylistTest(unittest.TestCase, helper.TestHelper): | ||
def setUp(self): | ||
self.setup_beets() | ||
self.lib = beets.library.Library(':memory:') | ||
|
||
self.music_dir = os.path.expanduser('~/Music') | ||
|
||
i1 = _common.item() | ||
i1.path = beets.util.normpath(os.path.join( | ||
self.music_dir, | ||
'a/b/c.mp3', | ||
)) | ||
i1.title = u'some item' | ||
i1.album = u'some album' | ||
self.lib.add(i1) | ||
self.lib.add_album([i1]) | ||
|
||
i2 = _common.item() | ||
i2.path = beets.util.normpath(os.path.join( | ||
self.music_dir, | ||
'd/e/f.mp3', | ||
)) | ||
i2.title = 'another item' | ||
i2.album = 'another album' | ||
self.lib.add(i2) | ||
self.lib.add_album([i2]) | ||
|
||
i3 = _common.item() | ||
i3.path = beets.util.normpath(os.path.join( | ||
self.music_dir, | ||
'x/y/z.mp3', | ||
)) | ||
i3.title = 'yet another item' | ||
i3.album = 'yet another album' | ||
self.lib.add(i3) | ||
self.lib.add_album([i3]) | ||
|
||
self.playlist_dir = tempfile.mkdtemp() | ||
with open(os.path.join(self.playlist_dir, 'test.m3u'), 'w') as f: | ||
f.write('{0}\n'.format(beets.util.displayable_path(i1.path))) | ||
f.write('{0}\n'.format(beets.util.displayable_path(i2.path))) | ||
|
||
self.config['directory'] = self.music_dir | ||
self.config['playlist']['relative_to'] = 'library' | ||
self.config['playlist']['playlist_dir'] = self.playlist_dir | ||
self.load_plugins('playlist') | ||
|
||
def tearDown(self): | ||
self.unload_plugins() | ||
shutil.rmtree(self.playlist_dir) | ||
self.teardown_beets() | ||
|
||
def test_query_name(self): | ||
q = u'playlist:test' | ||
results = self.lib.items(q) | ||
self.assertEqual(set([i.title for i in results]), set([ | ||
u'some item', | ||
u'another item', | ||
])) | ||
|
||
def test_query_path(self): | ||
q = u'playlist:{0}'.format(shlex_quote(os.path.join( | ||
self.playlist_dir, | ||
'test.m3u', | ||
))) | ||
results = self.lib.items(q) | ||
self.assertEqual(set([i.title for i in results]), set([ | ||
u'some item', | ||
u'another item', | ||
])) | ||
|
||
def test_query_name_nonexisting(self): | ||
q = u'playlist:nonexisting'.format(self.playlist_dir) | ||
results = self.lib.items(q) | ||
self.assertEqual(set(results), set()) | ||
|
||
def test_query_path_nonexisting(self): | ||
q = u'playlist:{0}'.format(shlex_quote(os.path.join( | ||
self.playlist_dir, | ||
self.playlist_dir, | ||
'nonexisting.m3u', | ||
))) | ||
results = self.lib.items(q) | ||
self.assertEqual(set(results), set()) | ||
|
||
|
||
def suite(): | ||
return unittest.TestLoader().loadTestsFromName(__name__) | ||
|
||
if __name__ == '__main__': | ||
unittest.main(defaultTest='suite') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably a good idea to mention explicitly that there are three options here:
playlist
,library
, or a path.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.