Skip to content

Commit

Permalink
Move human_quote into the quoters module (#1361)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 21, 2024
1 parent 55967c2 commit 12413d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
14 changes: 14 additions & 0 deletions yarl/_quoters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Quoting and unquoting utilities for URL parts."""

from typing import Union
from urllib.parse import quote

from ._quoting import _Quoter, _Unquoter

QUOTER = _Quoter(requote=False)
Expand All @@ -16,3 +19,14 @@
PATH_UNQUOTER = _Unquoter(unsafe="+")
PATH_SAFE_UNQUOTER = _Unquoter(ignore="/%", unsafe="+")
QS_UNQUOTER = _Unquoter(qs=True)


def human_quote(s: Union[str, None], unsafe: str) -> Union[str, None]:
if not s:
return s
for c in "%" + unsafe:
if c in s:
s = s.replace(c, f"%{ord(c):02X}")
if s.isprintable():
return s
return "".join(c if c.isprintable() else quote(c) for c in s)
24 changes: 7 additions & 17 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from functools import _CacheInfo, lru_cache
from ipaddress import ip_address
from typing import TYPE_CHECKING, Any, TypedDict, TypeVar, Union, overload
from urllib.parse import SplitResult, parse_qsl, quote, uses_relative
from urllib.parse import SplitResult, parse_qsl, uses_relative

import idna
from multidict import MultiDict, MultiDictProxy
Expand Down Expand Up @@ -34,6 +34,7 @@
QUOTER,
REQUOTER,
UNQUOTER,
human_quote,
)

DEFAULT_PORTS = {"http": 80, "https": 443, "ws": 80, "wss": 443, "ftp": 21}
Expand Down Expand Up @@ -1334,36 +1335,25 @@ def joinpath(self, *other: str, encoded: bool = False) -> "URL":

def human_repr(self) -> str:
"""Return decoded human readable string for URL representation."""
user = _human_quote(self.user, "#/:?@[]")
password = _human_quote(self.password, "#/:?@[]")
user = human_quote(self.user, "#/:?@[]")
password = human_quote(self.password, "#/:?@[]")
if (host := self.host) and ":" in host:
host = f"[{host}]"
path = _human_quote(self.path, "#?")
path = human_quote(self.path, "#?")
if TYPE_CHECKING:
assert path is not None
query_string = "&".join(
"{}={}".format(_human_quote(k, "#&+;="), _human_quote(v, "#&+;="))
"{}={}".format(human_quote(k, "#&+;="), human_quote(v, "#&+;="))
for k, v in self.query.items()
)
fragment = _human_quote(self.fragment, "")
fragment = human_quote(self.fragment, "")
if TYPE_CHECKING:
assert fragment is not None
netloc = make_netloc(user, password, host, self.explicit_port)
scheme = self._val.scheme
return unsplit_result(scheme, netloc, path, query_string, fragment)


def _human_quote(s: Union[str, None], unsafe: str) -> Union[str, None]:
if not s:
return s
for c in "%" + unsafe:
if c in s:
s = s.replace(c, f"%{ord(c):02X}")
if s.isprintable():
return s
return "".join(c if c.isprintable() else quote(c) for c in s)


_DEFAULT_IDNA_SIZE = 256
_DEFAULT_ENCODE_SIZE = 512

Expand Down

0 comments on commit 12413d3

Please sign in to comment.