-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from jodal/scanner-improvements
Scanner improvements
- Loading branch information
Showing
8 changed files
with
173 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import os | ||
import pathlib | ||
import urllib | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def local_uri_to_file_uri(uri, media_dir): | ||
def local_uri_to_file_uri(local_uri: str, media_dir: Path) -> str: | ||
"""Convert local track or directory URI to file URI.""" | ||
return path_to_file_uri(local_uri_to_path(uri, media_dir)) | ||
path = local_uri_to_path(local_uri, media_dir) | ||
return path.as_uri() | ||
|
||
|
||
def local_uri_to_path(uri, media_dir): | ||
def local_uri_to_path(local_uri: str, media_dir: Path) -> Path: | ||
"""Convert local track or directory URI to absolute path.""" | ||
if not uri.startswith("local:directory:") and not uri.startswith("local:track:"): | ||
if not local_uri.startswith(("local:directory:", "local:track:")): | ||
raise ValueError("Invalid URI.") | ||
uri_path = uri.split(":", 2)[2] | ||
file_bytes = urllib.parse.unquote_to_bytes(urllib.parse.urlsplit(uri_path).path) | ||
file_path = pathlib.Path(os.fsdecode(file_bytes)) | ||
uri_path = urllib.parse.urlsplit(local_uri.split(":", 2)[2]).path | ||
file_bytes = urllib.parse.unquote_to_bytes(uri_path) | ||
file_path = Path(os.fsdecode(file_bytes)) | ||
return media_dir / file_path | ||
|
||
|
||
def path_to_file_uri(path): | ||
def path_to_file_uri(path: Union[str, bytes, Path]) -> str: | ||
"""Convert absolute path to file URI.""" | ||
return pathlib.Path(os.fsdecode(path)).as_uri() | ||
|
||
|
||
def path_to_local_track_uri(relpath): | ||
"""Convert path relative to :confval:`local/media_dir` to local track | ||
URI.""" | ||
if isinstance(relpath, str): | ||
relpath = relpath.encode() | ||
return "local:track:%s" % urllib.parse.quote(relpath) | ||
|
||
|
||
def path_to_local_directory_uri(relpath): | ||
"""Convert path relative to :confval:`local/media_dir` to directory URI.""" | ||
if isinstance(relpath, str): | ||
relpath = relpath.encode() | ||
return "local:directory:%s" % urllib.parse.quote(relpath) | ||
ppath = Path(os.fsdecode(path)) | ||
assert ppath.is_absolute() | ||
return ppath.as_uri() | ||
|
||
|
||
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) | ||
quoted_path = urllib.parse.quote(bytes(ppath)) | ||
return f"local:track:{quoted_path}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.