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
7 changes: 7 additions & 0 deletions beets/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ def sorted_walk(path, ignore=(), ignore_hidden=False, logger=None):
yield res


def path_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
parent).
Expand Down
10 changes: 8 additions & 2 deletions beetsplug/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import fnmatch
import tempfile
import beets
from beets.util import path_as_posix


class PlaylistQuery(beets.dbcore.Query):
Expand Down Expand Up @@ -86,6 +87,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 +162,8 @@ def update_playlist(self, filename, base_dir):
try:
new_path = self.changes[beets.util.normpath(lookup)]
except KeyError:
if self.config['forward_slash']:
line = path_as_posix(line)
tempfp.write(line)
else:
if new_path is None:
Expand All @@ -170,8 +174,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']:
line = path_as_posix(line)
tempfp.write(line)

if changes or deletions:
self._log.info(
Expand Down
7 changes: 5 additions & 2 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, path_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,8 @@ def update_playlists(self, lib):
mkdirall(m3u_path)
with open(syspath(m3u_path), 'wb') as f:
for path in m3us[m3u]:
if self.config['forward_slash'].get():
path = path_as_posix(path)
f.write(path + b'\n')

self._log.info(u"{0} playlists updated", len(self._matched_playlists))
5 changes: 5 additions & 0 deletions docs/plugins/playlist.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Then configure your playlists like this::
auto: no
relative_to: ~/Music
playlist_dir: ~/.mpd/playlists
forward_slash: no

It is possible to query the library based on a playlist by speicifying its
absolute path::
Expand Down Expand Up @@ -45,3 +46,7 @@ other configuration options are:
set it to ``playlist`` to use the playlist's parent directory or to
``library`` to use the library directory.
Default: ``library``
- **forward_slah**: Forces forward slashes in the generated playlist files.
If you intend to use this plugin to generate playlists for MPD on
Windows, set this to yes.
Default: Use system separator.
5 changes: 5 additions & 0 deletions docs/plugins/smartplaylist.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Then configure your smart playlists like the following example::
smartplaylist:
relative_to: ~/Music
playlist_dir: ~/.mpd/playlists
forward_slah: no
playlists:
- name: all.m3u
query: ''
Expand Down Expand Up @@ -96,3 +97,7 @@ other configuration options are:
directory. If you intend to use this plugin to generate playlists for MPD,
point this to your MPD music directory.
Default: Use absolute paths.
- **forward_slah**: Forces forward slashes in the generated playlist files.
If you intend to use this plugin to generate playlists for MPD on
Windows, set this to yes.
Default: Use system separator.
5 changes: 5 additions & 0 deletions test/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def test_components_works_on_relative(self):
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 = br'C:\a\b\c'
a = br'C:/a/b/c'
self.assertEqual(util.path_as_posix(p), a)


class AlbumFileTest(_common.TestCase):
def setUp(self):
Expand Down