-
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.
Replace file URI creating by yarl's
URL()
- Loading branch information
Showing
17 changed files
with
131 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,33 @@ | ||
import os | ||
from typing import Any, Union | ||
|
||
from yarl import URL | ||
|
||
MAGNET_SCHEME = 'magnet' | ||
HTTP_SCHEME = 'http' | ||
FILE_SCHEME = 'file' | ||
|
||
|
||
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 |
43 changes: 43 additions & 0 deletions
43
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,43 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from tribler_common.rest_utils import path_to_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'), | ||
] | ||
|
||
|
||
# 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 |
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
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
Oops, something went wrong.