-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
92 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import re | ||
import six | ||
import platform | ||
|
||
|
||
_WINDOWS_PLATFORM = platform.system() == "Windows" | ||
|
||
|
||
def url_quote(path_snippet): | ||
""" | ||
On Windows, it will separate drive letter and quote windows | ||
path alone. No magic on Unix-alie path, just pythonic | ||
`pathname2url` | ||
Arguments: | ||
path_snippet: a file path, relative or absolute. | ||
""" | ||
# type: (Text) -> Text | ||
if _WINDOWS_PLATFORM and _has_drive_letter(path_snippet): | ||
drive_letter, path = path_snippet.split(":", 1) | ||
if six.PY2: | ||
path = path.encode("utf-8") | ||
path = six.moves.urllib.request.pathname2url(path) | ||
path_snippet = "{}:{}".format(drive_letter, path) | ||
else: | ||
if six.PY2: | ||
path_snippet = path_snippet.encode("utf-8") | ||
path_snippet = six.moves.urllib.request.pathname2url(path_snippet) | ||
return path_snippet | ||
|
||
|
||
def _has_drive_letter(path_snippet): | ||
""" | ||
The following path will get True | ||
D:/Data | ||
C:\\My Dcouments\\ test | ||
And will get False | ||
/tmp/abc:test | ||
Arguments: | ||
path_snippet: a file path, relative or absolute. | ||
""" | ||
# type: (Text) -> Text | ||
windows_drive_pattern = ".:[/\\\\].*$" | ||
return re.match(windows_drive_pattern, path_snippet) is not None |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import platform | ||
import unittest | ||
|
||
from fs._url_tools import url_quote | ||
|
||
|
||
class TestBase(unittest.TestCase): | ||
def test_quote(self): | ||
test_fixtures = [ | ||
# test_snippet, expected | ||
["foo/bar/egg/foofoo", "foo/bar/egg/foofoo"], | ||
["foo/bar ha/barz", "foo/bar%20ha/barz"], | ||
["example b.txt", "example%20b.txt"], | ||
["exampleㄓ.txt", "example%E3%84%93.txt"], | ||
] | ||
if platform.system() == "Windows": | ||
test_fixtures.extend( | ||
[ | ||
["C:\\My Documents\\test.txt", "C:/My%20Documents/test.txt"], | ||
["C:/My Documents/test.txt", "C:/My%20Documents/test.txt"], | ||
# on Windows '\' is regarded as path separator | ||
["test/forward\\slash", "test/forward/slash"], | ||
] | ||
) | ||
else: | ||
test_fixtures.extend( | ||
[ | ||
# colon:tmp is bad path under Windows | ||
["test/colon:tmp", "test/colon%3Atmp"], | ||
# Unix treat \ as %5C | ||
["test/forward\\slash", "test/forward%5Cslash"], | ||
] | ||
) | ||
for test_snippet, expected in test_fixtures: | ||
self.assertEqual(url_quote(test_snippet), expected) |