From d1ba309f36774fe71213c663a35dce49da8eae64 Mon Sep 17 00:00:00 2001 From: MartyLake Date: Mon, 22 Jul 2019 16:55:46 +0200 Subject: [PATCH] Add a new method that copied pathlib.path.as_posix --- beets/util/__init__.py | 4 ++++ beetsplug/playlist.py | 11 +++++++++-- beetsplug/smartplaylist.py | 10 +++++++--- test/test_files.py | 4 ++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 29b2a73e7..9c87e7994 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -222,6 +222,10 @@ def sorted_walk(path, ignore=(), ignore_hidden=False, logger=None): for res in sorted_walk(cur, ignore, ignore_hidden, logger): yield res +def pathlib_as_posix(path): + """Return the string representation of the path with forward (/) + slashes.""" + return path.replace(b'\\', b'/') def mkdirall(path): """Make all the enclosing directories of path (like mkdir -p on the diff --git a/beetsplug/playlist.py b/beetsplug/playlist.py index 4ab02c6b7..af98a250c 100644 --- a/beetsplug/playlist.py +++ b/beetsplug/playlist.py @@ -18,6 +18,8 @@ import os import fnmatch import tempfile import beets +from beets.util import (pathlib_as_posix) + class PlaylistQuery(beets.dbcore.Query): @@ -86,6 +88,7 @@ class PlaylistPlugin(beets.plugins.BeetsPlugin): 'auto': False, 'playlist_dir': '.', 'relative_to': 'library', + 'forward_slash': False, }) self.playlist_dir = self.config['playlist_dir'].as_filename() @@ -160,6 +163,8 @@ class PlaylistPlugin(beets.plugins.BeetsPlugin): try: new_path = self.changes[beets.util.normpath(lookup)] except KeyError: + if self.config['forward_slash'].get(): + line = pathlib_as_posix(line) tempfp.write(line) else: if new_path is None: @@ -170,8 +175,10 @@ class PlaylistPlugin(beets.plugins.BeetsPlugin): changes += 1 if is_relative: new_path = os.path.relpath(new_path, base_dir) - - tempfp.write(line.replace(original_path, new_path)) + line = line.replace(original_path, new_path) + if self.config['forward_slash'].get(): + line = pathlib_as_posix(line) + tempfp.write(line) if changes or deletions: self._log.info( diff --git a/beetsplug/smartplaylist.py b/beetsplug/smartplaylist.py index a83fc4d19..a440cb7fd 100644 --- a/beetsplug/smartplaylist.py +++ b/beetsplug/smartplaylist.py @@ -21,7 +21,7 @@ from __future__ import division, absolute_import, print_function from beets.plugins import BeetsPlugin from beets import ui from beets.util import (mkdirall, normpath, sanitize_path, syspath, - bytestring_path) + bytestring_path, pathlib_as_posix) from beets.library import Item, Album, parse_query_string from beets.dbcore import OrQuery from beets.dbcore.query import MultipleSort, ParsingError @@ -37,7 +37,8 @@ class SmartPlaylistPlugin(BeetsPlugin): 'relative_to': None, 'playlist_dir': u'.', 'auto': True, - 'playlists': [] + 'playlists': [], + 'forward_slash': False, }) self._matched_playlists = None @@ -206,6 +207,9 @@ class SmartPlaylistPlugin(BeetsPlugin): mkdirall(m3u_path) with open(syspath(m3u_path), 'wb') as f: for path in m3us[m3u]: - f.write(path + b'\n') + if self.config['forward_slash'].get(): + path = pathlib_as_posix(path) + f.write(path) + f.write(b'\n') self._log.info(u"{0} playlists updated", len(self._matched_playlists)) diff --git a/test/test_files.py b/test/test_files.py index ff055ac6f..87e4862f3 100644 --- a/test/test_files.py +++ b/test/test_files.py @@ -194,6 +194,10 @@ class HelperTest(_common.TestCase): p = 'a/b/c' a = ['a', 'b', 'c'] self.assertEqual(util.components(p), a) + def test_forward_slash(self): + p = r'C:\a\b\c' + a = r'C:/a/b/c' + self.assertEqual(util.pathlib_as_posix(p), a) class AlbumFileTest(_common.TestCase):