From 13c1056a9c94a3800de736b052a86600d1e8f4b4 Mon Sep 17 00:00:00 2001 From: Robin McCorkell Date: Tue, 10 Jan 2017 14:42:15 +0000 Subject: [PATCH 1/3] Add PlaylistQuery plugin --- beetsplug/playlistquery.py | 70 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 beetsplug/playlistquery.py diff --git a/beetsplug/playlistquery.py b/beetsplug/playlistquery.py new file mode 100644 index 0000000000..b7d0dccc3f --- /dev/null +++ b/beetsplug/playlistquery.py @@ -0,0 +1,70 @@ +# -*- 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 +from beets.plugins import BeetsPlugin +from beets.dbcore import FieldQuery, types +from beets.util.confit import NotFoundError + +class PlaylistQuery(FieldQuery): + """Matches files listed by a playlist file. + """ + relative_path = None + playlist_dir = None + + def __init__(self, field, pattern, fast=True): + super(PlaylistQuery, self).__init__(field, pattern, fast) + + playlist_file = pattern + '.m3u' + playlist_path = os.path.join(self.playlist_dir, playlist_file) + + self.paths = [] + with open(playlist_path, 'r') as f: + for line in f: + if line[0] == '#': + # ignore comments, and extm3u extension + continue + self.paths.append(os.path.normpath( + os.path.join(self.relative_path, line.decode('utf-8').rstrip()) + )) + + def match(self, item): + return item.path.decode('utf-8') in self.paths + + +class PlaylistType(types.String): + """Custom type for playlist query. + """ + query = PlaylistQuery + + +class PlaylistQueryPlugin(BeetsPlugin): + item_types = {'playlist': PlaylistType()} + + def __init__(self): + super(PlaylistQueryPlugin, self).__init__() + self.register_listener('library_opened', self.library_opened) + + PlaylistQuery.playlist_dir = self.config['playlist_dir'].as_filename() + + def library_opened(self, lib): + try: + relative_to = self.config['relative_to'].as_choice(['base', 'playlist']) + except NotFoundError: + relative_to = 'base' + + if relative_to == 'playlist': + PlaylistQuery.relative_path = PlaylistQuery.playlist_dir + else: + PlaylistQuery.relative_path = lib.directory From 790e5316e7fce44213e61a3d1a2890ad8d1f3f1d Mon Sep 17 00:00:00 2001 From: Robin McCorkell Date: Sun, 15 Jan 2017 15:46:59 +0000 Subject: [PATCH 2/3] playlistquery: Encode all paths to bytes, not str --- beetsplug/playlistquery.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/beetsplug/playlistquery.py b/beetsplug/playlistquery.py index b7d0dccc3f..5190ef9427 100644 --- a/beetsplug/playlistquery.py +++ b/beetsplug/playlistquery.py @@ -17,6 +17,7 @@ from beets.dbcore import FieldQuery, types from beets.util.confit import NotFoundError + class PlaylistQuery(FieldQuery): """Matches files listed by a playlist file. """ @@ -26,21 +27,21 @@ class PlaylistQuery(FieldQuery): def __init__(self, field, pattern, fast=True): super(PlaylistQuery, self).__init__(field, pattern, fast) - playlist_file = pattern + '.m3u' + playlist_file = (pattern + '.m3u').encode() playlist_path = os.path.join(self.playlist_dir, playlist_file) self.paths = [] - with open(playlist_path, 'r') as f: + with open(playlist_path, 'rb') as f: for line in f: if line[0] == '#': # ignore comments, and extm3u extension continue self.paths.append(os.path.normpath( - os.path.join(self.relative_path, line.decode('utf-8').rstrip()) + os.path.join(self.relative_path, line.rstrip()) )) def match(self, item): - return item.path.decode('utf-8') in self.paths + return item.path in self.paths class PlaylistType(types.String): @@ -56,7 +57,9 @@ def __init__(self): super(PlaylistQueryPlugin, self).__init__() self.register_listener('library_opened', self.library_opened) - PlaylistQuery.playlist_dir = self.config['playlist_dir'].as_filename() + PlaylistQuery.playlist_dir = ( + self.config['playlist_dir'].as_filename().encode() + ) def library_opened(self, lib): try: From ece3dfa5a754b0a8cde4b56ff78b0ec13c651e30 Mon Sep 17 00:00:00 2001 From: Robin McCorkell Date: Sun, 15 Jan 2017 16:04:34 +0000 Subject: [PATCH 3/3] playlistquery: Set default config options --- beetsplug/playlistquery.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/beetsplug/playlistquery.py b/beetsplug/playlistquery.py index 5190ef9427..60bd3b8015 100644 --- a/beetsplug/playlistquery.py +++ b/beetsplug/playlistquery.py @@ -24,7 +24,7 @@ class PlaylistQuery(FieldQuery): relative_path = None playlist_dir = None - def __init__(self, field, pattern, fast=True): + def __init__(self, field, pattern, fast=False): super(PlaylistQuery, self).__init__(field, pattern, fast) playlist_file = (pattern + '.m3u').encode() @@ -55,6 +55,9 @@ class PlaylistQueryPlugin(BeetsPlugin): def __init__(self): super(PlaylistQueryPlugin, self).__init__() + self.config.add({ + u'relative_to': 'base' + }) self.register_listener('library_opened', self.library_opened) PlaylistQuery.playlist_dir = ( @@ -62,10 +65,7 @@ def __init__(self): ) def library_opened(self, lib): - try: - relative_to = self.config['relative_to'].as_choice(['base', 'playlist']) - except NotFoundError: - relative_to = 'base' + relative_to = self.config['relative_to'].as_choice(['base', 'playlist']) if relative_to == 'playlist': PlaylistQuery.relative_path = PlaylistQuery.playlist_dir