From 414b6821230c39ddd6e070a88764ec1d13129e31 Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Mon, 22 Feb 2021 11:19:23 +0100 Subject: [PATCH 1/8] Add strip_path to mpdstats --- beetsplug/mpdstats.py | 10 +++++++++- docs/plugins/mpdstats.rst | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index 599aa7631..3daa08fe2 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -53,6 +53,9 @@ class MPDClientWrapper(object): self.music_directory = ( mpd_config['music_directory'].as_str()) + self.strip_path = ( + mpd_config['strip_path'].as_str()) + if sys.version_info < (3, 0): # On Python 2, use_unicode will enable the utf-8 mode for # python-mpd2 @@ -118,12 +121,16 @@ class MPDClientWrapper(object): """Return the path to the currently playing song, along with its songid. Prefixes paths with the music_directory, to get the absolute path. + In some cases, we need to remove the local path from MPD server, + we replace 'strip_path' with ''. + `strip_path` defaults to ''. """ result = None entry = self.get('currentsong') if 'file' in entry: if not is_url(entry['file']): - result = os.path.join(self.music_directory, entry['file']) + file = entry['file'].replace(self.strip_path, '') + result = os.path.join(self.music_directory, file) else: result = entry['file'] return result, entry.get('id') @@ -334,6 +341,7 @@ class MPDStatsPlugin(plugins.BeetsPlugin): super(MPDStatsPlugin, self).__init__() mpd_config.add({ 'music_directory': config['directory'].as_filename(), + 'strip_path': u'', 'rating': True, 'rating_mix': 0.75, 'host': os.environ.get('MPD_HOST', u'localhost'), diff --git a/docs/plugins/mpdstats.rst b/docs/plugins/mpdstats.rst index de9b2ca59..33b3aa43a 100644 --- a/docs/plugins/mpdstats.rst +++ b/docs/plugins/mpdstats.rst @@ -53,6 +53,9 @@ configuration file. The available options are: - **music_directory**: If your MPD library is at a different location from the beets library (e.g., because one is mounted on a NFS share), specify the path here. +- **strip_path**: If your MPD library contains local path, specify the part to remove + here. Combining this with **music_directory** you can mangle MPD path to match the + **beets library** one. Default: The beets library directory. - **rating**: Enable rating updates. Default: ``yes``. From af2229c1b7a149444fa8276cf069f4eaefc15ffd Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Mon, 22 Feb 2021 14:28:14 +0100 Subject: [PATCH 2/8] Fix docs and changelog --- docs/changelog.rst | 2 ++ docs/plugins/mpdstats.rst | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index e2a62e6d8..cabebdd5b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,8 @@ Changelog New features: +* :doc:`/plugins/mpdstats`: Add strip_path option to help build the right local path + from MPD information * Submitting acoustID information on tracks which already have a fingerprint :bug:`3834` * conversion uses par_map to parallelize conversion jobs in python3 diff --git a/docs/plugins/mpdstats.rst b/docs/plugins/mpdstats.rst index 33b3aa43a..c5adbc64b 100644 --- a/docs/plugins/mpdstats.rst +++ b/docs/plugins/mpdstats.rst @@ -55,7 +55,7 @@ configuration file. The available options are: here. - **strip_path**: If your MPD library contains local path, specify the part to remove here. Combining this with **music_directory** you can mangle MPD path to match the - **beets library** one. + beets library one. Default: The beets library directory. - **rating**: Enable rating updates. Default: ``yes``. From ce974d4fc7c3974c0c7c60a02b26f0586bf2a184 Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Mon, 22 Feb 2021 14:30:50 +0100 Subject: [PATCH 3/8] One line :) --- beetsplug/mpdstats.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index 3daa08fe2..8c32d5867 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -50,11 +50,9 @@ class MPDClientWrapper(object): def __init__(self, log): self._log = log - self.music_directory = ( - mpd_config['music_directory'].as_str()) + self.music_directory = (mpd_config['music_directory'].as_str()) - self.strip_path = ( - mpd_config['strip_path'].as_str()) + self.strip_path = (mpd_config['strip_path'].as_str()) if sys.version_info < (3, 0): # On Python 2, use_unicode will enable the utf-8 mode for From 99de85b5c295d673dda1e43bbd7220b8aa4cbddc Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Mon, 22 Feb 2021 14:33:01 +0100 Subject: [PATCH 4/8] Only leftmost subpath is replaced --- beetsplug/mpdstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index 8c32d5867..37093a97a 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -127,7 +127,7 @@ class MPDClientWrapper(object): entry = self.get('currentsong') if 'file' in entry: if not is_url(entry['file']): - file = entry['file'].replace(self.strip_path, '') + file = entry['file'].replace(self.strip_path, '', count=1) result = os.path.join(self.music_directory, file) else: result = entry['file'] From 921bc424cd3774d6dea3e74e0f0f77b009f3bcaa Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Mon, 22 Feb 2021 14:47:58 +0100 Subject: [PATCH 5/8] Better handling of strip_path --- beetsplug/mpdstats.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index 37093a97a..be543229d 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -127,8 +127,9 @@ class MPDClientWrapper(object): entry = self.get('currentsong') if 'file' in entry: if not is_url(entry['file']): - file = entry['file'].replace(self.strip_path, '', count=1) - result = os.path.join(self.music_directory, file) + if entry.startswith(self.strip_path): + entry = entry[len(self.strip_path):] + result = os.path.join(self.music_directory, entry) else: result = entry['file'] return result, entry.get('id') From 0e67b6801c2d4fa1071df7661b62cadb060ac5c4 Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Mon, 22 Feb 2021 14:50:30 +0100 Subject: [PATCH 6/8] ARGH! Wrong variable --- beetsplug/mpdstats.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index be543229d..8047a5c94 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -127,9 +127,10 @@ class MPDClientWrapper(object): entry = self.get('currentsong') if 'file' in entry: if not is_url(entry['file']): - if entry.startswith(self.strip_path): - entry = entry[len(self.strip_path):] - result = os.path.join(self.music_directory, entry) + file = entry['file'] + if file.startswith(self.strip_path): + file = file[len(self.strip_path):] + result = os.path.join(self.music_directory, file) else: result = entry['file'] return result, entry.get('id') From 48e7d2964e2fd48985ad1a4eb6c00d025ba0d723 Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Tue, 23 Feb 2021 05:45:53 +0100 Subject: [PATCH 7/8] Style changes --- beetsplug/mpdstats.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index 8047a5c94..b4e04a1f4 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -50,9 +50,8 @@ class MPDClientWrapper(object): def __init__(self, log): self._log = log - self.music_directory = (mpd_config['music_directory'].as_str()) - - self.strip_path = (mpd_config['strip_path'].as_str()) + self.music_directory = mpd_config['music_directory'].as_str() + self.strip_path = mpd_config['strip_path'].as_str() if sys.version_info < (3, 0): # On Python 2, use_unicode will enable the utf-8 mode for From 1a65501cee664f32199467ffa950760d4082c141 Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Tue, 23 Feb 2021 10:40:59 +0100 Subject: [PATCH 8/8] Clean up strip_path and logging --- beetsplug/mpdstats.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index b4e04a1f4..5c8402329 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -53,6 +53,13 @@ class MPDClientWrapper(object): self.music_directory = mpd_config['music_directory'].as_str() self.strip_path = mpd_config['strip_path'].as_str() + # Ensure strip_path end with '/' + if not self.strip_path.endswith('/'): + self.strip_path += '/' + + self._log.debug('music_directory: {0}', self.music_directory) + self._log.debug('strip_path: {0}', self.strip_path) + if sys.version_info < (3, 0): # On Python 2, use_unicode will enable the utf-8 mode for # python-mpd2 @@ -132,6 +139,7 @@ class MPDClientWrapper(object): result = os.path.join(self.music_directory, file) else: result = entry['file'] + self._log.debug('returning: {0}', result) return result, entry.get('id') def status(self):