Add a new method that copied pathlib.path.as_posix

This commit is contained in:
MartyLake 2019-07-22 16:55:46 +02:00
parent bd6a5cfd8e
commit d1ba309f36
4 changed files with 24 additions and 5 deletions

View file

@ -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

View file

@ -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(

View file

@ -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))

View file

@ -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):