-
Notifications
You must be signed in to change notification settings - Fork 452
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 #6738 from drew2a/fix/6700
Fix/6700
- Loading branch information
Showing
23 changed files
with
405 additions
and
239 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
from typing import Any, Union | ||
|
||
from yarl import URL | ||
|
||
MAGNET_SCHEME = 'magnet' | ||
FILE_SCHEME = 'file' | ||
HTTP_SCHEME = 'http' | ||
HTTPS_SCHEME = 'https' | ||
|
||
|
||
def path_to_uri(file_path: Union[str, Any]) -> str: | ||
"""Convert path to url | ||
Example: | ||
'/path/to/file' -> 'file:///path/to/file' | ||
""" | ||
if not isinstance(file_path, str): | ||
file_path = str(file_path) | ||
return str(URL().build(scheme=FILE_SCHEME, path=file_path)) | ||
|
||
|
||
def uri_to_path(file_uri: str) -> str: | ||
"""Convert uri to path | ||
Example: | ||
'file:///path/to/file' -> '/path/to/file' | ||
""" | ||
path = URL(file_uri).path | ||
if os.name == 'nt': | ||
# Removes first slash for win OS | ||
# see https://github.com/aio-libs/yarl/issues/674 | ||
return path.lstrip('/') | ||
return path | ||
|
||
|
||
def scheme_from_uri(uri: str) -> str: | ||
"""Get scheme from URI | ||
Examples: | ||
'file:///some/file' -> 'file' | ||
'magnet:link' -> 'magnet' | ||
'http://en.wikipedia.org' -> 'http' | ||
""" | ||
return URL(uri).scheme |
53 changes: 53 additions & 0 deletions
53
src/tribler-common/tribler_common/tests/test_rest_utils.py
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from tribler_common.rest_utils import path_to_uri, scheme_from_uri, uri_to_path | ||
|
||
NIX_PATHS = [ | ||
('/path/to/file', 'file:///path/to/file'), | ||
('/path/to/file with space', 'file:///path/to/file%20with%20space'), | ||
('/path/to/%20%21file', 'file:///path/to/%2520%2521file'), # See: https://github.com/Tribler/tribler/issues/6700 | ||
] | ||
|
||
WIN_PATHS = [ | ||
('C:\\path\\to\\file', 'file:///C:%5Cpath%5Cto%5Cfile'), | ||
('C:\\path\\to\\file with space', 'file:///C:%5Cpath%5Cto%5Cfile%20with%20space'), | ||
('C:\\path\\to\\%20%21file', 'file:///C:%5Cpath%5Cto%5C%2520%2521file'), | ||
] | ||
|
||
SCHEMES = [ | ||
('file:///path/to/file', 'file'), | ||
('magnet:link', 'magnet'), | ||
('http://en.wikipedia.org', 'http'), | ||
] | ||
|
||
# posix | ||
@pytest.mark.parametrize('path, uri', NIX_PATHS) | ||
@patch('os.name', 'posix') | ||
def test_path_to_uri(path, uri): | ||
assert path_to_uri(path) == uri | ||
|
||
|
||
@pytest.mark.parametrize('path, uri', NIX_PATHS) | ||
@patch('os.name', 'posix') | ||
def test_uri_to_path(path, uri): | ||
assert uri_to_path(uri) == path | ||
|
||
|
||
# win | ||
@pytest.mark.parametrize('path, uri', WIN_PATHS) | ||
@patch('os.name', 'nt') | ||
def test_path_to_uri_win(path, uri): | ||
assert path_to_uri(path) == uri | ||
|
||
|
||
@pytest.mark.parametrize('path, uri', WIN_PATHS) | ||
@patch('os.name', 'nt') | ||
def test_uri_to_path_win(path, uri): | ||
assert uri_to_path(uri) == path | ||
|
||
|
||
@pytest.mark.parametrize('path, scheme', SCHEMES) | ||
def test_scheme_from_uri(path, scheme): | ||
assert scheme_from_uri(path) == scheme |
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
Oops, something went wrong.