Skip to content

Commit

Permalink
Refactor file-url-to-path
Browse files Browse the repository at this point in the history
This is the same issue as #112, so I added a general method to do this conversion.
  • Loading branch information
Tony Tung committed Sep 3, 2019
1 parent 08ca8bb commit 25568f3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
7 changes: 3 additions & 4 deletions slicedimage/io/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from packaging import version

from slicedimage.url.path import get_path_from_parsed_file_url
from slicedimage.url.resolve import resolve_url
from slicedimage._collection import Collection
from slicedimage._formats import ImageFormat
Expand Down Expand Up @@ -283,8 +284,7 @@ def partition_url_generator(self, parent_partition_url: str, partition_name: str
if self.partition_path_generator is None:
return super().partition_url_generator(parent_partition_url, partition_name)
parent_parsed_url = urllib.parse.urlparse(parent_partition_url)
assert parent_parsed_url.scheme == "file"
parent_path = PurePosixPath(parent_parsed_url.path)
parent_path = get_path_from_parsed_file_url(parent_parsed_url)
partition_path = self.partition_path_generator(parent_path, partition_name)
partition_parsed_url = parent_parsed_url._replace(path=str(partition_path))
return urllib.parse.urlunparse(partition_parsed_url)
Expand All @@ -310,8 +310,7 @@ def tile_url_generator(self, tileset_url: str, tile: Tile, ext: str) -> str:
if self.tile_opener is None:
return super().tile_url_generator(tileset_url, tile, ext)
tileset_parsed_url = urllib.parse.urlparse(tileset_url)
assert tileset_parsed_url.scheme == "file"
tileset_path = PurePosixPath(tileset_parsed_url.path)
tileset_path = get_path_from_parsed_file_url(tileset_parsed_url)
with self.tile_opener(tileset_path, tile, ext) as open_fh:
tile_path = open_fh.name

Expand Down
15 changes: 15 additions & 0 deletions slicedimage/url/path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pathlib
import posixpath
import urllib.parse
Expand Down Expand Up @@ -104,3 +105,17 @@ def calculate_relative_url(baseurl: str, name_or_url: str) -> str:
result = result + "#" + absolute_url_parsed.fragment

return result


def get_path_from_parsed_file_url(parsed_file_url: urllib.parse.ParseResult) -> pathlib.Path:
"""If parsed_file_url is the result of parsing a URL using urllib.parse.unparse, and the URL is
a "file:" URL, then extract the local filesystem path. Handles idiosyncrasies such as pathlib
and Windows paths.
"""
assert parsed_file_url.scheme == "file"
if os.name == "nt":
# pathlib can parse c:/windows/xxx, but not /c:/windows/xxx. however, url paths always
# start with a /
return pathlib.Path(urllib.parse.unquote(parsed_file_url.path[1:]))
else:
return pathlib.Path(urllib.parse.unquote(parsed_file_url.path))
9 changes: 2 additions & 7 deletions slicedimage/url/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from slicedimage._compat import fspath
from slicedimage.backends import CachingBackend, DiskBackend, HttpBackend, S3Backend, SIZE_LIMIT
from .path import get_absolute_url
from .path import get_absolute_url, get_path_from_parsed_file_url


def infer_backend(baseurl, backend_config=None):
Expand All @@ -26,12 +26,7 @@ def infer_backend(baseurl, backend_config=None):
parsed = urllib.parse.urlparse(baseurl)

if parsed.scheme == "file":
if os.name == "nt":
# pathlib can parse c:/windows/xxx, but not /c:/windows/xxx. however, url paths always
# start with a /
local_path = pathlib.Path(urllib.parse.unquote(parsed.path[1:]))
else:
local_path = pathlib.Path(urllib.parse.unquote(parsed.path))
local_path = get_path_from_parsed_file_url(parsed)
return DiskBackend(fspath(local_path))

if parsed.scheme in ("http", "https"):
Expand Down

0 comments on commit 25568f3

Please sign in to comment.