Skip to content
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

Add option to force forward slashes in paths #3334

Merged
merged 13 commits into from
Jul 25, 2019
4 changes: 4 additions & 0 deletions beets/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
MartyLake marked this conversation as resolved.
Show resolved Hide resolved
"""Return the string representation of the path with forward (/)
slashes."""
MartyLake marked this conversation as resolved.
Show resolved Hide resolved
return path.replace(b'\\', b'/')

def mkdirall(path):
"""Make all the enclosing directories of path (like mkdir -p on the
Expand Down
11 changes: 9 additions & 2 deletions beetsplug/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import fnmatch
import tempfile
import beets
from beets.util import (pathlib_as_posix)
MartyLake marked this conversation as resolved.
Show resolved Hide resolved

MartyLake marked this conversation as resolved.
Show resolved Hide resolved


class PlaylistQuery(beets.dbcore.Query):
Expand Down Expand Up @@ -86,6 +88,7 @@ def __init__(self):
'auto': False,
'playlist_dir': '.',
'relative_to': 'library',
'forward_slash': False,
})

self.playlist_dir = self.config['playlist_dir'].as_filename()
Expand Down Expand Up @@ -160,6 +163,8 @@ def update_playlist(self, filename, base_dir):
try:
new_path = self.changes[beets.util.normpath(lookup)]
except KeyError:
if self.config['forward_slash'].get():
MartyLake marked this conversation as resolved.
Show resolved Hide resolved
line = pathlib_as_posix(line)
tempfp.write(line)
else:
if new_path is None:
Expand All @@ -170,8 +175,10 @@ def update_playlist(self, filename, base_dir):
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():
MartyLake marked this conversation as resolved.
Show resolved Hide resolved
line = pathlib_as_posix(line)
tempfp.write(line)

if changes or deletions:
self._log.info(
Expand Down
10 changes: 7 additions & 3 deletions beetsplug/smartplaylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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
Expand All @@ -37,7 +37,8 @@ def __init__(self):
'relative_to': None,
'playlist_dir': u'.',
'auto': True,
'playlists': []
'playlists': [],
'forward_slash': False,
})

self._matched_playlists = None
Expand Down Expand Up @@ -206,6 +207,9 @@ def update_playlists(self, lib):
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')
MartyLake marked this conversation as resolved.
Show resolved Hide resolved

self._log.info(u"{0} playlists updated", len(self._matched_playlists))
4 changes: 4 additions & 0 deletions test/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ def test_components_works_on_relative(self):
p = 'a/b/c'
a = ['a', 'b', 'c']
self.assertEqual(util.components(p), a)
def test_forward_slash(self):
MartyLake marked this conversation as resolved.
Show resolved Hide resolved
p = r'C:\a\b\c'
a = r'C:/a/b/c'
self.assertEqual(util.pathlib_as_posix(p), a)


class AlbumFileTest(_common.TestCase):
Expand Down