Skip to content

Commit

Permalink
translator: Make path_to_local_track_uri() handle absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Jan 2, 2020
1 parent 4cf798f commit 7a9700f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
5 changes: 3 additions & 2 deletions mopidy_local/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ def _scan_metadata(
f"Track shorter than {MIN_DURATION_MS}ms"
)
else:
relative_path = absolute_path.relative_to(media_dir)
local_uri = translator.path_to_local_track_uri(relative_path)
local_uri = translator.path_to_local_track_uri(
absolute_path, media_dir
)
mtime = file_mtimes.get(absolute_path)
track = tags.convert_tags_to_track(result.tags).replace(
uri=local_uri, length=result.duration, last_modified=mtime
Expand Down
15 changes: 9 additions & 6 deletions mopidy_local/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ def local_uri_to_path(local_uri: str, media_dir: Path) -> Path:

def path_to_file_uri(path: Union[str, bytes, Path]) -> str:
"""Convert absolute path to file URI."""
return Path(os.fsdecode(path)).as_uri()
ppath = Path(os.fsdecode(path))
assert ppath.is_absolute()
return ppath.as_uri()


def path_to_local_track_uri(relpath: Union[str, bytes, Path]) -> str:
"""Convert path relative to :confval:`local/media_dir` to local track
URI."""
bytes_path = os.fsencode(relpath)
return "local:track:%s" % urllib.parse.quote(bytes_path)
def path_to_local_track_uri(path: Union[str, bytes, Path], media_dir: Path) -> str:
"""Convert path to local track URI."""
ppath = Path(os.fsdecode(path))
if ppath.is_absolute():
ppath = ppath.relative_to(media_dir)
return "local:track:%s" % urllib.parse.quote(bytes(ppath))

This comment has been minimized.

Copy link
@kingosticks

kingosticks Jan 2, 2020

Member

Could be an f-string

7 changes: 5 additions & 2 deletions tests/test_library.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pathlib
import os
import unittest

Expand Down Expand Up @@ -42,7 +43,7 @@ def tearDown(self): # noqa: N802

def test_add_noname_ascii(self):
name = "Test.mp3"
uri = translator.path_to_local_track_uri(name)
uri = translator.path_to_local_track_uri(name, pathlib.Path("/media/dir"))
track = Track(name=name, uri=uri)
self.storage.begin()
self.storage.add(track)
Expand All @@ -51,7 +52,9 @@ def test_add_noname_ascii(self):

def test_add_noname_utf8(self):
name = "Mi\xf0vikudags.mp3"
uri = translator.path_to_local_track_uri(name.encode())
uri = translator.path_to_local_track_uri(
name.encode(), pathlib.Path("/media/dir")
)
track = Track(name=name, uri=uri)
self.storage.begin()
self.storage.add(track)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,16 @@ def test_path_to_file_uri(path, uri):
"path,uri",
[
(pathlib.Path("foo"), "local:track:foo"),
(pathlib.Path("/home/alice/Music/foo"), "local:track:foo"),
(pathlib.Path("æøå"), "local:track:%C3%A6%C3%B8%C3%A5"),
(pathlib.Path(os.fsdecode(b"\x00\x01\x02")), "local:track:%00%01%02"),
(pathlib.Path("æøå"), "local:track:%C3%A6%C3%B8%C3%A5"),

This comment has been minimized.

Copy link
@kingosticks

kingosticks Jan 2, 2020

Member

This is same as line 96

],
)
def test_path_to_local_track_uri(path, uri):
result = translator.path_to_local_track_uri(path)
media_dir = pathlib.Path("/home/alice/Music")

result = translator.path_to_local_track_uri(path, media_dir)

assert isinstance(result, str)
assert result == uri

0 comments on commit 7a9700f

Please sign in to comment.