Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stdlib Path <-> URL conversions #153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/unearth/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from unearth.fetchers import Fetcher, Response
from unearth.link import Link
from unearth.utils import is_archive_file, path_to_url
from unearth.utils import is_archive_file

SUPPORTED_CONTENT_TYPES = (
"text/html",
Expand Down Expand Up @@ -165,15 +165,15 @@ def collect_links_from_location(
if path.is_dir():
if expand:
for child in path.iterdir():
file_url = path_to_url(str(child))
file_url = child.as_uri()
if _is_html_file(file_url):
yield from _collect_links_from_index(
session, Link(file_url), headers
)
else:
yield Link(file_url)
else:
index_html = Link(path_to_url(path.joinpath("index.html").as_posix()))
index_html = Link(path.joinpath("index.html").as_uri())
yield from _collect_links_from_index(session, index_html, headers)
else:
if _is_html_file(str(path)):
Expand Down
5 changes: 2 additions & 3 deletions src/unearth/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from unearth.utils import (
add_ssh_scheme_to_git_uri,
parse_query,
path_to_url,
split_auth_from_url,
url_to_path,
)
Expand Down Expand Up @@ -86,7 +85,7 @@ def __eq__(self, __o: object) -> bool:
@classmethod
def from_path(cls, file_path: str | pathlib.Path) -> Link:
"""Create a link from a local file path"""
url = path_to_url(str(file_path))
url = pathlib.Path(file_path).as_uri()
return cls(url)

@property
Expand All @@ -95,7 +94,7 @@ def is_file(self) -> bool:

@property
def file_path(self) -> pathlib.Path:
return pathlib.Path(url_to_path(self.url_without_fragment))
return url_to_path(self.url_without_fragment)

@property
def is_vcs(self) -> bool:
Expand Down
18 changes: 7 additions & 11 deletions src/unearth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import warnings
from pathlib import Path
from typing import Callable, Iterable, Iterator, Sequence, TypeVar
from urllib.request import pathname2url, url2pathname
from urllib.request import url2pathname

WINDOWS = sys.platform == "win32"
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -60,7 +60,7 @@ def parse_netloc(netloc: str) -> tuple[str, int | None]:
return parsed.hostname or "", parsed.port


def url_to_path(url: str) -> str:
def url_to_path(url: str) -> Path:
Copy link
Contributor Author

@wimglenn wimglenn Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once Path.from_uri is available in all supported Python versions, this whole function can presumably be deleted.

"""
Convert a file: URL to a path.
"""
Expand Down Expand Up @@ -96,17 +96,13 @@ def url_to_path(url: str) -> str:
):
path = path[1:]

return path
return Path(path)


def path_to_url(path: str) -> str:
"""
Convert a path to a file: URL. The path will be made absolute and have
quoted path parts.
"""
path = os.path.normpath(os.path.abspath(path))
url = parse.urljoin("file:", pathname2url(path))
return url
if sys.version_info >= (3, 13):

def url_to_path(url: str) -> Path:
return Path.from_uri(url)


WHEEL_EXTENSION = ".whl"
Expand Down
4 changes: 2 additions & 2 deletions src/unearth/vcs/bazaar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from unearth.errors import UnpackError
from unearth.link import Link
from unearth.utils import display_path, path_to_url
from unearth.utils import display_path
from unearth.vcs.base import HiddenText, VersionControl, vcs_support

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -58,7 +58,7 @@ def get_remote_url(self, location: Path) -> str:
if line.startswith(x):
repo = line.split(x)[1]
if self._is_local_repository(repo):
return path_to_url(repo)
return Path(repo).as_uri()
return repo
raise UnpackError(f"Remote not found for {display_path(location)}")

Expand Down
4 changes: 2 additions & 2 deletions src/unearth/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from unearth.errors import UnpackError
from unearth.link import Link
from unearth.utils import add_ssh_scheme_to_git_uri, display_path, path_to_url
from unearth.utils import add_ssh_scheme_to_git_uri, display_path
from unearth.vcs.base import HiddenText, VersionControl, vcs_support

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -112,7 +112,7 @@ def _git_remote_to_pip_url(self, url: str) -> str:
if "://" in url:
return url
if os.path.exists(url):
return path_to_url(os.path.abspath(url))
return Path(os.path.abspath(url)).as_uri()
else:
return add_ssh_scheme_to_git_uri(url)

Expand Down
4 changes: 2 additions & 2 deletions src/unearth/vcs/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from pathlib import Path

from unearth.utils import display_path, path_to_url
from unearth.utils import display_path
from unearth.vcs.base import HiddenText, VersionControl, vcs_support

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,5 +61,5 @@ def get_remote_url(self, location: Path) -> str:
cwd=location,
).stdout.strip()
if self._is_local_repository(url):
url = path_to_url(url)
url = Path(url).as_uri()
return url.strip()