Skip to content

Commit

Permalink
Fix win path
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 24, 2022
1 parent 65fa25e commit e791fbe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/tribler-common/tribler_common/rest_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from typing import Any, Union

from yarl import URL
Expand All @@ -6,6 +7,8 @@
HTTP_SCHEME = 'http'
FILE_SCHEME = 'file'

re_fix_win_path = re.compile(r'^(?:/)(\w:)', re.UNICODE)


def to_file_uri(file_path: Union[str, Any]) -> str:
if not isinstance(file_path, str):
Expand All @@ -14,4 +17,6 @@ def to_file_uri(file_path: Union[str, Any]) -> str:


def from_file_uri(file_uri: str) -> str:
return URL(file_uri).path
path = URL(file_uri).path
corrected_path = re_fix_win_path.sub(r'\1', path)
return corrected_path
36 changes: 31 additions & 5 deletions src/tribler-common/tribler_common/tests/test_rest_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
from tribler_common.rest_utils import from_file_uri, to_file_uri
import pytest

from tribler_common.rest_utils import from_file_uri, re_fix_win_path, to_file_uri

def test_to_file_uri():
assert to_file_uri('/path') == 'file:///path'
PATHS = [
# nix
('/path/to/file', 'file:///path/to/file'),
('/path/to/%20%21file', 'file:///path/to/%2520%2521file'), # See: https://github.com/Tribler/tribler/issues/6700
# win
('E:\\path\\to\\file', 'file:///E:%5Cpath%5Cto%5Cfile'),
]

PATHS_TO_FIX = [
# correct
('c:\\', 'c:\\'),
('/path', '/path'),
# corrupted:
('/c:\\', 'c:\\'),
('/C:\\', 'C:\\'),
]

def test_from_file_uri():
assert from_file_uri('file:///path') == '/path'

@pytest.mark.parametrize('path,uri', PATHS)
def test_to_file_uri(path, uri):
assert to_file_uri(path) == uri


@pytest.mark.parametrize('path,uri', PATHS)
def test_from_file_uri(path, uri):
assert from_file_uri(uri) == path


@pytest.mark.parametrize('original,expected', PATHS_TO_FIX)
def test_re_fix_win_path(original, expected):
assert re_fix_win_path.sub(r'\1', original) == expected
2 changes: 1 addition & 1 deletion src/tribler-core/tribler_core/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ pyyaml==6.0
sentry-sdk==1.5.0
service-identity==21.1.0
yappi==1.3.3
yarl==1.7.0
yarl==1.7.2 # keep this dependency higher than 1.6.3. See: https://github.com/aio-libs/yarl/issues/517

0 comments on commit e791fbe

Please sign in to comment.