Add loan-advisor-agent
Browse files- loan-advisor-agent/.venv/Lib/site-packages/yarl/__init__.py +14 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_parse.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_path.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_query.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_quoters.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_quoting.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_url.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_parse.py +202 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_path.py +41 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_query.py +121 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoters.py +32 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoting.py +19 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoting_c.cp312-win_amd64.pyd +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoting_c.pyx +451 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoting_py.py +213 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/_url.py +1665 -0
- loan-advisor-agent/.venv/Lib/site-packages/yarl/py.typed +1 -0
loan-advisor-agent/.venv/Lib/site-packages/yarl/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ._query import Query, QueryVariable, SimpleQuery
|
| 2 |
+
from ._url import URL, cache_clear, cache_configure, cache_info
|
| 3 |
+
|
| 4 |
+
__version__ = "1.23.0"
|
| 5 |
+
|
| 6 |
+
__all__ = (
|
| 7 |
+
"URL",
|
| 8 |
+
"SimpleQuery",
|
| 9 |
+
"QueryVariable",
|
| 10 |
+
"Query",
|
| 11 |
+
"cache_clear",
|
| 12 |
+
"cache_configure",
|
| 13 |
+
"cache_info",
|
| 14 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (461 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_parse.cpython-312.pyc
ADDED
|
Binary file (6.84 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_path.cpython-312.pyc
ADDED
|
Binary file (1.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_query.cpython-312.pyc
ADDED
|
Binary file (4.98 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_quoters.cpython-312.pyc
ADDED
|
Binary file (1.94 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_quoting.cpython-312.pyc
ADDED
|
Binary file (755 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/__pycache__/_url.cpython-312.pyc
ADDED
|
Binary file (62.2 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_parse.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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2 |
+
|
| 3 |
+
import re
|
| 4 |
+
import unicodedata
|
| 5 |
+
from functools import lru_cache
|
| 6 |
+
from urllib.parse import scheme_chars, uses_netloc
|
| 7 |
+
|
| 8 |
+
from ._quoters import QUOTER, UNQUOTER_PLUS
|
| 9 |
+
|
| 10 |
+
# Leading and trailing C0 control and space to be stripped per WHATWG spec.
|
| 11 |
+
# == "".join([chr(i) for i in range(0, 0x20 + 1)])
|
| 12 |
+
WHATWG_C0_CONTROL_OR_SPACE = (
|
| 13 |
+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10"
|
| 14 |
+
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f "
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Unsafe bytes to be removed per WHATWG spec
|
| 18 |
+
UNSAFE_URL_BYTES_TO_REMOVE = ["\t", "\r", "\n"]
|
| 19 |
+
USES_AUTHORITY = frozenset(uses_netloc)
|
| 20 |
+
|
| 21 |
+
SplitURLType = tuple[str, str, str, str, str]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def split_url(url: str) -> SplitURLType:
|
| 25 |
+
"""Split URL into parts."""
|
| 26 |
+
# Adapted from urllib.parse.urlsplit
|
| 27 |
+
# Only lstrip url as some applications rely on preserving trailing space.
|
| 28 |
+
# (https://url.spec.whatwg.org/#concept-basic-url-parser would strip both)
|
| 29 |
+
url = url.lstrip(WHATWG_C0_CONTROL_OR_SPACE)
|
| 30 |
+
for b in UNSAFE_URL_BYTES_TO_REMOVE:
|
| 31 |
+
if b in url:
|
| 32 |
+
url = url.replace(b, "")
|
| 33 |
+
|
| 34 |
+
scheme = netloc = query = fragment = ""
|
| 35 |
+
i = url.find(":")
|
| 36 |
+
if i > 0 and url[0] in scheme_chars:
|
| 37 |
+
for c in url[1:i]:
|
| 38 |
+
if c not in scheme_chars:
|
| 39 |
+
break
|
| 40 |
+
else:
|
| 41 |
+
scheme, url = url[:i].lower(), url[i + 1 :]
|
| 42 |
+
has_hash = "#" in url
|
| 43 |
+
has_question_mark = "?" in url
|
| 44 |
+
if url[:2] == "//":
|
| 45 |
+
delim = len(url) # position of end of domain part of url, default is end
|
| 46 |
+
if has_hash and has_question_mark:
|
| 47 |
+
delim_chars = "/?#"
|
| 48 |
+
elif has_question_mark:
|
| 49 |
+
delim_chars = "/?"
|
| 50 |
+
elif has_hash:
|
| 51 |
+
delim_chars = "/#"
|
| 52 |
+
else:
|
| 53 |
+
delim_chars = "/"
|
| 54 |
+
for c in delim_chars: # look for delimiters; the order is NOT important
|
| 55 |
+
wdelim = url.find(c, 2) # find first of this delim
|
| 56 |
+
if wdelim >= 0 and wdelim < delim: # if found
|
| 57 |
+
delim = wdelim # use earliest delim position
|
| 58 |
+
netloc = url[2:delim]
|
| 59 |
+
url = url[delim:]
|
| 60 |
+
has_left_bracket = "[" in netloc
|
| 61 |
+
has_right_bracket = "]" in netloc
|
| 62 |
+
if (has_left_bracket and not has_right_bracket) or (
|
| 63 |
+
has_right_bracket and not has_left_bracket
|
| 64 |
+
):
|
| 65 |
+
raise ValueError("Invalid IPv6 URL")
|
| 66 |
+
if has_left_bracket:
|
| 67 |
+
bracketed_host = netloc.partition("[")[2].partition("]")[0]
|
| 68 |
+
# Valid bracketed hosts are defined in
|
| 69 |
+
# https://www.rfc-editor.org/rfc/rfc3986#page-49
|
| 70 |
+
# https://url.spec.whatwg.org/
|
| 71 |
+
if bracketed_host and bracketed_host[0] == "v":
|
| 72 |
+
if not re.match(r"\Av[a-fA-F0-9]+\..+\Z", bracketed_host):
|
| 73 |
+
raise ValueError("IPvFuture address is invalid")
|
| 74 |
+
elif ":" not in bracketed_host:
|
| 75 |
+
raise ValueError("The IPv6 content between brackets is not valid")
|
| 76 |
+
if has_hash:
|
| 77 |
+
url, _, fragment = url.partition("#")
|
| 78 |
+
if has_question_mark:
|
| 79 |
+
url, _, query = url.partition("?")
|
| 80 |
+
if netloc and not netloc.isascii():
|
| 81 |
+
_check_netloc(netloc)
|
| 82 |
+
return scheme, netloc, url, query, fragment
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def _check_netloc(netloc: str) -> None:
|
| 86 |
+
# Adapted from urllib.parse._checknetloc
|
| 87 |
+
# looking for characters like \u2100 that expand to 'a/c'
|
| 88 |
+
# IDNA uses NFKC equivalence, so normalize for this check
|
| 89 |
+
|
| 90 |
+
# ignore characters already included
|
| 91 |
+
# but not the surrounding text
|
| 92 |
+
n = netloc.replace("@", "").replace(":", "").replace("#", "").replace("?", "")
|
| 93 |
+
normalized_netloc = unicodedata.normalize("NFKC", n)
|
| 94 |
+
if n == normalized_netloc:
|
| 95 |
+
return
|
| 96 |
+
# Note that there are no unicode decompositions for the character '@' so
|
| 97 |
+
# its currently impossible to have test coverage for this branch, however if the
|
| 98 |
+
# one should be added in the future we want to make sure its still checked.
|
| 99 |
+
for c in "/?#@:": # pragma: no branch
|
| 100 |
+
if c in normalized_netloc:
|
| 101 |
+
raise ValueError(
|
| 102 |
+
f"netloc '{netloc}' contains invalid "
|
| 103 |
+
"characters under NFKC normalization"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@lru_cache # match the same size as urlsplit
|
| 108 |
+
def split_netloc(
|
| 109 |
+
netloc: str,
|
| 110 |
+
) -> tuple[str | None, str | None, str | None, int | None]:
|
| 111 |
+
"""Split netloc into username, password, host and port."""
|
| 112 |
+
if "@" not in netloc:
|
| 113 |
+
username: str | None = None
|
| 114 |
+
password: str | None = None
|
| 115 |
+
hostinfo = netloc
|
| 116 |
+
else:
|
| 117 |
+
userinfo, _, hostinfo = netloc.rpartition("@")
|
| 118 |
+
username, have_password, password = userinfo.partition(":")
|
| 119 |
+
if not have_password:
|
| 120 |
+
password = None
|
| 121 |
+
|
| 122 |
+
if "[" in hostinfo:
|
| 123 |
+
_, _, bracketed = hostinfo.partition("[")
|
| 124 |
+
hostname, _, port_str = bracketed.partition("]")
|
| 125 |
+
_, _, port_str = port_str.partition(":")
|
| 126 |
+
else:
|
| 127 |
+
hostname, _, port_str = hostinfo.partition(":")
|
| 128 |
+
|
| 129 |
+
if not port_str:
|
| 130 |
+
return username or None, password, hostname or None, None
|
| 131 |
+
|
| 132 |
+
try:
|
| 133 |
+
port = int(port_str)
|
| 134 |
+
except ValueError:
|
| 135 |
+
raise ValueError("Invalid URL: port can't be converted to integer")
|
| 136 |
+
if not (0 <= port <= 65535):
|
| 137 |
+
raise ValueError("Port out of range 0-65535")
|
| 138 |
+
return username or None, password, hostname or None, port
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def unsplit_result(
|
| 142 |
+
scheme: str, netloc: str, url: str, query: str, fragment: str
|
| 143 |
+
) -> str:
|
| 144 |
+
"""Unsplit a URL without any normalization."""
|
| 145 |
+
if netloc or (scheme and scheme in USES_AUTHORITY) or url[:2] == "//":
|
| 146 |
+
if url and url[:1] != "/":
|
| 147 |
+
url = f"{scheme}://{netloc}/{url}" if scheme else f"{scheme}:{url}"
|
| 148 |
+
else:
|
| 149 |
+
url = f"{scheme}://{netloc}{url}" if scheme else f"//{netloc}{url}"
|
| 150 |
+
elif scheme:
|
| 151 |
+
url = f"{scheme}:{url}"
|
| 152 |
+
if query:
|
| 153 |
+
url = f"{url}?{query}"
|
| 154 |
+
return f"{url}#{fragment}" if fragment else url
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
@lru_cache # match the same size as urlsplit
|
| 158 |
+
def make_netloc(
|
| 159 |
+
user: str | None,
|
| 160 |
+
password: str | None,
|
| 161 |
+
host: str | None,
|
| 162 |
+
port: int | None,
|
| 163 |
+
encode: bool = False,
|
| 164 |
+
) -> str:
|
| 165 |
+
"""Make netloc from parts.
|
| 166 |
+
|
| 167 |
+
The user and password are encoded if encode is True.
|
| 168 |
+
|
| 169 |
+
The host must already be encoded with _encode_host.
|
| 170 |
+
"""
|
| 171 |
+
if host is None:
|
| 172 |
+
return ""
|
| 173 |
+
ret = host
|
| 174 |
+
if port is not None:
|
| 175 |
+
ret = f"{ret}:{port}"
|
| 176 |
+
if user is None and password is None:
|
| 177 |
+
return ret
|
| 178 |
+
if password is not None:
|
| 179 |
+
if not user:
|
| 180 |
+
user = ""
|
| 181 |
+
elif encode:
|
| 182 |
+
user = QUOTER(user)
|
| 183 |
+
if encode:
|
| 184 |
+
password = QUOTER(password)
|
| 185 |
+
user = f"{user}:{password}"
|
| 186 |
+
elif user and encode:
|
| 187 |
+
user = QUOTER(user)
|
| 188 |
+
return f"{user}@{ret}" if user else ret
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
def query_to_pairs(query_string: str) -> list[tuple[str, str]]:
|
| 192 |
+
"""Parse a query given as a string argument.
|
| 193 |
+
|
| 194 |
+
Works like urllib.parse.parse_qsl with keep empty values.
|
| 195 |
+
"""
|
| 196 |
+
pairs: list[tuple[str, str]] = []
|
| 197 |
+
if not query_string:
|
| 198 |
+
return pairs
|
| 199 |
+
for k_v in query_string.split("&"):
|
| 200 |
+
k, _, v = k_v.partition("=")
|
| 201 |
+
pairs.append((UNQUOTER_PLUS(k), UNQUOTER_PLUS(v)))
|
| 202 |
+
return pairs
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_path.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Utilities for working with paths."""
|
| 2 |
+
|
| 3 |
+
from collections.abc import Sequence
|
| 4 |
+
from contextlib import suppress
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def normalize_path_segments(segments: Sequence[str]) -> list[str]:
|
| 8 |
+
"""Drop '.' and '..' from a sequence of str segments"""
|
| 9 |
+
|
| 10 |
+
resolved_path: list[str] = []
|
| 11 |
+
|
| 12 |
+
for seg in segments:
|
| 13 |
+
if seg == "..":
|
| 14 |
+
# ignore any .. segments that would otherwise cause an
|
| 15 |
+
# IndexError when popped from resolved_path if
|
| 16 |
+
# resolving for rfc3986
|
| 17 |
+
with suppress(IndexError):
|
| 18 |
+
resolved_path.pop()
|
| 19 |
+
elif seg != ".":
|
| 20 |
+
resolved_path.append(seg)
|
| 21 |
+
|
| 22 |
+
if segments and segments[-1] in (".", ".."):
|
| 23 |
+
# do some post-processing here.
|
| 24 |
+
# if the last segment was a relative dir,
|
| 25 |
+
# then we need to append the trailing '/'
|
| 26 |
+
resolved_path.append("")
|
| 27 |
+
|
| 28 |
+
return resolved_path
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def normalize_path(path: str) -> str:
|
| 32 |
+
# Drop '.' and '..' from str path
|
| 33 |
+
prefix = ""
|
| 34 |
+
if path and path[0] == "/":
|
| 35 |
+
# preserve the "/" root element of absolute paths, copying it to the
|
| 36 |
+
# normalised output as per sections 5.2.4 and 6.2.2.3 of rfc3986.
|
| 37 |
+
prefix = "/"
|
| 38 |
+
path = path[1:]
|
| 39 |
+
|
| 40 |
+
segments = path.split("/")
|
| 41 |
+
return prefix + "/".join(normalize_path_segments(segments))
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_query.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Query string handling."""
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
from collections.abc import Iterable, Mapping, Sequence
|
| 5 |
+
from typing import TYPE_CHECKING, Any, SupportsInt, Union, cast
|
| 6 |
+
|
| 7 |
+
from multidict import istr
|
| 8 |
+
|
| 9 |
+
from ._quoters import QUERY_PART_QUOTER, QUERY_QUOTER
|
| 10 |
+
|
| 11 |
+
SimpleQuery = Union[str, SupportsInt, float]
|
| 12 |
+
QueryVariable = Union[SimpleQuery, Sequence[SimpleQuery]]
|
| 13 |
+
Query = Union[
|
| 14 |
+
None, str, Mapping[str, QueryVariable], Sequence[tuple[str, QueryVariable]]
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def query_var(v: SimpleQuery) -> str:
|
| 19 |
+
"""Convert a query variable to a string."""
|
| 20 |
+
cls = type(v)
|
| 21 |
+
if cls is int: # Fast path for non-subclassed int
|
| 22 |
+
return str(v)
|
| 23 |
+
if isinstance(v, str):
|
| 24 |
+
return v
|
| 25 |
+
if isinstance(v, float):
|
| 26 |
+
if math.isinf(v):
|
| 27 |
+
raise ValueError("float('inf') is not supported")
|
| 28 |
+
if math.isnan(v):
|
| 29 |
+
raise ValueError("float('nan') is not supported")
|
| 30 |
+
return str(float(v))
|
| 31 |
+
if cls is not bool and isinstance(v, SupportsInt):
|
| 32 |
+
return str(int(v))
|
| 33 |
+
raise TypeError(
|
| 34 |
+
"Invalid variable type: value "
|
| 35 |
+
"should be str, int or float, got {!r} "
|
| 36 |
+
"of type {}".format(v, cls)
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_str_query_from_sequence_iterable(
|
| 41 |
+
items: Iterable[tuple[str | istr, QueryVariable]],
|
| 42 |
+
) -> str:
|
| 43 |
+
"""Return a query string from a sequence of (key, value) pairs.
|
| 44 |
+
|
| 45 |
+
value is a single value or a sequence of values for the key
|
| 46 |
+
|
| 47 |
+
The sequence of values must be a list or tuple.
|
| 48 |
+
"""
|
| 49 |
+
quoter = QUERY_PART_QUOTER
|
| 50 |
+
pairs = [
|
| 51 |
+
f"{quoter(k)}={quoter(v if type(v) is str else query_var(v))}"
|
| 52 |
+
for k, val in items
|
| 53 |
+
for v in (
|
| 54 |
+
val if type(val) is not str and isinstance(val, (list, tuple)) else (val,)
|
| 55 |
+
)
|
| 56 |
+
]
|
| 57 |
+
return "&".join(pairs)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_str_query_from_iterable(
|
| 61 |
+
items: Iterable[tuple[str | istr, SimpleQuery]],
|
| 62 |
+
) -> str:
|
| 63 |
+
"""Return a query string from an iterable.
|
| 64 |
+
|
| 65 |
+
The iterable must contain (key, value) pairs.
|
| 66 |
+
|
| 67 |
+
The values are not allowed to be sequences, only single values are
|
| 68 |
+
allowed. For sequences, use `_get_str_query_from_sequence_iterable`.
|
| 69 |
+
"""
|
| 70 |
+
quoter = QUERY_PART_QUOTER
|
| 71 |
+
# A listcomp is used since listcomps are inlined on CPython 3.12+ and
|
| 72 |
+
# they are a bit faster than a generator expression.
|
| 73 |
+
pairs = [
|
| 74 |
+
f"{quoter(k)}={quoter(v if type(v) is str else query_var(v))}" for k, v in items
|
| 75 |
+
]
|
| 76 |
+
return "&".join(pairs)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def get_str_query(*args: Any, **kwargs: Any) -> str | None:
|
| 80 |
+
"""Return a query string from supported args."""
|
| 81 |
+
query: (
|
| 82 |
+
str
|
| 83 |
+
| Mapping[str, QueryVariable]
|
| 84 |
+
| Sequence[tuple[str | istr, SimpleQuery]]
|
| 85 |
+
| None
|
| 86 |
+
)
|
| 87 |
+
if kwargs:
|
| 88 |
+
if args:
|
| 89 |
+
msg = "Either kwargs or single query parameter must be present"
|
| 90 |
+
raise ValueError(msg)
|
| 91 |
+
query = kwargs
|
| 92 |
+
elif len(args) == 1:
|
| 93 |
+
query = args[0]
|
| 94 |
+
else:
|
| 95 |
+
raise ValueError("Either kwargs or single query parameter must be present")
|
| 96 |
+
|
| 97 |
+
if query is None:
|
| 98 |
+
return None
|
| 99 |
+
if not query:
|
| 100 |
+
return ""
|
| 101 |
+
if type(query) is dict:
|
| 102 |
+
return get_str_query_from_sequence_iterable(query.items())
|
| 103 |
+
if type(query) is str or isinstance(query, str):
|
| 104 |
+
return QUERY_QUOTER(query)
|
| 105 |
+
if isinstance(query, Mapping):
|
| 106 |
+
return get_str_query_from_sequence_iterable(query.items())
|
| 107 |
+
if isinstance(query, (bytes, bytearray, memoryview)):
|
| 108 |
+
msg = "Invalid query type: bytes, bytearray and memoryview are forbidden"
|
| 109 |
+
raise TypeError(msg)
|
| 110 |
+
if isinstance(query, Sequence):
|
| 111 |
+
# We don't expect sequence values if we're given a list of pairs
|
| 112 |
+
# already; only mappings like builtin `dict` which can't have the
|
| 113 |
+
# same key pointing to multiple values are allowed to use
|
| 114 |
+
# `_query_seq_pairs`.
|
| 115 |
+
if TYPE_CHECKING:
|
| 116 |
+
query = cast(Sequence[tuple[Union[str, istr], SimpleQuery]], query)
|
| 117 |
+
return get_str_query_from_iterable(query)
|
| 118 |
+
raise TypeError(
|
| 119 |
+
"Invalid query type: only str, mapping or "
|
| 120 |
+
"sequence of (key, value) pairs is allowed"
|
| 121 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoters.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Quoting and unquoting utilities for URL parts."""
|
| 2 |
+
|
| 3 |
+
from urllib.parse import quote
|
| 4 |
+
|
| 5 |
+
from ._quoting import _Quoter, _Unquoter
|
| 6 |
+
|
| 7 |
+
QUOTER = _Quoter(requote=False)
|
| 8 |
+
REQUOTER = _Quoter()
|
| 9 |
+
PATH_QUOTER = _Quoter(safe="@:", protected="/+", requote=False)
|
| 10 |
+
PATH_REQUOTER = _Quoter(safe="@:", protected="/+")
|
| 11 |
+
QUERY_QUOTER = _Quoter(safe="?/:@", protected="=+&;", qs=True, requote=False)
|
| 12 |
+
QUERY_REQUOTER = _Quoter(safe="?/:@", protected="=+&;", qs=True)
|
| 13 |
+
QUERY_PART_QUOTER = _Quoter(safe="?/:@", qs=True, requote=False)
|
| 14 |
+
FRAGMENT_QUOTER = _Quoter(safe="?/:@", requote=False)
|
| 15 |
+
FRAGMENT_REQUOTER = _Quoter(safe="?/:@")
|
| 16 |
+
|
| 17 |
+
UNQUOTER = _Unquoter()
|
| 18 |
+
PATH_UNQUOTER = _Unquoter(unsafe="+")
|
| 19 |
+
PATH_SAFE_UNQUOTER = _Unquoter(ignore="/%", unsafe="+")
|
| 20 |
+
QS_UNQUOTER = _Unquoter(qs=True)
|
| 21 |
+
UNQUOTER_PLUS = _Unquoter(plus=True) # to match urllib.parse.unquote_plus
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def human_quote(s: str | None, unsafe: str) -> str | None:
|
| 25 |
+
if not s:
|
| 26 |
+
return s
|
| 27 |
+
for c in "%" + unsafe:
|
| 28 |
+
if c in s:
|
| 29 |
+
s = s.replace(c, f"%{ord(c):02X}")
|
| 30 |
+
if s.isprintable():
|
| 31 |
+
return s
|
| 32 |
+
return "".join(c if c.isprintable() else quote(c) for c in s)
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoting.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from typing import TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
__all__ = ("_Quoter", "_Unquoter")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
NO_EXTENSIONS = bool(os.environ.get("YARL_NO_EXTENSIONS")) # type: bool
|
| 9 |
+
if sys.implementation.name != "cpython":
|
| 10 |
+
NO_EXTENSIONS = True
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
if TYPE_CHECKING or NO_EXTENSIONS:
|
| 14 |
+
from ._quoting_py import _Quoter, _Unquoter
|
| 15 |
+
else:
|
| 16 |
+
try:
|
| 17 |
+
from ._quoting_c import _Quoter, _Unquoter
|
| 18 |
+
except ImportError: # pragma: no cover
|
| 19 |
+
from ._quoting_py import _Quoter, _Unquoter # type: ignore[assignment]
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoting_c.cp312-win_amd64.pyd
ADDED
|
Binary file (83.5 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoting_c.pyx
ADDED
|
@@ -0,0 +1,451 @@
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from cpython.exc cimport PyErr_NoMemory
|
| 2 |
+
from cpython.mem cimport PyMem_Free, PyMem_Malloc, PyMem_Realloc
|
| 3 |
+
from cpython.unicode cimport (
|
| 4 |
+
PyUnicode_DATA,
|
| 5 |
+
PyUnicode_DecodeASCII,
|
| 6 |
+
PyUnicode_DecodeUTF8Stateful,
|
| 7 |
+
PyUnicode_GET_LENGTH,
|
| 8 |
+
PyUnicode_KIND,
|
| 9 |
+
PyUnicode_READ,
|
| 10 |
+
)
|
| 11 |
+
from libc.stdint cimport uint8_t, uint64_t
|
| 12 |
+
from libc.string cimport memcpy, memset
|
| 13 |
+
|
| 14 |
+
from string import ascii_letters, digits
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
cdef str GEN_DELIMS = ":/?#[]@"
|
| 18 |
+
cdef str SUB_DELIMS_WITHOUT_QS = "!$'()*,"
|
| 19 |
+
cdef str SUB_DELIMS = SUB_DELIMS_WITHOUT_QS + '+?=;'
|
| 20 |
+
cdef str RESERVED = GEN_DELIMS + SUB_DELIMS
|
| 21 |
+
cdef str UNRESERVED = ascii_letters + digits + '-._~'
|
| 22 |
+
cdef str ALLOWED = UNRESERVED + SUB_DELIMS_WITHOUT_QS
|
| 23 |
+
cdef str QS = '+&=;'
|
| 24 |
+
|
| 25 |
+
DEF BUF_SIZE = 8 * 1024 # 8KiB
|
| 26 |
+
|
| 27 |
+
cdef inline Py_UCS4 _to_hex(uint8_t v) noexcept:
|
| 28 |
+
if v < 10:
|
| 29 |
+
return <Py_UCS4>(v+0x30) # ord('0') == 0x30
|
| 30 |
+
else:
|
| 31 |
+
return <Py_UCS4>(v+0x41-10) # ord('A') == 0x41
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
cdef inline int _from_hex(Py_UCS4 v) noexcept:
|
| 35 |
+
if '0' <= v <= '9':
|
| 36 |
+
return <int>(v) - 0x30 # ord('0') == 0x30
|
| 37 |
+
elif 'A' <= v <= 'F':
|
| 38 |
+
return <int>(v) - 0x41 + 10 # ord('A') == 0x41
|
| 39 |
+
elif 'a' <= v <= 'f':
|
| 40 |
+
return <int>(v) - 0x61 + 10 # ord('a') == 0x61
|
| 41 |
+
else:
|
| 42 |
+
return -1
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
cdef inline int _is_lower_hex(Py_UCS4 v) noexcept:
|
| 46 |
+
return 'a' <= v <= 'f'
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
cdef inline long _restore_ch(Py_UCS4 d1, Py_UCS4 d2):
|
| 50 |
+
cdef int digit1 = _from_hex(d1)
|
| 51 |
+
if digit1 < 0:
|
| 52 |
+
return -1
|
| 53 |
+
cdef int digit2 = _from_hex(d2)
|
| 54 |
+
if digit2 < 0:
|
| 55 |
+
return -1
|
| 56 |
+
return digit1 << 4 | digit2
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
cdef uint8_t ALLOWED_TABLE[16]
|
| 60 |
+
cdef uint8_t ALLOWED_NOTQS_TABLE[16]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
cdef inline bint bit_at(uint8_t array[], uint64_t ch) noexcept:
|
| 64 |
+
return array[ch >> 3] & (1 << (ch & 7))
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
cdef inline void set_bit(uint8_t array[], uint64_t ch) noexcept:
|
| 68 |
+
array[ch >> 3] |= (1 << (ch & 7))
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
memset(ALLOWED_TABLE, 0, sizeof(ALLOWED_TABLE))
|
| 72 |
+
memset(ALLOWED_NOTQS_TABLE, 0, sizeof(ALLOWED_NOTQS_TABLE))
|
| 73 |
+
|
| 74 |
+
for i in range(128):
|
| 75 |
+
if chr(i) in ALLOWED:
|
| 76 |
+
set_bit(ALLOWED_TABLE, i)
|
| 77 |
+
set_bit(ALLOWED_NOTQS_TABLE, i)
|
| 78 |
+
if chr(i) in QS:
|
| 79 |
+
set_bit(ALLOWED_NOTQS_TABLE, i)
|
| 80 |
+
|
| 81 |
+
# ----------------- writer ---------------------------
|
| 82 |
+
|
| 83 |
+
cdef struct Writer:
|
| 84 |
+
char *buf
|
| 85 |
+
bint heap_allocated_buf
|
| 86 |
+
Py_ssize_t size
|
| 87 |
+
Py_ssize_t pos
|
| 88 |
+
bint changed
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
cdef inline void _init_writer(Writer* writer, char* buf):
|
| 92 |
+
writer.buf = buf
|
| 93 |
+
writer.heap_allocated_buf = False
|
| 94 |
+
writer.size = BUF_SIZE
|
| 95 |
+
writer.pos = 0
|
| 96 |
+
writer.changed = 0
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
cdef inline void _release_writer(Writer* writer):
|
| 100 |
+
if writer.heap_allocated_buf:
|
| 101 |
+
PyMem_Free(writer.buf)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
cdef inline int _write_char(Writer* writer, Py_UCS4 ch, bint changed):
|
| 105 |
+
cdef char * buf
|
| 106 |
+
cdef Py_ssize_t size
|
| 107 |
+
|
| 108 |
+
if writer.pos == writer.size:
|
| 109 |
+
# reallocate
|
| 110 |
+
size = writer.size + BUF_SIZE
|
| 111 |
+
if not writer.heap_allocated_buf:
|
| 112 |
+
buf = <char*>PyMem_Malloc(size)
|
| 113 |
+
if buf == NULL:
|
| 114 |
+
PyErr_NoMemory()
|
| 115 |
+
return -1
|
| 116 |
+
memcpy(buf, writer.buf, writer.size)
|
| 117 |
+
writer.heap_allocated_buf = True
|
| 118 |
+
else:
|
| 119 |
+
buf = <char*>PyMem_Realloc(writer.buf, size)
|
| 120 |
+
if buf == NULL:
|
| 121 |
+
PyErr_NoMemory()
|
| 122 |
+
return -1
|
| 123 |
+
writer.buf = buf
|
| 124 |
+
writer.size = size
|
| 125 |
+
writer.buf[writer.pos] = <char>ch
|
| 126 |
+
writer.pos += 1
|
| 127 |
+
writer.changed |= changed
|
| 128 |
+
return 0
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
cdef inline int _write_pct(Writer* writer, uint8_t ch, bint changed):
|
| 132 |
+
if _write_char(writer, '%', changed) < 0:
|
| 133 |
+
return -1
|
| 134 |
+
if _write_char(writer, _to_hex(<uint8_t>ch >> 4), changed) < 0:
|
| 135 |
+
return -1
|
| 136 |
+
return _write_char(writer, _to_hex(<uint8_t>ch & 0x0f), changed)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
cdef inline int _write_utf8(Writer* writer, Py_UCS4 symbol):
|
| 140 |
+
cdef uint64_t utf = <uint64_t> symbol
|
| 141 |
+
|
| 142 |
+
if utf < 0x80:
|
| 143 |
+
return _write_pct(writer, <uint8_t>utf, True)
|
| 144 |
+
elif utf < 0x800:
|
| 145 |
+
if _write_pct(writer, <uint8_t>(0xc0 | (utf >> 6)), True) < 0:
|
| 146 |
+
return -1
|
| 147 |
+
return _write_pct(writer, <uint8_t>(0x80 | (utf & 0x3f)), True)
|
| 148 |
+
elif 0xD800 <= utf <= 0xDFFF:
|
| 149 |
+
# surogate pair, ignored
|
| 150 |
+
return 0
|
| 151 |
+
elif utf < 0x10000:
|
| 152 |
+
if _write_pct(writer, <uint8_t>(0xe0 | (utf >> 12)), True) < 0:
|
| 153 |
+
return -1
|
| 154 |
+
if _write_pct(writer, <uint8_t>(0x80 | ((utf >> 6) & 0x3f)),
|
| 155 |
+
True) < 0:
|
| 156 |
+
return -1
|
| 157 |
+
return _write_pct(writer, <uint8_t>(0x80 | (utf & 0x3f)), True)
|
| 158 |
+
elif utf > 0x10FFFF:
|
| 159 |
+
# symbol is too large
|
| 160 |
+
return 0
|
| 161 |
+
else:
|
| 162 |
+
if _write_pct(writer, <uint8_t>(0xf0 | (utf >> 18)), True) < 0:
|
| 163 |
+
return -1
|
| 164 |
+
if _write_pct(writer, <uint8_t>(0x80 | ((utf >> 12) & 0x3f)),
|
| 165 |
+
True) < 0:
|
| 166 |
+
return -1
|
| 167 |
+
if _write_pct(writer, <uint8_t>(0x80 | ((utf >> 6) & 0x3f)),
|
| 168 |
+
True) < 0:
|
| 169 |
+
return -1
|
| 170 |
+
return _write_pct(writer, <uint8_t>(0x80 | (utf & 0x3f)), True)
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
# --------------------- end writer --------------------------
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
cdef class _Quoter:
|
| 177 |
+
cdef bint _qs
|
| 178 |
+
cdef bint _requote
|
| 179 |
+
|
| 180 |
+
cdef uint8_t _safe_table[16]
|
| 181 |
+
cdef uint8_t _protected_table[16]
|
| 182 |
+
|
| 183 |
+
def __init__(
|
| 184 |
+
self, *, str safe='', str protected='', bint qs=False, bint requote=True,
|
| 185 |
+
):
|
| 186 |
+
cdef Py_UCS4 ch
|
| 187 |
+
|
| 188 |
+
self._qs = qs
|
| 189 |
+
self._requote = requote
|
| 190 |
+
|
| 191 |
+
if not self._qs:
|
| 192 |
+
memcpy(self._safe_table,
|
| 193 |
+
ALLOWED_NOTQS_TABLE,
|
| 194 |
+
sizeof(self._safe_table))
|
| 195 |
+
else:
|
| 196 |
+
memcpy(self._safe_table,
|
| 197 |
+
ALLOWED_TABLE,
|
| 198 |
+
sizeof(self._safe_table))
|
| 199 |
+
for ch in safe:
|
| 200 |
+
if ord(ch) > 127:
|
| 201 |
+
raise ValueError("Only safe symbols with ORD < 128 are allowed")
|
| 202 |
+
set_bit(self._safe_table, ch)
|
| 203 |
+
|
| 204 |
+
memset(self._protected_table, 0, sizeof(self._protected_table))
|
| 205 |
+
for ch in protected:
|
| 206 |
+
if ord(ch) > 127:
|
| 207 |
+
raise ValueError("Only safe symbols with ORD < 128 are allowed")
|
| 208 |
+
set_bit(self._safe_table, ch)
|
| 209 |
+
set_bit(self._protected_table, ch)
|
| 210 |
+
|
| 211 |
+
def __call__(self, val):
|
| 212 |
+
if val is None:
|
| 213 |
+
return None
|
| 214 |
+
if type(val) is not str:
|
| 215 |
+
if isinstance(val, str):
|
| 216 |
+
# derived from str
|
| 217 |
+
val = str(val)
|
| 218 |
+
else:
|
| 219 |
+
raise TypeError("Argument should be str")
|
| 220 |
+
return self._do_quote_or_skip(<str>val)
|
| 221 |
+
|
| 222 |
+
cdef str _do_quote_or_skip(self, str val):
|
| 223 |
+
cdef char[BUF_SIZE] buffer
|
| 224 |
+
cdef Py_UCS4 ch
|
| 225 |
+
cdef Py_ssize_t length = PyUnicode_GET_LENGTH(val)
|
| 226 |
+
cdef Py_ssize_t idx = length
|
| 227 |
+
cdef bint must_quote = 0
|
| 228 |
+
cdef Writer writer
|
| 229 |
+
cdef int kind = PyUnicode_KIND(val)
|
| 230 |
+
cdef const void *data = PyUnicode_DATA(val)
|
| 231 |
+
|
| 232 |
+
# If everything in the string is in the safe
|
| 233 |
+
# table and all ASCII, we can skip quoting
|
| 234 |
+
while idx:
|
| 235 |
+
idx -= 1
|
| 236 |
+
ch = PyUnicode_READ(kind, data, idx)
|
| 237 |
+
if ch >= 128 or not bit_at(self._safe_table, ch):
|
| 238 |
+
must_quote = 1
|
| 239 |
+
break
|
| 240 |
+
|
| 241 |
+
if not must_quote:
|
| 242 |
+
return val
|
| 243 |
+
|
| 244 |
+
_init_writer(&writer, &buffer[0])
|
| 245 |
+
try:
|
| 246 |
+
return self._do_quote(<str>val, length, kind, data, &writer)
|
| 247 |
+
finally:
|
| 248 |
+
_release_writer(&writer)
|
| 249 |
+
|
| 250 |
+
cdef str _do_quote(
|
| 251 |
+
self,
|
| 252 |
+
str val,
|
| 253 |
+
Py_ssize_t length,
|
| 254 |
+
int kind,
|
| 255 |
+
const void *data,
|
| 256 |
+
Writer *writer
|
| 257 |
+
):
|
| 258 |
+
cdef Py_UCS4 ch
|
| 259 |
+
cdef long chl
|
| 260 |
+
cdef int changed
|
| 261 |
+
cdef Py_ssize_t idx = 0
|
| 262 |
+
|
| 263 |
+
while idx < length:
|
| 264 |
+
ch = PyUnicode_READ(kind, data, idx)
|
| 265 |
+
idx += 1
|
| 266 |
+
if ch == '%' and self._requote and idx <= length - 2:
|
| 267 |
+
chl = _restore_ch(
|
| 268 |
+
PyUnicode_READ(kind, data, idx),
|
| 269 |
+
PyUnicode_READ(kind, data, idx + 1)
|
| 270 |
+
)
|
| 271 |
+
if chl != -1:
|
| 272 |
+
ch = <Py_UCS4>chl
|
| 273 |
+
idx += 2
|
| 274 |
+
if ch < 128:
|
| 275 |
+
if bit_at(self._protected_table, ch):
|
| 276 |
+
if _write_pct(writer, ch, True) < 0:
|
| 277 |
+
raise
|
| 278 |
+
continue
|
| 279 |
+
|
| 280 |
+
if bit_at(self._safe_table, ch):
|
| 281 |
+
if _write_char(writer, ch, True) < 0:
|
| 282 |
+
raise
|
| 283 |
+
continue
|
| 284 |
+
|
| 285 |
+
changed = (_is_lower_hex(PyUnicode_READ(kind, data, idx - 2)) or
|
| 286 |
+
_is_lower_hex(PyUnicode_READ(kind, data, idx - 1)))
|
| 287 |
+
if _write_pct(writer, ch, changed) < 0:
|
| 288 |
+
raise
|
| 289 |
+
continue
|
| 290 |
+
else:
|
| 291 |
+
ch = '%'
|
| 292 |
+
|
| 293 |
+
if self._write(writer, ch) < 0:
|
| 294 |
+
raise
|
| 295 |
+
|
| 296 |
+
if not writer.changed:
|
| 297 |
+
return val
|
| 298 |
+
else:
|
| 299 |
+
return PyUnicode_DecodeASCII(writer.buf, writer.pos, "strict")
|
| 300 |
+
|
| 301 |
+
cdef inline int _write(self, Writer *writer, Py_UCS4 ch):
|
| 302 |
+
if self._qs:
|
| 303 |
+
if ch == ' ':
|
| 304 |
+
return _write_char(writer, '+', True)
|
| 305 |
+
|
| 306 |
+
if ch < 128 and bit_at(self._safe_table, ch):
|
| 307 |
+
return _write_char(writer, ch, False)
|
| 308 |
+
|
| 309 |
+
return _write_utf8(writer, ch)
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
cdef class _Unquoter:
|
| 313 |
+
cdef str _ignore
|
| 314 |
+
cdef bint _has_ignore
|
| 315 |
+
cdef str _unsafe
|
| 316 |
+
cdef bytes _unsafe_bytes
|
| 317 |
+
cdef Py_ssize_t _unsafe_bytes_len
|
| 318 |
+
cdef const unsigned char * _unsafe_bytes_char
|
| 319 |
+
cdef bint _qs
|
| 320 |
+
cdef bint _plus # to match urllib.parse.unquote_plus
|
| 321 |
+
cdef _Quoter _quoter
|
| 322 |
+
cdef _Quoter _qs_quoter
|
| 323 |
+
|
| 324 |
+
def __init__(self, *, ignore="", unsafe="", qs=False, plus=False):
|
| 325 |
+
self._ignore = ignore
|
| 326 |
+
self._has_ignore = bool(self._ignore)
|
| 327 |
+
self._unsafe = unsafe
|
| 328 |
+
# unsafe may only be extended ascii characters (0-255)
|
| 329 |
+
self._unsafe_bytes = self._unsafe.encode('ascii')
|
| 330 |
+
self._unsafe_bytes_len = len(self._unsafe_bytes)
|
| 331 |
+
self._unsafe_bytes_char = self._unsafe_bytes
|
| 332 |
+
self._qs = qs
|
| 333 |
+
self._plus = plus
|
| 334 |
+
self._quoter = _Quoter()
|
| 335 |
+
self._qs_quoter = _Quoter(qs=True)
|
| 336 |
+
|
| 337 |
+
def __call__(self, val):
|
| 338 |
+
if val is None:
|
| 339 |
+
return None
|
| 340 |
+
if type(val) is not str:
|
| 341 |
+
if isinstance(val, str):
|
| 342 |
+
# derived from str
|
| 343 |
+
val = str(val)
|
| 344 |
+
else:
|
| 345 |
+
raise TypeError("Argument should be str")
|
| 346 |
+
return self._do_unquote(<str>val)
|
| 347 |
+
|
| 348 |
+
cdef str _do_unquote(self, str val):
|
| 349 |
+
cdef Py_ssize_t length = PyUnicode_GET_LENGTH(val)
|
| 350 |
+
if length == 0:
|
| 351 |
+
return val
|
| 352 |
+
|
| 353 |
+
cdef list ret = []
|
| 354 |
+
cdef char buffer[4]
|
| 355 |
+
cdef Py_ssize_t buflen = 0
|
| 356 |
+
cdef Py_ssize_t consumed
|
| 357 |
+
cdef str unquoted
|
| 358 |
+
cdef Py_UCS4 ch = 0
|
| 359 |
+
cdef long chl = 0
|
| 360 |
+
cdef Py_ssize_t idx = 0
|
| 361 |
+
cdef Py_ssize_t start_pct
|
| 362 |
+
cdef int kind = PyUnicode_KIND(val)
|
| 363 |
+
cdef const void *data = PyUnicode_DATA(val)
|
| 364 |
+
cdef bint changed = 0
|
| 365 |
+
while idx < length:
|
| 366 |
+
ch = PyUnicode_READ(kind, data, idx)
|
| 367 |
+
idx += 1
|
| 368 |
+
if ch == '%' and idx <= length - 2:
|
| 369 |
+
changed = 1
|
| 370 |
+
chl = _restore_ch(
|
| 371 |
+
PyUnicode_READ(kind, data, idx),
|
| 372 |
+
PyUnicode_READ(kind, data, idx + 1)
|
| 373 |
+
)
|
| 374 |
+
if chl != -1:
|
| 375 |
+
ch = <Py_UCS4>chl
|
| 376 |
+
idx += 2
|
| 377 |
+
assert buflen < 4
|
| 378 |
+
buffer[buflen] = ch
|
| 379 |
+
buflen += 1
|
| 380 |
+
try:
|
| 381 |
+
unquoted = PyUnicode_DecodeUTF8Stateful(buffer, buflen,
|
| 382 |
+
NULL, &consumed)
|
| 383 |
+
except UnicodeDecodeError:
|
| 384 |
+
start_pct = idx - buflen * 3
|
| 385 |
+
buffer[0] = ch
|
| 386 |
+
buflen = 1
|
| 387 |
+
ret.append(val[start_pct : idx - 3])
|
| 388 |
+
try:
|
| 389 |
+
unquoted = PyUnicode_DecodeUTF8Stateful(buffer, buflen,
|
| 390 |
+
NULL, &consumed)
|
| 391 |
+
except UnicodeDecodeError:
|
| 392 |
+
buflen = 0
|
| 393 |
+
ret.append(val[idx - 3 : idx])
|
| 394 |
+
continue
|
| 395 |
+
if not unquoted:
|
| 396 |
+
assert consumed == 0
|
| 397 |
+
continue
|
| 398 |
+
assert consumed == buflen
|
| 399 |
+
buflen = 0
|
| 400 |
+
if self._qs and unquoted in '+=&;':
|
| 401 |
+
ret.append(self._qs_quoter(unquoted))
|
| 402 |
+
elif (
|
| 403 |
+
(self._unsafe_bytes_len and unquoted in self._unsafe) or
|
| 404 |
+
(self._has_ignore and unquoted in self._ignore)
|
| 405 |
+
):
|
| 406 |
+
ret.append(self._quoter(unquoted))
|
| 407 |
+
else:
|
| 408 |
+
ret.append(unquoted)
|
| 409 |
+
continue
|
| 410 |
+
else:
|
| 411 |
+
ch = '%'
|
| 412 |
+
|
| 413 |
+
if buflen:
|
| 414 |
+
start_pct = idx - 1 - buflen * 3
|
| 415 |
+
ret.append(val[start_pct : idx - 1])
|
| 416 |
+
buflen = 0
|
| 417 |
+
|
| 418 |
+
if ch == '+':
|
| 419 |
+
if (
|
| 420 |
+
(not self._qs and not self._plus) or
|
| 421 |
+
(self._unsafe_bytes_len and self._is_char_unsafe(ch))
|
| 422 |
+
):
|
| 423 |
+
ret.append('+')
|
| 424 |
+
else:
|
| 425 |
+
changed = 1
|
| 426 |
+
ret.append(' ')
|
| 427 |
+
continue
|
| 428 |
+
|
| 429 |
+
if self._unsafe_bytes_len and self._is_char_unsafe(ch):
|
| 430 |
+
changed = 1
|
| 431 |
+
ret.append('%')
|
| 432 |
+
h = hex(ord(ch)).upper()[2:]
|
| 433 |
+
for ch in h:
|
| 434 |
+
ret.append(ch)
|
| 435 |
+
continue
|
| 436 |
+
|
| 437 |
+
ret.append(ch)
|
| 438 |
+
|
| 439 |
+
if not changed:
|
| 440 |
+
return val
|
| 441 |
+
|
| 442 |
+
if buflen:
|
| 443 |
+
ret.append(val[length - buflen * 3 : length])
|
| 444 |
+
|
| 445 |
+
return ''.join(ret)
|
| 446 |
+
|
| 447 |
+
cdef inline bint _is_char_unsafe(self, Py_UCS4 ch):
|
| 448 |
+
for i in range(self._unsafe_bytes_len):
|
| 449 |
+
if ch == self._unsafe_bytes_char[i]:
|
| 450 |
+
return True
|
| 451 |
+
return False
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_quoting_py.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import codecs
|
| 2 |
+
import re
|
| 3 |
+
from string import ascii_letters, ascii_lowercase, digits
|
| 4 |
+
from typing import overload
|
| 5 |
+
|
| 6 |
+
BASCII_LOWERCASE = ascii_lowercase.encode("ascii")
|
| 7 |
+
BPCT_ALLOWED = {f"%{i:02X}".encode("ascii") for i in range(256)}
|
| 8 |
+
GEN_DELIMS = ":/?#[]@"
|
| 9 |
+
SUB_DELIMS_WITHOUT_QS = "!$'()*,"
|
| 10 |
+
SUB_DELIMS = SUB_DELIMS_WITHOUT_QS + "+&=;"
|
| 11 |
+
RESERVED = GEN_DELIMS + SUB_DELIMS
|
| 12 |
+
UNRESERVED = ascii_letters + digits + "-._~"
|
| 13 |
+
ALLOWED = UNRESERVED + SUB_DELIMS_WITHOUT_QS
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
_IS_HEX = re.compile(b"[A-Z0-9][A-Z0-9]")
|
| 17 |
+
_IS_HEX_STR = re.compile("[A-Fa-f0-9][A-Fa-f0-9]")
|
| 18 |
+
|
| 19 |
+
utf8_decoder = codecs.getincrementaldecoder("utf-8")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class _Quoter:
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
*,
|
| 26 |
+
safe: str = "",
|
| 27 |
+
protected: str = "",
|
| 28 |
+
qs: bool = False,
|
| 29 |
+
requote: bool = True,
|
| 30 |
+
) -> None:
|
| 31 |
+
self._safe = safe
|
| 32 |
+
self._protected = protected
|
| 33 |
+
self._qs = qs
|
| 34 |
+
self._requote = requote
|
| 35 |
+
|
| 36 |
+
@overload
|
| 37 |
+
def __call__(self, val: str) -> str: ...
|
| 38 |
+
@overload
|
| 39 |
+
def __call__(self, val: None) -> None: ...
|
| 40 |
+
def __call__(self, val: str | None) -> str | None:
|
| 41 |
+
if val is None:
|
| 42 |
+
return None
|
| 43 |
+
if not isinstance(val, str):
|
| 44 |
+
raise TypeError("Argument should be str")
|
| 45 |
+
if not val:
|
| 46 |
+
return ""
|
| 47 |
+
bval = val.encode("utf8", errors="ignore")
|
| 48 |
+
ret = bytearray()
|
| 49 |
+
pct = bytearray()
|
| 50 |
+
safe = self._safe
|
| 51 |
+
safe += ALLOWED
|
| 52 |
+
if not self._qs:
|
| 53 |
+
safe += "+&=;"
|
| 54 |
+
safe += self._protected
|
| 55 |
+
bsafe = safe.encode("ascii")
|
| 56 |
+
idx = 0
|
| 57 |
+
while idx < len(bval):
|
| 58 |
+
ch = bval[idx]
|
| 59 |
+
idx += 1
|
| 60 |
+
|
| 61 |
+
if pct:
|
| 62 |
+
if ch in BASCII_LOWERCASE:
|
| 63 |
+
ch = ch - 32 # convert to uppercase
|
| 64 |
+
pct.append(ch)
|
| 65 |
+
if len(pct) == 3: # pragma: no branch # peephole optimizer
|
| 66 |
+
buf = pct[1:]
|
| 67 |
+
if not _IS_HEX.match(buf):
|
| 68 |
+
ret.extend(b"%25")
|
| 69 |
+
pct.clear()
|
| 70 |
+
idx -= 2
|
| 71 |
+
continue
|
| 72 |
+
try:
|
| 73 |
+
unquoted = chr(int(pct[1:].decode("ascii"), base=16))
|
| 74 |
+
except ValueError:
|
| 75 |
+
ret.extend(b"%25")
|
| 76 |
+
pct.clear()
|
| 77 |
+
idx -= 2
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
if unquoted in self._protected:
|
| 81 |
+
ret.extend(pct)
|
| 82 |
+
elif unquoted in safe:
|
| 83 |
+
ret.append(ord(unquoted))
|
| 84 |
+
else:
|
| 85 |
+
ret.extend(pct)
|
| 86 |
+
pct.clear()
|
| 87 |
+
|
| 88 |
+
# special case, if we have only one char after "%"
|
| 89 |
+
elif len(pct) == 2 and idx == len(bval):
|
| 90 |
+
ret.extend(b"%25")
|
| 91 |
+
pct.clear()
|
| 92 |
+
idx -= 1
|
| 93 |
+
|
| 94 |
+
continue
|
| 95 |
+
|
| 96 |
+
elif ch == ord("%") and self._requote:
|
| 97 |
+
pct.clear()
|
| 98 |
+
pct.append(ch)
|
| 99 |
+
|
| 100 |
+
# special case if "%" is last char
|
| 101 |
+
if idx == len(bval):
|
| 102 |
+
ret.extend(b"%25")
|
| 103 |
+
|
| 104 |
+
continue
|
| 105 |
+
|
| 106 |
+
if self._qs and ch == ord(" "):
|
| 107 |
+
ret.append(ord("+"))
|
| 108 |
+
continue
|
| 109 |
+
if ch in bsafe:
|
| 110 |
+
ret.append(ch)
|
| 111 |
+
continue
|
| 112 |
+
|
| 113 |
+
ret.extend((f"%{ch:02X}").encode("ascii"))
|
| 114 |
+
|
| 115 |
+
ret2 = ret.decode("ascii")
|
| 116 |
+
if ret2 == val:
|
| 117 |
+
return val
|
| 118 |
+
return ret2
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
class _Unquoter:
|
| 122 |
+
def __init__(
|
| 123 |
+
self,
|
| 124 |
+
*,
|
| 125 |
+
ignore: str = "",
|
| 126 |
+
unsafe: str = "",
|
| 127 |
+
qs: bool = False,
|
| 128 |
+
plus: bool = False,
|
| 129 |
+
) -> None:
|
| 130 |
+
self._ignore = ignore
|
| 131 |
+
self._unsafe = unsafe
|
| 132 |
+
self._qs = qs
|
| 133 |
+
self._plus = plus # to match urllib.parse.unquote_plus
|
| 134 |
+
self._quoter = _Quoter()
|
| 135 |
+
self._qs_quoter = _Quoter(qs=True)
|
| 136 |
+
|
| 137 |
+
@overload
|
| 138 |
+
def __call__(self, val: str) -> str: ...
|
| 139 |
+
@overload
|
| 140 |
+
def __call__(self, val: None) -> None: ...
|
| 141 |
+
def __call__(self, val: str | None) -> str | None:
|
| 142 |
+
if val is None:
|
| 143 |
+
return None
|
| 144 |
+
if not isinstance(val, str):
|
| 145 |
+
raise TypeError("Argument should be str")
|
| 146 |
+
if not val:
|
| 147 |
+
return ""
|
| 148 |
+
decoder = utf8_decoder()
|
| 149 |
+
ret = []
|
| 150 |
+
idx = 0
|
| 151 |
+
while idx < len(val):
|
| 152 |
+
ch = val[idx]
|
| 153 |
+
idx += 1
|
| 154 |
+
if ch == "%" and idx <= len(val) - 2:
|
| 155 |
+
pct = val[idx : idx + 2]
|
| 156 |
+
if _IS_HEX_STR.fullmatch(pct):
|
| 157 |
+
b = bytes([int(pct, base=16)])
|
| 158 |
+
idx += 2
|
| 159 |
+
try:
|
| 160 |
+
unquoted = decoder.decode(b)
|
| 161 |
+
except UnicodeDecodeError:
|
| 162 |
+
start_pct = idx - 3 - len(decoder.buffer) * 3
|
| 163 |
+
ret.append(val[start_pct : idx - 3])
|
| 164 |
+
decoder.reset()
|
| 165 |
+
try:
|
| 166 |
+
unquoted = decoder.decode(b)
|
| 167 |
+
except UnicodeDecodeError:
|
| 168 |
+
ret.append(val[idx - 3 : idx])
|
| 169 |
+
continue
|
| 170 |
+
if not unquoted:
|
| 171 |
+
continue
|
| 172 |
+
if self._qs and unquoted in "+=&;":
|
| 173 |
+
to_add = self._qs_quoter(unquoted)
|
| 174 |
+
if to_add is None: # pragma: no cover
|
| 175 |
+
raise RuntimeError("Cannot quote None")
|
| 176 |
+
ret.append(to_add)
|
| 177 |
+
elif unquoted in self._unsafe or unquoted in self._ignore:
|
| 178 |
+
to_add = self._quoter(unquoted)
|
| 179 |
+
if to_add is None: # pragma: no cover
|
| 180 |
+
raise RuntimeError("Cannot quote None")
|
| 181 |
+
ret.append(to_add)
|
| 182 |
+
else:
|
| 183 |
+
ret.append(unquoted)
|
| 184 |
+
continue
|
| 185 |
+
|
| 186 |
+
if decoder.buffer:
|
| 187 |
+
start_pct = idx - 1 - len(decoder.buffer) * 3
|
| 188 |
+
ret.append(val[start_pct : idx - 1])
|
| 189 |
+
decoder.reset()
|
| 190 |
+
|
| 191 |
+
if ch == "+":
|
| 192 |
+
if (not self._qs and not self._plus) or ch in self._unsafe:
|
| 193 |
+
ret.append("+")
|
| 194 |
+
else:
|
| 195 |
+
ret.append(" ")
|
| 196 |
+
continue
|
| 197 |
+
|
| 198 |
+
if ch in self._unsafe:
|
| 199 |
+
ret.append("%")
|
| 200 |
+
h = hex(ord(ch)).upper()[2:]
|
| 201 |
+
for ch in h:
|
| 202 |
+
ret.append(ch)
|
| 203 |
+
continue
|
| 204 |
+
|
| 205 |
+
ret.append(ch)
|
| 206 |
+
|
| 207 |
+
if decoder.buffer:
|
| 208 |
+
ret.append(val[-len(decoder.buffer) * 3 :])
|
| 209 |
+
|
| 210 |
+
ret2 = "".join(ret)
|
| 211 |
+
if ret2 == val:
|
| 212 |
+
return val
|
| 213 |
+
return ret2
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/_url.py
ADDED
|
@@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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import sys
|
| 3 |
+
import warnings
|
| 4 |
+
from collections.abc import Mapping, Sequence
|
| 5 |
+
from enum import Enum
|
| 6 |
+
from functools import _CacheInfo, lru_cache
|
| 7 |
+
from ipaddress import ip_address
|
| 8 |
+
from typing import (
|
| 9 |
+
TYPE_CHECKING,
|
| 10 |
+
Any,
|
| 11 |
+
NoReturn,
|
| 12 |
+
TypedDict,
|
| 13 |
+
TypeVar,
|
| 14 |
+
Union,
|
| 15 |
+
cast,
|
| 16 |
+
overload,
|
| 17 |
+
)
|
| 18 |
+
from urllib.parse import SplitResult, uses_relative
|
| 19 |
+
|
| 20 |
+
import idna
|
| 21 |
+
from multidict import MultiDict, MultiDictProxy, istr
|
| 22 |
+
from propcache.api import under_cached_property as cached_property
|
| 23 |
+
|
| 24 |
+
from ._parse import (
|
| 25 |
+
USES_AUTHORITY,
|
| 26 |
+
SplitURLType,
|
| 27 |
+
make_netloc,
|
| 28 |
+
query_to_pairs,
|
| 29 |
+
split_netloc,
|
| 30 |
+
split_url,
|
| 31 |
+
unsplit_result,
|
| 32 |
+
)
|
| 33 |
+
from ._path import normalize_path, normalize_path_segments
|
| 34 |
+
from ._query import (
|
| 35 |
+
Query,
|
| 36 |
+
QueryVariable,
|
| 37 |
+
SimpleQuery,
|
| 38 |
+
get_str_query,
|
| 39 |
+
get_str_query_from_iterable,
|
| 40 |
+
get_str_query_from_sequence_iterable,
|
| 41 |
+
)
|
| 42 |
+
from ._quoters import (
|
| 43 |
+
FRAGMENT_QUOTER,
|
| 44 |
+
FRAGMENT_REQUOTER,
|
| 45 |
+
PATH_QUOTER,
|
| 46 |
+
PATH_REQUOTER,
|
| 47 |
+
PATH_SAFE_UNQUOTER,
|
| 48 |
+
PATH_UNQUOTER,
|
| 49 |
+
QS_UNQUOTER,
|
| 50 |
+
QUERY_QUOTER,
|
| 51 |
+
QUERY_REQUOTER,
|
| 52 |
+
QUOTER,
|
| 53 |
+
REQUOTER,
|
| 54 |
+
UNQUOTER,
|
| 55 |
+
human_quote,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler
|
| 60 |
+
from pydantic.json_schema import JsonSchemaValue
|
| 61 |
+
from pydantic_core import core_schema
|
| 62 |
+
|
| 63 |
+
HAS_PYDANTIC = True
|
| 64 |
+
except ImportError:
|
| 65 |
+
HAS_PYDANTIC = False
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
DEFAULT_PORTS = {"http": 80, "https": 443, "ws": 80, "wss": 443, "ftp": 21}
|
| 69 |
+
USES_RELATIVE = frozenset(uses_relative)
|
| 70 |
+
|
| 71 |
+
# Special schemes https://url.spec.whatwg.org/#special-scheme
|
| 72 |
+
# are not allowed to have an empty host https://url.spec.whatwg.org/#url-representation
|
| 73 |
+
SCHEME_REQUIRES_HOST = frozenset(("http", "https", "ws", "wss", "ftp"))
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# reg-name: unreserved / pct-encoded / sub-delims
|
| 77 |
+
# this pattern matches anything that is *not* in those classes. and is only used
|
| 78 |
+
# on lower-cased ASCII values.
|
| 79 |
+
NOT_REG_NAME = re.compile(
|
| 80 |
+
r"""
|
| 81 |
+
# any character not in the unreserved or sub-delims sets, plus %
|
| 82 |
+
# (validated with the additional check for pct-encoded sequences below)
|
| 83 |
+
[^a-z0-9\-._~!$&'()*+,;=%]
|
| 84 |
+
|
|
| 85 |
+
# % only allowed if it is part of a pct-encoded
|
| 86 |
+
# sequence of 2 hex digits.
|
| 87 |
+
%(?![0-9a-f]{2})
|
| 88 |
+
""",
|
| 89 |
+
re.VERBOSE,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
_T = TypeVar("_T")
|
| 93 |
+
|
| 94 |
+
if sys.version_info >= (3, 11):
|
| 95 |
+
from typing import Self
|
| 96 |
+
else:
|
| 97 |
+
Self = Any
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class UndefinedType(Enum):
|
| 101 |
+
"""Singleton type for use with not set sentinel values."""
|
| 102 |
+
|
| 103 |
+
_singleton = 0
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
UNDEFINED = UndefinedType._singleton
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class CacheInfo(TypedDict):
|
| 110 |
+
"""Host encoding cache."""
|
| 111 |
+
|
| 112 |
+
idna_encode: _CacheInfo
|
| 113 |
+
idna_decode: _CacheInfo
|
| 114 |
+
ip_address: _CacheInfo
|
| 115 |
+
host_validate: _CacheInfo
|
| 116 |
+
encode_host: _CacheInfo
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
class _InternalURLCache(TypedDict, total=False):
|
| 120 |
+
_val: SplitURLType
|
| 121 |
+
_origin: "URL"
|
| 122 |
+
absolute: bool
|
| 123 |
+
hash: int
|
| 124 |
+
scheme: str
|
| 125 |
+
raw_authority: str
|
| 126 |
+
authority: str
|
| 127 |
+
raw_user: str | None
|
| 128 |
+
user: str | None
|
| 129 |
+
raw_password: str | None
|
| 130 |
+
password: str | None
|
| 131 |
+
raw_host: str | None
|
| 132 |
+
host: str | None
|
| 133 |
+
host_subcomponent: str | None
|
| 134 |
+
host_port_subcomponent: str | None
|
| 135 |
+
port: int | None
|
| 136 |
+
explicit_port: int | None
|
| 137 |
+
raw_path: str
|
| 138 |
+
path: str
|
| 139 |
+
_parsed_query: list[tuple[str, str]]
|
| 140 |
+
query: "MultiDictProxy[str]"
|
| 141 |
+
raw_query_string: str
|
| 142 |
+
query_string: str
|
| 143 |
+
path_qs: str
|
| 144 |
+
raw_path_qs: str
|
| 145 |
+
raw_fragment: str
|
| 146 |
+
fragment: str
|
| 147 |
+
raw_parts: tuple[str, ...]
|
| 148 |
+
parts: tuple[str, ...]
|
| 149 |
+
parent: "URL"
|
| 150 |
+
raw_name: str
|
| 151 |
+
name: str
|
| 152 |
+
raw_suffix: str
|
| 153 |
+
suffix: str
|
| 154 |
+
raw_suffixes: tuple[str, ...]
|
| 155 |
+
suffixes: tuple[str, ...]
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def rewrite_module(obj: _T) -> _T:
|
| 159 |
+
obj.__module__ = "yarl"
|
| 160 |
+
return obj
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
@lru_cache
|
| 164 |
+
def encode_url(url_str: str) -> "URL":
|
| 165 |
+
"""Parse unencoded URL."""
|
| 166 |
+
cache: _InternalURLCache = {}
|
| 167 |
+
host: str | None
|
| 168 |
+
scheme, netloc, path, query, fragment = split_url(url_str)
|
| 169 |
+
if not netloc: # netloc
|
| 170 |
+
host = ""
|
| 171 |
+
else:
|
| 172 |
+
if ":" in netloc or "@" in netloc or "[" in netloc:
|
| 173 |
+
# Complex netloc
|
| 174 |
+
username, password, host, port = split_netloc(netloc)
|
| 175 |
+
else:
|
| 176 |
+
username = password = port = None
|
| 177 |
+
host = netloc
|
| 178 |
+
if host is None:
|
| 179 |
+
if scheme in SCHEME_REQUIRES_HOST:
|
| 180 |
+
msg = (
|
| 181 |
+
"Invalid URL: host is required for "
|
| 182 |
+
f"absolute urls with the {scheme} scheme"
|
| 183 |
+
)
|
| 184 |
+
raise ValueError(msg)
|
| 185 |
+
else:
|
| 186 |
+
host = ""
|
| 187 |
+
host = _encode_host(host, validate_host=False)
|
| 188 |
+
# Remove brackets as host encoder adds back brackets for IPv6 addresses
|
| 189 |
+
cache["raw_host"] = host[1:-1] if "[" in host else host
|
| 190 |
+
cache["explicit_port"] = port
|
| 191 |
+
if password is None and username is None:
|
| 192 |
+
# Fast path for URLs without user, password
|
| 193 |
+
netloc = host if port is None else f"{host}:{port}"
|
| 194 |
+
cache["raw_user"] = None
|
| 195 |
+
cache["raw_password"] = None
|
| 196 |
+
else:
|
| 197 |
+
raw_user = REQUOTER(username) if username else username
|
| 198 |
+
raw_password = REQUOTER(password) if password else password
|
| 199 |
+
netloc = make_netloc(raw_user, raw_password, host, port)
|
| 200 |
+
cache["raw_user"] = raw_user
|
| 201 |
+
cache["raw_password"] = raw_password
|
| 202 |
+
|
| 203 |
+
if path:
|
| 204 |
+
path = PATH_REQUOTER(path)
|
| 205 |
+
if netloc and "." in path:
|
| 206 |
+
path = normalize_path(path)
|
| 207 |
+
if query:
|
| 208 |
+
query = QUERY_REQUOTER(query)
|
| 209 |
+
if fragment:
|
| 210 |
+
fragment = FRAGMENT_REQUOTER(fragment)
|
| 211 |
+
|
| 212 |
+
cache["scheme"] = scheme
|
| 213 |
+
cache["raw_path"] = "/" if not path and netloc else path
|
| 214 |
+
cache["raw_query_string"] = query
|
| 215 |
+
cache["raw_fragment"] = fragment
|
| 216 |
+
|
| 217 |
+
self = object.__new__(URL)
|
| 218 |
+
self._scheme = scheme
|
| 219 |
+
self._netloc = netloc
|
| 220 |
+
self._path = path
|
| 221 |
+
self._query = query
|
| 222 |
+
self._fragment = fragment
|
| 223 |
+
self._cache = cache
|
| 224 |
+
return self
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
@lru_cache
|
| 228 |
+
def pre_encoded_url(url_str: str) -> "URL":
|
| 229 |
+
"""Parse pre-encoded URL."""
|
| 230 |
+
self = object.__new__(URL)
|
| 231 |
+
val = split_url(url_str)
|
| 232 |
+
self._scheme, self._netloc, self._path, self._query, self._fragment = val
|
| 233 |
+
self._cache = {}
|
| 234 |
+
return self
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
@lru_cache
|
| 238 |
+
def build_pre_encoded_url(
|
| 239 |
+
scheme: str,
|
| 240 |
+
authority: str,
|
| 241 |
+
user: str | None,
|
| 242 |
+
password: str | None,
|
| 243 |
+
host: str,
|
| 244 |
+
port: int | None,
|
| 245 |
+
path: str,
|
| 246 |
+
query_string: str,
|
| 247 |
+
fragment: str,
|
| 248 |
+
) -> "URL":
|
| 249 |
+
"""Build a pre-encoded URL from parts."""
|
| 250 |
+
self = object.__new__(URL)
|
| 251 |
+
self._scheme = scheme
|
| 252 |
+
if authority:
|
| 253 |
+
self._netloc = authority
|
| 254 |
+
elif host:
|
| 255 |
+
if port is not None:
|
| 256 |
+
port = None if port == DEFAULT_PORTS.get(scheme) else port
|
| 257 |
+
if user is None and password is None:
|
| 258 |
+
self._netloc = host if port is None else f"{host}:{port}"
|
| 259 |
+
else:
|
| 260 |
+
self._netloc = make_netloc(user, password, host, port)
|
| 261 |
+
else:
|
| 262 |
+
self._netloc = ""
|
| 263 |
+
self._path = path
|
| 264 |
+
self._query = query_string
|
| 265 |
+
self._fragment = fragment
|
| 266 |
+
self._cache = {}
|
| 267 |
+
return self
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
def from_parts_uncached(
|
| 271 |
+
scheme: str, netloc: str, path: str, query: str, fragment: str
|
| 272 |
+
) -> "URL":
|
| 273 |
+
"""Create a new URL from parts."""
|
| 274 |
+
self = object.__new__(URL)
|
| 275 |
+
self._scheme = scheme
|
| 276 |
+
self._netloc = netloc
|
| 277 |
+
self._path = path
|
| 278 |
+
self._query = query
|
| 279 |
+
self._fragment = fragment
|
| 280 |
+
self._cache = {}
|
| 281 |
+
return self
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
from_parts = lru_cache(from_parts_uncached)
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
@rewrite_module
|
| 288 |
+
class URL:
|
| 289 |
+
# Don't derive from str
|
| 290 |
+
# follow pathlib.Path design
|
| 291 |
+
# probably URL will not suffer from pathlib problems:
|
| 292 |
+
# it's intended for libraries like aiohttp,
|
| 293 |
+
# not to be passed into standard library functions like os.open etc.
|
| 294 |
+
|
| 295 |
+
# URL grammar (RFC 3986)
|
| 296 |
+
# pct-encoded = "%" HEXDIG HEXDIG
|
| 297 |
+
# reserved = gen-delims / sub-delims
|
| 298 |
+
# gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
|
| 299 |
+
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
| 300 |
+
# / "*" / "+" / "," / ";" / "="
|
| 301 |
+
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
| 302 |
+
# URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
|
| 303 |
+
# hier-part = "//" authority path-abempty
|
| 304 |
+
# / path-absolute
|
| 305 |
+
# / path-rootless
|
| 306 |
+
# / path-empty
|
| 307 |
+
# scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
|
| 308 |
+
# authority = [ userinfo "@" ] host [ ":" port ]
|
| 309 |
+
# userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
|
| 310 |
+
# host = IP-literal / IPv4address / reg-name
|
| 311 |
+
# IP-literal = "[" ( IPv6address / IPvFuture ) "]"
|
| 312 |
+
# IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
|
| 313 |
+
# IPv6address = 6( h16 ":" ) ls32
|
| 314 |
+
# / "::" 5( h16 ":" ) ls32
|
| 315 |
+
# / [ h16 ] "::" 4( h16 ":" ) ls32
|
| 316 |
+
# / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
|
| 317 |
+
# / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
|
| 318 |
+
# / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
|
| 319 |
+
# / [ *4( h16 ":" ) h16 ] "::" ls32
|
| 320 |
+
# / [ *5( h16 ":" ) h16 ] "::" h16
|
| 321 |
+
# / [ *6( h16 ":" ) h16 ] "::"
|
| 322 |
+
# ls32 = ( h16 ":" h16 ) / IPv4address
|
| 323 |
+
# ; least-significant 32 bits of address
|
| 324 |
+
# h16 = 1*4HEXDIG
|
| 325 |
+
# ; 16 bits of address represented in hexadecimal
|
| 326 |
+
# IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
|
| 327 |
+
# dec-octet = DIGIT ; 0-9
|
| 328 |
+
# / %x31-39 DIGIT ; 10-99
|
| 329 |
+
# / "1" 2DIGIT ; 100-199
|
| 330 |
+
# / "2" %x30-34 DIGIT ; 200-249
|
| 331 |
+
# / "25" %x30-35 ; 250-255
|
| 332 |
+
# reg-name = *( unreserved / pct-encoded / sub-delims )
|
| 333 |
+
# port = *DIGIT
|
| 334 |
+
# path = path-abempty ; begins with "/" or is empty
|
| 335 |
+
# / path-absolute ; begins with "/" but not "//"
|
| 336 |
+
# / path-noscheme ; begins with a non-colon segment
|
| 337 |
+
# / path-rootless ; begins with a segment
|
| 338 |
+
# / path-empty ; zero characters
|
| 339 |
+
# path-abempty = *( "/" segment )
|
| 340 |
+
# path-absolute = "/" [ segment-nz *( "/" segment ) ]
|
| 341 |
+
# path-noscheme = segment-nz-nc *( "/" segment )
|
| 342 |
+
# path-rootless = segment-nz *( "/" segment )
|
| 343 |
+
# path-empty = 0<pchar>
|
| 344 |
+
# segment = *pchar
|
| 345 |
+
# segment-nz = 1*pchar
|
| 346 |
+
# segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
|
| 347 |
+
# ; non-zero-length segment without any colon ":"
|
| 348 |
+
# pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
|
| 349 |
+
# query = *( pchar / "/" / "?" )
|
| 350 |
+
# fragment = *( pchar / "/" / "?" )
|
| 351 |
+
# URI-reference = URI / relative-ref
|
| 352 |
+
# relative-ref = relative-part [ "?" query ] [ "#" fragment ]
|
| 353 |
+
# relative-part = "//" authority path-abempty
|
| 354 |
+
# / path-absolute
|
| 355 |
+
# / path-noscheme
|
| 356 |
+
# / path-empty
|
| 357 |
+
# absolute-URI = scheme ":" hier-part [ "?" query ]
|
| 358 |
+
__slots__ = ("_cache", "_scheme", "_netloc", "_path", "_query", "_fragment")
|
| 359 |
+
|
| 360 |
+
_cache: _InternalURLCache
|
| 361 |
+
_scheme: str
|
| 362 |
+
_netloc: str
|
| 363 |
+
_path: str
|
| 364 |
+
_query: str
|
| 365 |
+
_fragment: str
|
| 366 |
+
|
| 367 |
+
def __new__(
|
| 368 |
+
cls,
|
| 369 |
+
val: Union[str, SplitResult, "URL", UndefinedType] = UNDEFINED,
|
| 370 |
+
*,
|
| 371 |
+
encoded: bool = False,
|
| 372 |
+
strict: bool | None = None,
|
| 373 |
+
) -> "URL":
|
| 374 |
+
if strict is not None: # pragma: no cover
|
| 375 |
+
warnings.warn("strict parameter is ignored")
|
| 376 |
+
if type(val) is str:
|
| 377 |
+
return pre_encoded_url(val) if encoded else encode_url(val)
|
| 378 |
+
if type(val) is cls:
|
| 379 |
+
return val
|
| 380 |
+
if type(val) is SplitResult:
|
| 381 |
+
if not encoded:
|
| 382 |
+
raise ValueError("Cannot apply decoding to SplitResult")
|
| 383 |
+
return from_parts(*val)
|
| 384 |
+
if isinstance(val, str):
|
| 385 |
+
return pre_encoded_url(str(val)) if encoded else encode_url(str(val))
|
| 386 |
+
if val is UNDEFINED:
|
| 387 |
+
# Special case for UNDEFINED since it might be unpickling and we do
|
| 388 |
+
# not want to cache as the `__set_state__` call would mutate the URL
|
| 389 |
+
# object in the `pre_encoded_url` or `encoded_url` caches.
|
| 390 |
+
self = object.__new__(URL)
|
| 391 |
+
self._scheme = self._netloc = self._path = self._query = self._fragment = ""
|
| 392 |
+
self._cache = {}
|
| 393 |
+
return self
|
| 394 |
+
raise TypeError("Constructor parameter should be str")
|
| 395 |
+
|
| 396 |
+
@classmethod
|
| 397 |
+
def build(
|
| 398 |
+
cls,
|
| 399 |
+
*,
|
| 400 |
+
scheme: str = "",
|
| 401 |
+
authority: str = "",
|
| 402 |
+
user: str | None = None,
|
| 403 |
+
password: str | None = None,
|
| 404 |
+
host: str = "",
|
| 405 |
+
port: int | None = None,
|
| 406 |
+
path: str = "",
|
| 407 |
+
query: Query | None = None,
|
| 408 |
+
query_string: str = "",
|
| 409 |
+
fragment: str = "",
|
| 410 |
+
encoded: bool = False,
|
| 411 |
+
) -> "URL":
|
| 412 |
+
"""Creates and returns a new URL"""
|
| 413 |
+
|
| 414 |
+
if authority and (user or password or host or port):
|
| 415 |
+
raise ValueError(
|
| 416 |
+
'Can\'t mix "authority" with "user", "password", "host" or "port".'
|
| 417 |
+
)
|
| 418 |
+
if port is not None and not isinstance(port, int):
|
| 419 |
+
raise TypeError(f"The port is required to be int, got {type(port)!r}.")
|
| 420 |
+
if port and not host:
|
| 421 |
+
raise ValueError('Can\'t build URL with "port" but without "host".')
|
| 422 |
+
if query and query_string:
|
| 423 |
+
raise ValueError('Only one of "query" or "query_string" should be passed')
|
| 424 |
+
if (
|
| 425 |
+
scheme is None # type: ignore[redundant-expr]
|
| 426 |
+
or authority is None # type: ignore[redundant-expr]
|
| 427 |
+
or host is None # type: ignore[redundant-expr]
|
| 428 |
+
or path is None # type: ignore[redundant-expr]
|
| 429 |
+
or query_string is None # type: ignore[redundant-expr]
|
| 430 |
+
or fragment is None
|
| 431 |
+
):
|
| 432 |
+
raise TypeError(
|
| 433 |
+
'NoneType is illegal for "scheme", "authority", "host", "path", '
|
| 434 |
+
'"query_string", and "fragment" args, use empty string instead.'
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
if query:
|
| 438 |
+
query_string = get_str_query(query) or ""
|
| 439 |
+
|
| 440 |
+
if encoded:
|
| 441 |
+
return build_pre_encoded_url(
|
| 442 |
+
scheme,
|
| 443 |
+
authority,
|
| 444 |
+
user,
|
| 445 |
+
password,
|
| 446 |
+
host,
|
| 447 |
+
port,
|
| 448 |
+
path,
|
| 449 |
+
query_string,
|
| 450 |
+
fragment,
|
| 451 |
+
)
|
| 452 |
+
|
| 453 |
+
self = object.__new__(URL)
|
| 454 |
+
self._scheme = scheme
|
| 455 |
+
_host: str | None = None
|
| 456 |
+
if authority:
|
| 457 |
+
user, password, _host, port = split_netloc(authority)
|
| 458 |
+
_host = _encode_host(_host, validate_host=False) if _host else ""
|
| 459 |
+
elif host:
|
| 460 |
+
_host = _encode_host(host, validate_host=True)
|
| 461 |
+
else:
|
| 462 |
+
self._netloc = ""
|
| 463 |
+
|
| 464 |
+
if _host is not None:
|
| 465 |
+
if port is not None:
|
| 466 |
+
port = None if port == DEFAULT_PORTS.get(scheme) else port
|
| 467 |
+
if user is None and password is None:
|
| 468 |
+
self._netloc = _host if port is None else f"{_host}:{port}"
|
| 469 |
+
else:
|
| 470 |
+
self._netloc = make_netloc(user, password, _host, port, True)
|
| 471 |
+
|
| 472 |
+
path = PATH_QUOTER(path) if path else path
|
| 473 |
+
if path and self._netloc:
|
| 474 |
+
if "." in path:
|
| 475 |
+
path = normalize_path(path)
|
| 476 |
+
if path[0] != "/":
|
| 477 |
+
msg = (
|
| 478 |
+
"Path in a URL with authority should "
|
| 479 |
+
"start with a slash ('/') if set"
|
| 480 |
+
)
|
| 481 |
+
raise ValueError(msg)
|
| 482 |
+
|
| 483 |
+
self._path = path
|
| 484 |
+
if not query and query_string:
|
| 485 |
+
query_string = QUERY_QUOTER(query_string)
|
| 486 |
+
self._query = query_string
|
| 487 |
+
self._fragment = FRAGMENT_QUOTER(fragment) if fragment else fragment
|
| 488 |
+
self._cache = {}
|
| 489 |
+
return self
|
| 490 |
+
|
| 491 |
+
def __init_subclass__(cls) -> NoReturn:
|
| 492 |
+
raise TypeError(f"Inheriting a class {cls!r} from URL is forbidden")
|
| 493 |
+
|
| 494 |
+
def __str__(self) -> str:
|
| 495 |
+
if not self._path and self._netloc and (self._query or self._fragment):
|
| 496 |
+
path = "/"
|
| 497 |
+
else:
|
| 498 |
+
path = self._path
|
| 499 |
+
if (port := self.explicit_port) is not None and port == DEFAULT_PORTS.get(
|
| 500 |
+
self._scheme
|
| 501 |
+
):
|
| 502 |
+
# port normalization - using None for default ports to remove from rendering
|
| 503 |
+
# https://datatracker.ietf.org/doc/html/rfc3986.html#section-6.2.3
|
| 504 |
+
host = self.host_subcomponent
|
| 505 |
+
netloc = make_netloc(self.raw_user, self.raw_password, host, None)
|
| 506 |
+
else:
|
| 507 |
+
netloc = self._netloc
|
| 508 |
+
return unsplit_result(self._scheme, netloc, path, self._query, self._fragment)
|
| 509 |
+
|
| 510 |
+
def __repr__(self) -> str:
|
| 511 |
+
return f"{self.__class__.__name__}('{str(self)}')"
|
| 512 |
+
|
| 513 |
+
def __bytes__(self) -> bytes:
|
| 514 |
+
return str(self).encode("ascii")
|
| 515 |
+
|
| 516 |
+
def __eq__(self, other: object) -> bool:
|
| 517 |
+
if type(other) is not URL:
|
| 518 |
+
return NotImplemented
|
| 519 |
+
|
| 520 |
+
path1 = "/" if not self._path and self._netloc else self._path
|
| 521 |
+
path2 = "/" if not other._path and other._netloc else other._path
|
| 522 |
+
return (
|
| 523 |
+
self._scheme == other._scheme
|
| 524 |
+
and self._netloc == other._netloc
|
| 525 |
+
and path1 == path2
|
| 526 |
+
and self._query == other._query
|
| 527 |
+
and self._fragment == other._fragment
|
| 528 |
+
)
|
| 529 |
+
|
| 530 |
+
def __hash__(self) -> int:
|
| 531 |
+
if (ret := self._cache.get("hash")) is None:
|
| 532 |
+
path = "/" if not self._path and self._netloc else self._path
|
| 533 |
+
ret = self._cache["hash"] = hash(
|
| 534 |
+
(self._scheme, self._netloc, path, self._query, self._fragment)
|
| 535 |
+
)
|
| 536 |
+
return ret
|
| 537 |
+
|
| 538 |
+
def __le__(self, other: object) -> bool:
|
| 539 |
+
if type(other) is not URL:
|
| 540 |
+
return NotImplemented
|
| 541 |
+
return self._val <= other._val
|
| 542 |
+
|
| 543 |
+
def __lt__(self, other: object) -> bool:
|
| 544 |
+
if type(other) is not URL:
|
| 545 |
+
return NotImplemented
|
| 546 |
+
return self._val < other._val
|
| 547 |
+
|
| 548 |
+
def __ge__(self, other: object) -> bool:
|
| 549 |
+
if type(other) is not URL:
|
| 550 |
+
return NotImplemented
|
| 551 |
+
return self._val >= other._val
|
| 552 |
+
|
| 553 |
+
def __gt__(self, other: object) -> bool:
|
| 554 |
+
if type(other) is not URL:
|
| 555 |
+
return NotImplemented
|
| 556 |
+
return self._val > other._val
|
| 557 |
+
|
| 558 |
+
def __truediv__(self, name: str) -> "URL":
|
| 559 |
+
if not isinstance(name, str):
|
| 560 |
+
return NotImplemented # type: ignore[unreachable]
|
| 561 |
+
return self._make_child((str(name),))
|
| 562 |
+
|
| 563 |
+
def __mod__(self, query: Query) -> "URL":
|
| 564 |
+
return self.update_query(query)
|
| 565 |
+
|
| 566 |
+
def __bool__(self) -> bool:
|
| 567 |
+
return bool(self._netloc or self._path or self._query or self._fragment)
|
| 568 |
+
|
| 569 |
+
def __getstate__(self) -> tuple[SplitResult]:
|
| 570 |
+
return (tuple.__new__(SplitResult, self._val),)
|
| 571 |
+
|
| 572 |
+
def __setstate__(
|
| 573 |
+
self, state: tuple[SplitURLType] | tuple[None, _InternalURLCache]
|
| 574 |
+
) -> None:
|
| 575 |
+
if state[0] is None and isinstance(state[1], dict):
|
| 576 |
+
# default style pickle
|
| 577 |
+
val = state[1]["_val"]
|
| 578 |
+
else:
|
| 579 |
+
unused: list[object]
|
| 580 |
+
val, *unused = state
|
| 581 |
+
self._scheme, self._netloc, self._path, self._query, self._fragment = val
|
| 582 |
+
self._cache = {}
|
| 583 |
+
|
| 584 |
+
def _cache_netloc(self) -> None:
|
| 585 |
+
"""Cache the netloc parts of the URL."""
|
| 586 |
+
c = self._cache
|
| 587 |
+
split_loc = split_netloc(self._netloc)
|
| 588 |
+
c["raw_user"], c["raw_password"], c["raw_host"], c["explicit_port"] = split_loc
|
| 589 |
+
|
| 590 |
+
def is_absolute(self) -> bool:
|
| 591 |
+
"""A check for absolute URLs.
|
| 592 |
+
|
| 593 |
+
Return True for absolute ones (having scheme or starting
|
| 594 |
+
with //), False otherwise.
|
| 595 |
+
|
| 596 |
+
Is is preferred to call the .absolute property instead
|
| 597 |
+
as it is cached.
|
| 598 |
+
"""
|
| 599 |
+
return self.absolute
|
| 600 |
+
|
| 601 |
+
def is_default_port(self) -> bool:
|
| 602 |
+
"""A check for default port.
|
| 603 |
+
|
| 604 |
+
Return True if port is default for specified scheme,
|
| 605 |
+
e.g. 'http://python.org' or 'http://python.org:80', False
|
| 606 |
+
otherwise.
|
| 607 |
+
|
| 608 |
+
Return False for relative URLs.
|
| 609 |
+
|
| 610 |
+
"""
|
| 611 |
+
if (explicit := self.explicit_port) is None:
|
| 612 |
+
# If the explicit port is None, then the URL must be
|
| 613 |
+
# using the default port unless its a relative URL
|
| 614 |
+
# which does not have an implicit port / default port
|
| 615 |
+
return self._netloc != ""
|
| 616 |
+
return explicit == DEFAULT_PORTS.get(self._scheme)
|
| 617 |
+
|
| 618 |
+
def origin(self) -> "URL":
|
| 619 |
+
"""Return an URL with scheme, host and port parts only.
|
| 620 |
+
|
| 621 |
+
user, password, path, query and fragment are removed.
|
| 622 |
+
|
| 623 |
+
"""
|
| 624 |
+
# TODO: add a keyword-only option for keeping user/pass maybe?
|
| 625 |
+
return self._origin
|
| 626 |
+
|
| 627 |
+
@cached_property
|
| 628 |
+
def _val(self) -> SplitURLType:
|
| 629 |
+
return (self._scheme, self._netloc, self._path, self._query, self._fragment)
|
| 630 |
+
|
| 631 |
+
@cached_property
|
| 632 |
+
def _origin(self) -> "URL":
|
| 633 |
+
"""Return an URL with scheme, host and port parts only.
|
| 634 |
+
|
| 635 |
+
user, password, path, query and fragment are removed.
|
| 636 |
+
"""
|
| 637 |
+
if not (netloc := self._netloc):
|
| 638 |
+
raise ValueError("URL should be absolute")
|
| 639 |
+
if not (scheme := self._scheme):
|
| 640 |
+
raise ValueError("URL should have scheme")
|
| 641 |
+
if "@" in netloc:
|
| 642 |
+
encoded_host = self.host_subcomponent
|
| 643 |
+
netloc = make_netloc(None, None, encoded_host, self.explicit_port)
|
| 644 |
+
elif not self._path and not self._query and not self._fragment:
|
| 645 |
+
return self
|
| 646 |
+
return from_parts(scheme, netloc, "", "", "")
|
| 647 |
+
|
| 648 |
+
def relative(self) -> "URL":
|
| 649 |
+
"""Return a relative part of the URL.
|
| 650 |
+
|
| 651 |
+
scheme, user, password, host and port are removed.
|
| 652 |
+
|
| 653 |
+
"""
|
| 654 |
+
if not self._netloc:
|
| 655 |
+
raise ValueError("URL should be absolute")
|
| 656 |
+
return from_parts("", "", self._path, self._query, self._fragment)
|
| 657 |
+
|
| 658 |
+
@cached_property
|
| 659 |
+
def absolute(self) -> bool:
|
| 660 |
+
"""A check for absolute URLs.
|
| 661 |
+
|
| 662 |
+
Return True for absolute ones (having scheme or starting
|
| 663 |
+
with //), False otherwise.
|
| 664 |
+
|
| 665 |
+
"""
|
| 666 |
+
# `netloc`` is an empty string for relative URLs
|
| 667 |
+
# Checking `netloc` is faster than checking `hostname`
|
| 668 |
+
# because `hostname` is a property that does some extra work
|
| 669 |
+
# to parse the host from the `netloc`
|
| 670 |
+
return self._netloc != ""
|
| 671 |
+
|
| 672 |
+
@cached_property
|
| 673 |
+
def scheme(self) -> str:
|
| 674 |
+
"""Scheme for absolute URLs.
|
| 675 |
+
|
| 676 |
+
Empty string for relative URLs or URLs starting with //
|
| 677 |
+
|
| 678 |
+
"""
|
| 679 |
+
return self._scheme
|
| 680 |
+
|
| 681 |
+
@cached_property
|
| 682 |
+
def raw_authority(self) -> str:
|
| 683 |
+
"""Encoded authority part of URL.
|
| 684 |
+
|
| 685 |
+
Empty string for relative URLs.
|
| 686 |
+
|
| 687 |
+
"""
|
| 688 |
+
return self._netloc
|
| 689 |
+
|
| 690 |
+
@cached_property
|
| 691 |
+
def authority(self) -> str:
|
| 692 |
+
"""Decoded authority part of URL.
|
| 693 |
+
|
| 694 |
+
Empty string for relative URLs.
|
| 695 |
+
|
| 696 |
+
"""
|
| 697 |
+
return make_netloc(self.user, self.password, self.host, self.port)
|
| 698 |
+
|
| 699 |
+
@cached_property
|
| 700 |
+
def raw_user(self) -> str | None:
|
| 701 |
+
"""Encoded user part of URL.
|
| 702 |
+
|
| 703 |
+
None if user is missing.
|
| 704 |
+
|
| 705 |
+
"""
|
| 706 |
+
# not .username
|
| 707 |
+
self._cache_netloc()
|
| 708 |
+
return self._cache["raw_user"]
|
| 709 |
+
|
| 710 |
+
@cached_property
|
| 711 |
+
def user(self) -> str | None:
|
| 712 |
+
"""Decoded user part of URL.
|
| 713 |
+
|
| 714 |
+
None if user is missing.
|
| 715 |
+
|
| 716 |
+
"""
|
| 717 |
+
if (raw_user := self.raw_user) is None:
|
| 718 |
+
return None
|
| 719 |
+
return UNQUOTER(raw_user)
|
| 720 |
+
|
| 721 |
+
@cached_property
|
| 722 |
+
def raw_password(self) -> str | None:
|
| 723 |
+
"""Encoded password part of URL.
|
| 724 |
+
|
| 725 |
+
None if password is missing.
|
| 726 |
+
|
| 727 |
+
"""
|
| 728 |
+
self._cache_netloc()
|
| 729 |
+
return self._cache["raw_password"]
|
| 730 |
+
|
| 731 |
+
@cached_property
|
| 732 |
+
def password(self) -> str | None:
|
| 733 |
+
"""Decoded password part of URL.
|
| 734 |
+
|
| 735 |
+
None if password is missing.
|
| 736 |
+
|
| 737 |
+
"""
|
| 738 |
+
if (raw_password := self.raw_password) is None:
|
| 739 |
+
return None
|
| 740 |
+
return UNQUOTER(raw_password)
|
| 741 |
+
|
| 742 |
+
@cached_property
|
| 743 |
+
def raw_host(self) -> str | None:
|
| 744 |
+
"""Encoded host part of URL.
|
| 745 |
+
|
| 746 |
+
None for relative URLs.
|
| 747 |
+
|
| 748 |
+
When working with IPv6 addresses, use the `host_subcomponent` property instead
|
| 749 |
+
as it will return the host subcomponent with brackets.
|
| 750 |
+
"""
|
| 751 |
+
# Use host instead of hostname for sake of shortness
|
| 752 |
+
# May add .hostname prop later
|
| 753 |
+
self._cache_netloc()
|
| 754 |
+
return self._cache["raw_host"]
|
| 755 |
+
|
| 756 |
+
@cached_property
|
| 757 |
+
def host(self) -> str | None:
|
| 758 |
+
"""Decoded host part of URL.
|
| 759 |
+
|
| 760 |
+
None for relative URLs.
|
| 761 |
+
|
| 762 |
+
"""
|
| 763 |
+
if (raw := self.raw_host) is None:
|
| 764 |
+
return None
|
| 765 |
+
if raw and raw[-1].isdigit() or ":" in raw:
|
| 766 |
+
# IP addresses are never IDNA encoded
|
| 767 |
+
return raw
|
| 768 |
+
return _idna_decode(raw)
|
| 769 |
+
|
| 770 |
+
@cached_property
|
| 771 |
+
def host_subcomponent(self) -> str | None:
|
| 772 |
+
"""Return the host subcomponent part of URL.
|
| 773 |
+
|
| 774 |
+
None for relative URLs.
|
| 775 |
+
|
| 776 |
+
https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
|
| 777 |
+
|
| 778 |
+
`IP-literal = "[" ( IPv6address / IPvFuture ) "]"`
|
| 779 |
+
|
| 780 |
+
Examples:
|
| 781 |
+
- `http://example.com:8080` -> `example.com`
|
| 782 |
+
- `http://example.com:80` -> `example.com`
|
| 783 |
+
- `https://127.0.0.1:8443` -> `127.0.0.1`
|
| 784 |
+
- `https://[::1]:8443` -> `[::1]`
|
| 785 |
+
- `http://[::1]` -> `[::1]`
|
| 786 |
+
|
| 787 |
+
"""
|
| 788 |
+
if (raw := self.raw_host) is None:
|
| 789 |
+
return None
|
| 790 |
+
return f"[{raw}]" if ":" in raw else raw
|
| 791 |
+
|
| 792 |
+
@cached_property
|
| 793 |
+
def host_port_subcomponent(self) -> str | None:
|
| 794 |
+
"""Return the host and port subcomponent part of URL.
|
| 795 |
+
|
| 796 |
+
Trailing dots are removed from the host part.
|
| 797 |
+
|
| 798 |
+
This value is suitable for use in the Host header of an HTTP request.
|
| 799 |
+
|
| 800 |
+
None for relative URLs.
|
| 801 |
+
|
| 802 |
+
https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
|
| 803 |
+
`IP-literal = "[" ( IPv6address / IPvFuture ) "]"`
|
| 804 |
+
https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3
|
| 805 |
+
port = *DIGIT
|
| 806 |
+
|
| 807 |
+
Examples:
|
| 808 |
+
- `http://example.com:8080` -> `example.com:8080`
|
| 809 |
+
- `http://example.com:80` -> `example.com`
|
| 810 |
+
- `http://example.com.:80` -> `example.com`
|
| 811 |
+
- `https://127.0.0.1:8443` -> `127.0.0.1:8443`
|
| 812 |
+
- `https://[::1]:8443` -> `[::1]:8443`
|
| 813 |
+
- `http://[::1]` -> `[::1]`
|
| 814 |
+
|
| 815 |
+
"""
|
| 816 |
+
if (raw := self.raw_host) is None:
|
| 817 |
+
return None
|
| 818 |
+
if raw[-1] == ".":
|
| 819 |
+
# Remove all trailing dots from the netloc as while
|
| 820 |
+
# they are valid FQDNs in DNS, TLS validation fails.
|
| 821 |
+
# See https://github.com/aio-libs/aiohttp/issues/3636.
|
| 822 |
+
# To avoid string manipulation we only call rstrip if
|
| 823 |
+
# the last character is a dot.
|
| 824 |
+
raw = raw.rstrip(".")
|
| 825 |
+
port = self.explicit_port
|
| 826 |
+
if port is None or port == DEFAULT_PORTS.get(self._scheme):
|
| 827 |
+
return f"[{raw}]" if ":" in raw else raw
|
| 828 |
+
return f"[{raw}]:{port}" if ":" in raw else f"{raw}:{port}"
|
| 829 |
+
|
| 830 |
+
@cached_property
|
| 831 |
+
def port(self) -> int | None:
|
| 832 |
+
"""Port part of URL, with scheme-based fallback.
|
| 833 |
+
|
| 834 |
+
None for relative URLs or URLs without explicit port and
|
| 835 |
+
scheme without default port substitution.
|
| 836 |
+
|
| 837 |
+
"""
|
| 838 |
+
if (explicit_port := self.explicit_port) is not None:
|
| 839 |
+
return explicit_port
|
| 840 |
+
return DEFAULT_PORTS.get(self._scheme)
|
| 841 |
+
|
| 842 |
+
@cached_property
|
| 843 |
+
def explicit_port(self) -> int | None:
|
| 844 |
+
"""Port part of URL, without scheme-based fallback.
|
| 845 |
+
|
| 846 |
+
None for relative URLs or URLs without explicit port.
|
| 847 |
+
|
| 848 |
+
"""
|
| 849 |
+
self._cache_netloc()
|
| 850 |
+
return self._cache["explicit_port"]
|
| 851 |
+
|
| 852 |
+
@cached_property
|
| 853 |
+
def raw_path(self) -> str:
|
| 854 |
+
"""Encoded path of URL.
|
| 855 |
+
|
| 856 |
+
/ for absolute URLs without path part.
|
| 857 |
+
|
| 858 |
+
"""
|
| 859 |
+
return self._path if self._path or not self._netloc else "/"
|
| 860 |
+
|
| 861 |
+
@cached_property
|
| 862 |
+
def path(self) -> str:
|
| 863 |
+
"""Decoded path of URL.
|
| 864 |
+
|
| 865 |
+
/ for absolute URLs without path part.
|
| 866 |
+
|
| 867 |
+
"""
|
| 868 |
+
return PATH_UNQUOTER(self._path) if self._path else "/" if self._netloc else ""
|
| 869 |
+
|
| 870 |
+
@cached_property
|
| 871 |
+
def path_safe(self) -> str:
|
| 872 |
+
"""Decoded path of URL.
|
| 873 |
+
|
| 874 |
+
/ for absolute URLs without path part.
|
| 875 |
+
|
| 876 |
+
/ (%2F) and % (%25) are not decoded
|
| 877 |
+
|
| 878 |
+
"""
|
| 879 |
+
if self._path:
|
| 880 |
+
return PATH_SAFE_UNQUOTER(self._path)
|
| 881 |
+
return "/" if self._netloc else ""
|
| 882 |
+
|
| 883 |
+
@cached_property
|
| 884 |
+
def _parsed_query(self) -> list[tuple[str, str]]:
|
| 885 |
+
"""Parse query part of URL."""
|
| 886 |
+
return query_to_pairs(self._query)
|
| 887 |
+
|
| 888 |
+
@cached_property
|
| 889 |
+
def query(self) -> "MultiDictProxy[str]":
|
| 890 |
+
"""A MultiDictProxy representing parsed query parameters in decoded
|
| 891 |
+
representation.
|
| 892 |
+
|
| 893 |
+
Empty value if URL has no query part.
|
| 894 |
+
|
| 895 |
+
"""
|
| 896 |
+
return MultiDictProxy(MultiDict(self._parsed_query))
|
| 897 |
+
|
| 898 |
+
@cached_property
|
| 899 |
+
def raw_query_string(self) -> str:
|
| 900 |
+
"""Encoded query part of URL.
|
| 901 |
+
|
| 902 |
+
Empty string if query is missing.
|
| 903 |
+
|
| 904 |
+
"""
|
| 905 |
+
return self._query
|
| 906 |
+
|
| 907 |
+
@cached_property
|
| 908 |
+
def query_string(self) -> str:
|
| 909 |
+
"""Decoded query part of URL.
|
| 910 |
+
|
| 911 |
+
Empty string if query is missing.
|
| 912 |
+
|
| 913 |
+
"""
|
| 914 |
+
return QS_UNQUOTER(self._query) if self._query else ""
|
| 915 |
+
|
| 916 |
+
@cached_property
|
| 917 |
+
def path_qs(self) -> str:
|
| 918 |
+
"""Decoded path of URL with query."""
|
| 919 |
+
return self.path if not (q := self.query_string) else f"{self.path}?{q}"
|
| 920 |
+
|
| 921 |
+
@cached_property
|
| 922 |
+
def raw_path_qs(self) -> str:
|
| 923 |
+
"""Encoded path of URL with query."""
|
| 924 |
+
if q := self._query:
|
| 925 |
+
return f"{self._path}?{q}" if self._path or not self._netloc else f"/?{q}"
|
| 926 |
+
return self._path if self._path or not self._netloc else "/"
|
| 927 |
+
|
| 928 |
+
@cached_property
|
| 929 |
+
def raw_fragment(self) -> str:
|
| 930 |
+
"""Encoded fragment part of URL.
|
| 931 |
+
|
| 932 |
+
Empty string if fragment is missing.
|
| 933 |
+
|
| 934 |
+
"""
|
| 935 |
+
return self._fragment
|
| 936 |
+
|
| 937 |
+
@cached_property
|
| 938 |
+
def fragment(self) -> str:
|
| 939 |
+
"""Decoded fragment part of URL.
|
| 940 |
+
|
| 941 |
+
Empty string if fragment is missing.
|
| 942 |
+
|
| 943 |
+
"""
|
| 944 |
+
return UNQUOTER(self._fragment) if self._fragment else ""
|
| 945 |
+
|
| 946 |
+
@cached_property
|
| 947 |
+
def raw_parts(self) -> tuple[str, ...]:
|
| 948 |
+
"""A tuple containing encoded *path* parts.
|
| 949 |
+
|
| 950 |
+
('/',) for absolute URLs if *path* is missing.
|
| 951 |
+
|
| 952 |
+
"""
|
| 953 |
+
path = self._path
|
| 954 |
+
if self._netloc:
|
| 955 |
+
return ("/", *path[1:].split("/")) if path else ("/",)
|
| 956 |
+
if path and path[0] == "/":
|
| 957 |
+
return ("/", *path[1:].split("/"))
|
| 958 |
+
return tuple(path.split("/"))
|
| 959 |
+
|
| 960 |
+
@cached_property
|
| 961 |
+
def parts(self) -> tuple[str, ...]:
|
| 962 |
+
"""A tuple containing decoded *path* parts.
|
| 963 |
+
|
| 964 |
+
('/',) for absolute URLs if *path* is missing.
|
| 965 |
+
|
| 966 |
+
"""
|
| 967 |
+
return tuple(UNQUOTER(part) for part in self.raw_parts)
|
| 968 |
+
|
| 969 |
+
@cached_property
|
| 970 |
+
def parent(self) -> "URL":
|
| 971 |
+
"""A new URL with last part of path removed and cleaned up query and
|
| 972 |
+
fragment.
|
| 973 |
+
|
| 974 |
+
"""
|
| 975 |
+
path = self._path
|
| 976 |
+
if not path or path == "/":
|
| 977 |
+
if self._fragment or self._query:
|
| 978 |
+
return from_parts(self._scheme, self._netloc, path, "", "")
|
| 979 |
+
return self
|
| 980 |
+
parts = path.split("/")
|
| 981 |
+
return from_parts(self._scheme, self._netloc, "/".join(parts[:-1]), "", "")
|
| 982 |
+
|
| 983 |
+
@cached_property
|
| 984 |
+
def raw_name(self) -> str:
|
| 985 |
+
"""The last part of raw_parts."""
|
| 986 |
+
parts = self.raw_parts
|
| 987 |
+
if not self._netloc:
|
| 988 |
+
return parts[-1]
|
| 989 |
+
parts = parts[1:]
|
| 990 |
+
return parts[-1] if parts else ""
|
| 991 |
+
|
| 992 |
+
@cached_property
|
| 993 |
+
def name(self) -> str:
|
| 994 |
+
"""The last part of parts."""
|
| 995 |
+
return UNQUOTER(self.raw_name)
|
| 996 |
+
|
| 997 |
+
@cached_property
|
| 998 |
+
def raw_suffix(self) -> str:
|
| 999 |
+
name = self.raw_name
|
| 1000 |
+
i = name.rfind(".")
|
| 1001 |
+
return name[i:] if 0 < i < len(name) - 1 else ""
|
| 1002 |
+
|
| 1003 |
+
@cached_property
|
| 1004 |
+
def suffix(self) -> str:
|
| 1005 |
+
return UNQUOTER(self.raw_suffix)
|
| 1006 |
+
|
| 1007 |
+
@cached_property
|
| 1008 |
+
def raw_suffixes(self) -> tuple[str, ...]:
|
| 1009 |
+
name = self.raw_name
|
| 1010 |
+
if name.endswith("."):
|
| 1011 |
+
return ()
|
| 1012 |
+
name = name.lstrip(".")
|
| 1013 |
+
return tuple("." + suffix for suffix in name.split(".")[1:])
|
| 1014 |
+
|
| 1015 |
+
@cached_property
|
| 1016 |
+
def suffixes(self) -> tuple[str, ...]:
|
| 1017 |
+
return tuple(UNQUOTER(suffix) for suffix in self.raw_suffixes)
|
| 1018 |
+
|
| 1019 |
+
def _make_child(self, paths: "Sequence[str]", encoded: bool = False) -> "URL":
|
| 1020 |
+
"""
|
| 1021 |
+
add paths to self._path, accounting for absolute vs relative paths,
|
| 1022 |
+
keep existing, but do not create new, empty segments
|
| 1023 |
+
"""
|
| 1024 |
+
parsed: list[str] = []
|
| 1025 |
+
needs_normalize: bool = False
|
| 1026 |
+
for idx, path in enumerate(reversed(paths)):
|
| 1027 |
+
# empty segment of last is not removed
|
| 1028 |
+
last = idx == 0
|
| 1029 |
+
if path and path[0] == "/":
|
| 1030 |
+
raise ValueError(
|
| 1031 |
+
f"Appending path {path!r} starting from slash is forbidden"
|
| 1032 |
+
)
|
| 1033 |
+
# We need to quote the path if it is not already encoded
|
| 1034 |
+
# This cannot be done at the end because the existing
|
| 1035 |
+
# path is already quoted and we do not want to double quote
|
| 1036 |
+
# the existing path.
|
| 1037 |
+
path = path if encoded else PATH_QUOTER(path)
|
| 1038 |
+
needs_normalize |= "." in path
|
| 1039 |
+
segments = path.split("/")
|
| 1040 |
+
segments.reverse()
|
| 1041 |
+
# remove trailing empty segment for all but the last path
|
| 1042 |
+
parsed += segments[1:] if not last and segments[0] == "" else segments
|
| 1043 |
+
|
| 1044 |
+
if (path := self._path) and (old_segments := path.split("/")):
|
| 1045 |
+
# If the old path ends with a slash, the last segment is an empty string
|
| 1046 |
+
# and should be removed before adding the new path segments.
|
| 1047 |
+
old = old_segments[:-1] if old_segments[-1] == "" else old_segments
|
| 1048 |
+
old.reverse()
|
| 1049 |
+
parsed += old
|
| 1050 |
+
|
| 1051 |
+
# If the netloc is present, inject a leading slash when adding a
|
| 1052 |
+
# path to an absolute URL where there was none before.
|
| 1053 |
+
if (netloc := self._netloc) and parsed and parsed[-1] != "":
|
| 1054 |
+
parsed.append("")
|
| 1055 |
+
|
| 1056 |
+
parsed.reverse()
|
| 1057 |
+
if not netloc or not needs_normalize:
|
| 1058 |
+
return from_parts(self._scheme, netloc, "/".join(parsed), "", "")
|
| 1059 |
+
|
| 1060 |
+
path = "/".join(normalize_path_segments(parsed))
|
| 1061 |
+
# If normalizing the path segments removed the leading slash, add it back.
|
| 1062 |
+
if path and path[0] != "/":
|
| 1063 |
+
path = f"/{path}"
|
| 1064 |
+
return from_parts(self._scheme, netloc, path, "", "")
|
| 1065 |
+
|
| 1066 |
+
def with_scheme(self, scheme: str) -> "URL":
|
| 1067 |
+
"""Return a new URL with scheme replaced."""
|
| 1068 |
+
# N.B. doesn't cleanup query/fragment
|
| 1069 |
+
if not isinstance(scheme, str):
|
| 1070 |
+
raise TypeError("Invalid scheme type")
|
| 1071 |
+
lower_scheme = scheme.lower()
|
| 1072 |
+
netloc = self._netloc
|
| 1073 |
+
if not netloc and lower_scheme in SCHEME_REQUIRES_HOST:
|
| 1074 |
+
msg = (
|
| 1075 |
+
"scheme replacement is not allowed for "
|
| 1076 |
+
f"relative URLs for the {lower_scheme} scheme"
|
| 1077 |
+
)
|
| 1078 |
+
raise ValueError(msg)
|
| 1079 |
+
return from_parts(lower_scheme, netloc, self._path, self._query, self._fragment)
|
| 1080 |
+
|
| 1081 |
+
def with_user(self, user: str | None) -> "URL":
|
| 1082 |
+
"""Return a new URL with user replaced.
|
| 1083 |
+
|
| 1084 |
+
Autoencode user if needed.
|
| 1085 |
+
|
| 1086 |
+
Clear user/password if user is None.
|
| 1087 |
+
|
| 1088 |
+
"""
|
| 1089 |
+
# N.B. doesn't cleanup query/fragment
|
| 1090 |
+
if user is None:
|
| 1091 |
+
password = None
|
| 1092 |
+
elif isinstance(user, str):
|
| 1093 |
+
user = QUOTER(user)
|
| 1094 |
+
password = self.raw_password
|
| 1095 |
+
else:
|
| 1096 |
+
raise TypeError("Invalid user type")
|
| 1097 |
+
if not (netloc := self._netloc):
|
| 1098 |
+
raise ValueError("user replacement is not allowed for relative URLs")
|
| 1099 |
+
encoded_host = self.host_subcomponent or ""
|
| 1100 |
+
netloc = make_netloc(user, password, encoded_host, self.explicit_port)
|
| 1101 |
+
return from_parts(self._scheme, netloc, self._path, self._query, self._fragment)
|
| 1102 |
+
|
| 1103 |
+
def with_password(self, password: str | None) -> "URL":
|
| 1104 |
+
"""Return a new URL with password replaced.
|
| 1105 |
+
|
| 1106 |
+
Autoencode password if needed.
|
| 1107 |
+
|
| 1108 |
+
Clear password if argument is None.
|
| 1109 |
+
|
| 1110 |
+
"""
|
| 1111 |
+
# N.B. doesn't cleanup query/fragment
|
| 1112 |
+
if password is None:
|
| 1113 |
+
pass
|
| 1114 |
+
elif isinstance(password, str):
|
| 1115 |
+
password = QUOTER(password)
|
| 1116 |
+
else:
|
| 1117 |
+
raise TypeError("Invalid password type")
|
| 1118 |
+
if not (netloc := self._netloc):
|
| 1119 |
+
raise ValueError("password replacement is not allowed for relative URLs")
|
| 1120 |
+
encoded_host = self.host_subcomponent or ""
|
| 1121 |
+
port = self.explicit_port
|
| 1122 |
+
netloc = make_netloc(self.raw_user, password, encoded_host, port)
|
| 1123 |
+
return from_parts(self._scheme, netloc, self._path, self._query, self._fragment)
|
| 1124 |
+
|
| 1125 |
+
def with_host(self, host: str) -> "URL":
|
| 1126 |
+
"""Return a new URL with host replaced.
|
| 1127 |
+
|
| 1128 |
+
Autoencode host if needed.
|
| 1129 |
+
|
| 1130 |
+
Changing host for relative URLs is not allowed, use .join()
|
| 1131 |
+
instead.
|
| 1132 |
+
|
| 1133 |
+
"""
|
| 1134 |
+
# N.B. doesn't cleanup query/fragment
|
| 1135 |
+
if not isinstance(host, str):
|
| 1136 |
+
raise TypeError("Invalid host type")
|
| 1137 |
+
if not (netloc := self._netloc):
|
| 1138 |
+
raise ValueError("host replacement is not allowed for relative URLs")
|
| 1139 |
+
if not host:
|
| 1140 |
+
raise ValueError("host removing is not allowed")
|
| 1141 |
+
encoded_host = _encode_host(host, validate_host=True) if host else ""
|
| 1142 |
+
port = self.explicit_port
|
| 1143 |
+
netloc = make_netloc(self.raw_user, self.raw_password, encoded_host, port)
|
| 1144 |
+
return from_parts(self._scheme, netloc, self._path, self._query, self._fragment)
|
| 1145 |
+
|
| 1146 |
+
def with_port(self, port: int | None) -> "URL":
|
| 1147 |
+
"""Return a new URL with port replaced.
|
| 1148 |
+
|
| 1149 |
+
Clear port to default if None is passed.
|
| 1150 |
+
|
| 1151 |
+
"""
|
| 1152 |
+
# N.B. doesn't cleanup query/fragment
|
| 1153 |
+
if port is not None:
|
| 1154 |
+
if isinstance(port, bool) or not isinstance(port, int):
|
| 1155 |
+
raise TypeError(f"port should be int or None, got {type(port)}")
|
| 1156 |
+
if not (0 <= port <= 65535):
|
| 1157 |
+
raise ValueError(f"port must be between 0 and 65535, got {port}")
|
| 1158 |
+
if not (netloc := self._netloc):
|
| 1159 |
+
raise ValueError("port replacement is not allowed for relative URLs")
|
| 1160 |
+
encoded_host = self.host_subcomponent or ""
|
| 1161 |
+
netloc = make_netloc(self.raw_user, self.raw_password, encoded_host, port)
|
| 1162 |
+
return from_parts(self._scheme, netloc, self._path, self._query, self._fragment)
|
| 1163 |
+
|
| 1164 |
+
def with_path(
|
| 1165 |
+
self,
|
| 1166 |
+
path: str,
|
| 1167 |
+
*,
|
| 1168 |
+
encoded: bool = False,
|
| 1169 |
+
keep_query: bool = False,
|
| 1170 |
+
keep_fragment: bool = False,
|
| 1171 |
+
) -> "URL":
|
| 1172 |
+
"""Return a new URL with path replaced."""
|
| 1173 |
+
netloc = self._netloc
|
| 1174 |
+
if not encoded:
|
| 1175 |
+
path = PATH_QUOTER(path)
|
| 1176 |
+
if netloc:
|
| 1177 |
+
path = normalize_path(path) if "." in path else path
|
| 1178 |
+
if path and path[0] != "/":
|
| 1179 |
+
path = f"/{path}"
|
| 1180 |
+
query = self._query if keep_query else ""
|
| 1181 |
+
fragment = self._fragment if keep_fragment else ""
|
| 1182 |
+
return from_parts(self._scheme, netloc, path, query, fragment)
|
| 1183 |
+
|
| 1184 |
+
@overload
|
| 1185 |
+
def with_query(self, query: Query) -> "URL": ...
|
| 1186 |
+
|
| 1187 |
+
@overload
|
| 1188 |
+
def with_query(self, **kwargs: QueryVariable) -> "URL": ...
|
| 1189 |
+
|
| 1190 |
+
def with_query(self, *args: Any, **kwargs: Any) -> "URL":
|
| 1191 |
+
"""Return a new URL with query part replaced.
|
| 1192 |
+
|
| 1193 |
+
Accepts any Mapping (e.g. dict, multidict.MultiDict instances)
|
| 1194 |
+
or str, autoencode the argument if needed.
|
| 1195 |
+
|
| 1196 |
+
A sequence of (key, value) pairs is supported as well.
|
| 1197 |
+
|
| 1198 |
+
It also can take an arbitrary number of keyword arguments.
|
| 1199 |
+
|
| 1200 |
+
Clear query if None is passed.
|
| 1201 |
+
|
| 1202 |
+
"""
|
| 1203 |
+
# N.B. doesn't cleanup query/fragment
|
| 1204 |
+
query = get_str_query(*args, **kwargs) or ""
|
| 1205 |
+
return from_parts_uncached(
|
| 1206 |
+
self._scheme, self._netloc, self._path, query, self._fragment
|
| 1207 |
+
)
|
| 1208 |
+
|
| 1209 |
+
@overload
|
| 1210 |
+
def extend_query(self, query: Query) -> "URL": ...
|
| 1211 |
+
|
| 1212 |
+
@overload
|
| 1213 |
+
def extend_query(self, **kwargs: QueryVariable) -> "URL": ...
|
| 1214 |
+
|
| 1215 |
+
def extend_query(self, *args: Any, **kwargs: Any) -> "URL":
|
| 1216 |
+
"""Return a new URL with query part combined with the existing.
|
| 1217 |
+
|
| 1218 |
+
This method will not remove existing query parameters.
|
| 1219 |
+
|
| 1220 |
+
Example:
|
| 1221 |
+
>>> url = URL('http://example.com/?a=1&b=2')
|
| 1222 |
+
>>> url.extend_query(a=3, c=4)
|
| 1223 |
+
URL('http://example.com/?a=1&b=2&a=3&c=4')
|
| 1224 |
+
"""
|
| 1225 |
+
if not (new_query := get_str_query(*args, **kwargs)):
|
| 1226 |
+
return self
|
| 1227 |
+
if query := self._query:
|
| 1228 |
+
# both strings are already encoded so we can use a simple
|
| 1229 |
+
# string join
|
| 1230 |
+
query += new_query if query[-1] == "&" else f"&{new_query}"
|
| 1231 |
+
else:
|
| 1232 |
+
query = new_query
|
| 1233 |
+
return from_parts_uncached(
|
| 1234 |
+
self._scheme, self._netloc, self._path, query, self._fragment
|
| 1235 |
+
)
|
| 1236 |
+
|
| 1237 |
+
@overload
|
| 1238 |
+
def update_query(self, query: Query) -> "URL": ...
|
| 1239 |
+
|
| 1240 |
+
@overload
|
| 1241 |
+
def update_query(self, **kwargs: QueryVariable) -> "URL": ...
|
| 1242 |
+
|
| 1243 |
+
def update_query(self, *args: Any, **kwargs: Any) -> "URL":
|
| 1244 |
+
"""Return a new URL with query part updated.
|
| 1245 |
+
|
| 1246 |
+
This method will overwrite existing query parameters.
|
| 1247 |
+
|
| 1248 |
+
Example:
|
| 1249 |
+
>>> url = URL('http://example.com/?a=1&b=2')
|
| 1250 |
+
>>> url.update_query(a=3, c=4)
|
| 1251 |
+
URL('http://example.com/?a=3&b=2&c=4')
|
| 1252 |
+
"""
|
| 1253 |
+
in_query: (
|
| 1254 |
+
str
|
| 1255 |
+
| Mapping[str, QueryVariable]
|
| 1256 |
+
| Sequence[tuple[str | istr, SimpleQuery]]
|
| 1257 |
+
| None
|
| 1258 |
+
)
|
| 1259 |
+
if kwargs:
|
| 1260 |
+
if args:
|
| 1261 |
+
msg = "Either kwargs or single query parameter must be present"
|
| 1262 |
+
raise ValueError(msg)
|
| 1263 |
+
in_query = kwargs
|
| 1264 |
+
elif len(args) == 1:
|
| 1265 |
+
in_query = args[0]
|
| 1266 |
+
else:
|
| 1267 |
+
raise ValueError("Either kwargs or single query parameter must be present")
|
| 1268 |
+
|
| 1269 |
+
if in_query is None:
|
| 1270 |
+
query = ""
|
| 1271 |
+
elif not in_query:
|
| 1272 |
+
query = self._query
|
| 1273 |
+
elif isinstance(in_query, Mapping):
|
| 1274 |
+
qm: MultiDict[QueryVariable] = MultiDict(self._parsed_query)
|
| 1275 |
+
qm.update(in_query)
|
| 1276 |
+
query = get_str_query_from_sequence_iterable(qm.items())
|
| 1277 |
+
elif isinstance(in_query, str):
|
| 1278 |
+
qstr: MultiDict[str] = MultiDict(self._parsed_query)
|
| 1279 |
+
qstr.update(query_to_pairs(in_query))
|
| 1280 |
+
query = get_str_query_from_iterable(qstr.items())
|
| 1281 |
+
elif isinstance(in_query, (bytes, bytearray, memoryview)):
|
| 1282 |
+
msg = "Invalid query type: bytes, bytearray and memoryview are forbidden"
|
| 1283 |
+
raise TypeError(msg)
|
| 1284 |
+
elif isinstance(in_query, Sequence):
|
| 1285 |
+
# We don't expect sequence values if we're given a list of pairs
|
| 1286 |
+
# already; only mappings like builtin `dict` which can't have the
|
| 1287 |
+
# same key pointing to multiple values are allowed to use
|
| 1288 |
+
# `_query_seq_pairs`.
|
| 1289 |
+
if TYPE_CHECKING:
|
| 1290 |
+
in_query = cast(
|
| 1291 |
+
Sequence[tuple[Union[str, istr], SimpleQuery]], in_query
|
| 1292 |
+
)
|
| 1293 |
+
qs: MultiDict[SimpleQuery] = MultiDict(self._parsed_query)
|
| 1294 |
+
qs.update(in_query)
|
| 1295 |
+
query = get_str_query_from_iterable(qs.items())
|
| 1296 |
+
else:
|
| 1297 |
+
raise TypeError(
|
| 1298 |
+
"Invalid query type: only str, mapping or "
|
| 1299 |
+
"sequence of (key, value) pairs is allowed"
|
| 1300 |
+
)
|
| 1301 |
+
return from_parts_uncached(
|
| 1302 |
+
self._scheme, self._netloc, self._path, query, self._fragment
|
| 1303 |
+
)
|
| 1304 |
+
|
| 1305 |
+
def without_query_params(self, *query_params: str) -> "URL":
|
| 1306 |
+
"""Remove some keys from query part and return new URL."""
|
| 1307 |
+
params_to_remove = set(query_params) & self.query.keys()
|
| 1308 |
+
if not params_to_remove:
|
| 1309 |
+
return self
|
| 1310 |
+
return self.with_query(
|
| 1311 |
+
tuple(
|
| 1312 |
+
(name, value)
|
| 1313 |
+
for name, value in self.query.items()
|
| 1314 |
+
if name not in params_to_remove
|
| 1315 |
+
)
|
| 1316 |
+
)
|
| 1317 |
+
|
| 1318 |
+
def with_fragment(self, fragment: str | None) -> "URL":
|
| 1319 |
+
"""Return a new URL with fragment replaced.
|
| 1320 |
+
|
| 1321 |
+
Autoencode fragment if needed.
|
| 1322 |
+
|
| 1323 |
+
Clear fragment to default if None is passed.
|
| 1324 |
+
|
| 1325 |
+
"""
|
| 1326 |
+
# N.B. doesn't cleanup query/fragment
|
| 1327 |
+
if fragment is None:
|
| 1328 |
+
raw_fragment = ""
|
| 1329 |
+
elif not isinstance(fragment, str):
|
| 1330 |
+
raise TypeError("Invalid fragment type")
|
| 1331 |
+
else:
|
| 1332 |
+
raw_fragment = FRAGMENT_QUOTER(fragment)
|
| 1333 |
+
if self._fragment == raw_fragment:
|
| 1334 |
+
return self
|
| 1335 |
+
return from_parts(
|
| 1336 |
+
self._scheme, self._netloc, self._path, self._query, raw_fragment
|
| 1337 |
+
)
|
| 1338 |
+
|
| 1339 |
+
def with_name(
|
| 1340 |
+
self,
|
| 1341 |
+
name: str,
|
| 1342 |
+
*,
|
| 1343 |
+
keep_query: bool = False,
|
| 1344 |
+
keep_fragment: bool = False,
|
| 1345 |
+
) -> "URL":
|
| 1346 |
+
"""Return a new URL with name (last part of path) replaced.
|
| 1347 |
+
|
| 1348 |
+
Query and fragment parts are cleaned up.
|
| 1349 |
+
|
| 1350 |
+
Name is encoded if needed.
|
| 1351 |
+
|
| 1352 |
+
"""
|
| 1353 |
+
# N.B. DOES cleanup query/fragment
|
| 1354 |
+
if not isinstance(name, str):
|
| 1355 |
+
raise TypeError("Invalid name type")
|
| 1356 |
+
if "/" in name:
|
| 1357 |
+
raise ValueError("Slash in name is not allowed")
|
| 1358 |
+
name = PATH_QUOTER(name)
|
| 1359 |
+
if name in (".", ".."):
|
| 1360 |
+
raise ValueError(". and .. values are forbidden")
|
| 1361 |
+
parts = list(self.raw_parts)
|
| 1362 |
+
if netloc := self._netloc:
|
| 1363 |
+
if len(parts) == 1:
|
| 1364 |
+
parts.append(name)
|
| 1365 |
+
else:
|
| 1366 |
+
parts[-1] = name
|
| 1367 |
+
parts[0] = "" # replace leading '/'
|
| 1368 |
+
else:
|
| 1369 |
+
parts[-1] = name
|
| 1370 |
+
if parts[0] == "/":
|
| 1371 |
+
parts[0] = "" # replace leading '/'
|
| 1372 |
+
|
| 1373 |
+
query = self._query if keep_query else ""
|
| 1374 |
+
fragment = self._fragment if keep_fragment else ""
|
| 1375 |
+
return from_parts(self._scheme, netloc, "/".join(parts), query, fragment)
|
| 1376 |
+
|
| 1377 |
+
def with_suffix(
|
| 1378 |
+
self,
|
| 1379 |
+
suffix: str,
|
| 1380 |
+
*,
|
| 1381 |
+
keep_query: bool = False,
|
| 1382 |
+
keep_fragment: bool = False,
|
| 1383 |
+
) -> "URL":
|
| 1384 |
+
"""Return a new URL with suffix (file extension of name) replaced.
|
| 1385 |
+
|
| 1386 |
+
Query and fragment parts are cleaned up.
|
| 1387 |
+
|
| 1388 |
+
suffix is encoded if needed.
|
| 1389 |
+
"""
|
| 1390 |
+
if not isinstance(suffix, str):
|
| 1391 |
+
raise TypeError("Invalid suffix type")
|
| 1392 |
+
if suffix and not suffix[0] == "." or suffix == "." or "/" in suffix:
|
| 1393 |
+
raise ValueError(f"Invalid suffix {suffix!r}")
|
| 1394 |
+
name = self.raw_name
|
| 1395 |
+
if not name:
|
| 1396 |
+
raise ValueError(f"{self!r} has an empty name")
|
| 1397 |
+
old_suffix = self.raw_suffix
|
| 1398 |
+
suffix = PATH_QUOTER(suffix)
|
| 1399 |
+
name = name + suffix if not old_suffix else name[: -len(old_suffix)] + suffix
|
| 1400 |
+
if name in (".", ".."):
|
| 1401 |
+
raise ValueError(". and .. values are forbidden")
|
| 1402 |
+
parts = list(self.raw_parts)
|
| 1403 |
+
if netloc := self._netloc:
|
| 1404 |
+
if len(parts) == 1:
|
| 1405 |
+
parts.append(name)
|
| 1406 |
+
else:
|
| 1407 |
+
parts[-1] = name
|
| 1408 |
+
parts[0] = "" # replace leading '/'
|
| 1409 |
+
else:
|
| 1410 |
+
parts[-1] = name
|
| 1411 |
+
if parts[0] == "/":
|
| 1412 |
+
parts[0] = "" # replace leading '/'
|
| 1413 |
+
|
| 1414 |
+
query = self._query if keep_query else ""
|
| 1415 |
+
fragment = self._fragment if keep_fragment else ""
|
| 1416 |
+
return from_parts(self._scheme, netloc, "/".join(parts), query, fragment)
|
| 1417 |
+
|
| 1418 |
+
def join(self, url: "URL") -> "URL":
|
| 1419 |
+
"""Join URLs
|
| 1420 |
+
|
| 1421 |
+
Construct a full (“absolute”) URL by combining a “base URL”
|
| 1422 |
+
(self) with another URL (url).
|
| 1423 |
+
|
| 1424 |
+
Informally, this uses components of the base URL, in
|
| 1425 |
+
particular the addressing scheme, the network location and
|
| 1426 |
+
(part of) the path, to provide missing components in the
|
| 1427 |
+
relative URL.
|
| 1428 |
+
|
| 1429 |
+
"""
|
| 1430 |
+
if type(url) is not URL:
|
| 1431 |
+
raise TypeError("url should be URL")
|
| 1432 |
+
|
| 1433 |
+
scheme = url._scheme or self._scheme
|
| 1434 |
+
if scheme != self._scheme or scheme not in USES_RELATIVE:
|
| 1435 |
+
return url
|
| 1436 |
+
|
| 1437 |
+
# scheme is in uses_authority as uses_authority is a superset of uses_relative
|
| 1438 |
+
if (join_netloc := url._netloc) and scheme in USES_AUTHORITY:
|
| 1439 |
+
return from_parts(scheme, join_netloc, url._path, url._query, url._fragment)
|
| 1440 |
+
|
| 1441 |
+
orig_path = self._path
|
| 1442 |
+
if join_path := url._path:
|
| 1443 |
+
if join_path[0] == "/":
|
| 1444 |
+
path = join_path
|
| 1445 |
+
elif not orig_path:
|
| 1446 |
+
path = f"/{join_path}"
|
| 1447 |
+
elif orig_path[-1] == "/":
|
| 1448 |
+
path = f"{orig_path}{join_path}"
|
| 1449 |
+
else:
|
| 1450 |
+
# …
|
| 1451 |
+
# and relativizing ".."
|
| 1452 |
+
# parts[0] is / for absolute urls,
|
| 1453 |
+
# this join will add a double slash there
|
| 1454 |
+
path = "/".join([*self.parts[:-1], ""]) + join_path
|
| 1455 |
+
# which has to be removed
|
| 1456 |
+
if orig_path[0] == "/":
|
| 1457 |
+
path = path[1:]
|
| 1458 |
+
path = normalize_path(path) if "." in path else path
|
| 1459 |
+
else:
|
| 1460 |
+
path = orig_path
|
| 1461 |
+
|
| 1462 |
+
return from_parts(
|
| 1463 |
+
scheme,
|
| 1464 |
+
self._netloc,
|
| 1465 |
+
path,
|
| 1466 |
+
url._query if join_path or url._query else self._query,
|
| 1467 |
+
url._fragment if join_path or url._fragment else self._fragment,
|
| 1468 |
+
)
|
| 1469 |
+
|
| 1470 |
+
def joinpath(self, *other: str, encoded: bool = False) -> "URL":
|
| 1471 |
+
"""Return a new URL with the elements in other appended to the path."""
|
| 1472 |
+
return self._make_child(other, encoded=encoded)
|
| 1473 |
+
|
| 1474 |
+
def human_repr(self) -> str:
|
| 1475 |
+
"""Return decoded human readable string for URL representation."""
|
| 1476 |
+
user = human_quote(self.user, "#/:?@[]")
|
| 1477 |
+
password = human_quote(self.password, "#/:?@[]")
|
| 1478 |
+
if (host := self.host) and ":" in host:
|
| 1479 |
+
host = f"[{host}]"
|
| 1480 |
+
path = human_quote(self.path, "#?")
|
| 1481 |
+
if TYPE_CHECKING:
|
| 1482 |
+
assert path is not None
|
| 1483 |
+
query_string = "&".join(
|
| 1484 |
+
"{}={}".format(human_quote(k, "#&+;="), human_quote(v, "#&+;="))
|
| 1485 |
+
for k, v in self.query.items()
|
| 1486 |
+
)
|
| 1487 |
+
fragment = human_quote(self.fragment, "")
|
| 1488 |
+
if TYPE_CHECKING:
|
| 1489 |
+
assert fragment is not None
|
| 1490 |
+
netloc = make_netloc(user, password, host, self.explicit_port)
|
| 1491 |
+
return unsplit_result(self._scheme, netloc, path, query_string, fragment)
|
| 1492 |
+
|
| 1493 |
+
if HAS_PYDANTIC: # pragma: no cover
|
| 1494 |
+
# Borrowed from https://docs.pydantic.dev/latest/concepts/types/#handling-third-party-types
|
| 1495 |
+
@classmethod
|
| 1496 |
+
def __get_pydantic_json_schema__(
|
| 1497 |
+
cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
|
| 1498 |
+
) -> JsonSchemaValue:
|
| 1499 |
+
field_schema: dict[str, Any] = {}
|
| 1500 |
+
field_schema.update(type="string", format="uri")
|
| 1501 |
+
return field_schema
|
| 1502 |
+
|
| 1503 |
+
@classmethod
|
| 1504 |
+
def __get_pydantic_core_schema__(
|
| 1505 |
+
cls, source_type: type[Self] | type[str], handler: GetCoreSchemaHandler
|
| 1506 |
+
) -> core_schema.CoreSchema:
|
| 1507 |
+
from_str_schema = core_schema.chain_schema(
|
| 1508 |
+
[
|
| 1509 |
+
core_schema.str_schema(),
|
| 1510 |
+
core_schema.no_info_plain_validator_function(URL),
|
| 1511 |
+
]
|
| 1512 |
+
)
|
| 1513 |
+
|
| 1514 |
+
return core_schema.json_or_python_schema(
|
| 1515 |
+
json_schema=from_str_schema,
|
| 1516 |
+
python_schema=core_schema.union_schema(
|
| 1517 |
+
[
|
| 1518 |
+
# check if it's an instance first before doing any further work
|
| 1519 |
+
core_schema.is_instance_schema(URL),
|
| 1520 |
+
from_str_schema,
|
| 1521 |
+
]
|
| 1522 |
+
),
|
| 1523 |
+
serialization=core_schema.plain_serializer_function_ser_schema(str),
|
| 1524 |
+
)
|
| 1525 |
+
|
| 1526 |
+
|
| 1527 |
+
_DEFAULT_IDNA_SIZE = 256
|
| 1528 |
+
_DEFAULT_ENCODE_SIZE = 512
|
| 1529 |
+
|
| 1530 |
+
|
| 1531 |
+
@lru_cache(_DEFAULT_IDNA_SIZE)
|
| 1532 |
+
def _idna_decode(raw: str) -> str:
|
| 1533 |
+
try:
|
| 1534 |
+
return idna.decode(raw.encode("ascii"))
|
| 1535 |
+
except UnicodeError: # e.g. '::1'
|
| 1536 |
+
return raw.encode("ascii").decode("idna")
|
| 1537 |
+
|
| 1538 |
+
|
| 1539 |
+
@lru_cache(_DEFAULT_IDNA_SIZE)
|
| 1540 |
+
def _idna_encode(host: str) -> str:
|
| 1541 |
+
try:
|
| 1542 |
+
return idna.encode(host, uts46=True).decode("ascii")
|
| 1543 |
+
except UnicodeError:
|
| 1544 |
+
return host.encode("idna").decode("ascii")
|
| 1545 |
+
|
| 1546 |
+
|
| 1547 |
+
@lru_cache(_DEFAULT_ENCODE_SIZE)
|
| 1548 |
+
def _encode_host(host: str, validate_host: bool) -> str:
|
| 1549 |
+
"""Encode host part of URL."""
|
| 1550 |
+
# If the host ends with a digit or contains a colon, its likely
|
| 1551 |
+
# an IP address.
|
| 1552 |
+
if host and (host[-1].isdigit() or ":" in host):
|
| 1553 |
+
raw_ip, sep, zone = host.partition("%")
|
| 1554 |
+
# If it looks like an IP, we check with _ip_compressed_version
|
| 1555 |
+
# and fall-through if its not an IP address. This is a performance
|
| 1556 |
+
# optimization to avoid parsing IP addresses as much as possible
|
| 1557 |
+
# because it is orders of magnitude slower than almost any other
|
| 1558 |
+
# operation this library does.
|
| 1559 |
+
# Might be an IP address, check it
|
| 1560 |
+
#
|
| 1561 |
+
# IP Addresses can look like:
|
| 1562 |
+
# https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
|
| 1563 |
+
# - 127.0.0.1 (last character is a digit)
|
| 1564 |
+
# - 2001:db8::ff00:42:8329 (contains a colon)
|
| 1565 |
+
# - 2001:db8::ff00:42:8329%eth0 (contains a colon)
|
| 1566 |
+
# - [2001:db8::ff00:42:8329] (contains a colon -- brackets should
|
| 1567 |
+
# have been removed before it gets here)
|
| 1568 |
+
# Rare IP Address formats are not supported per:
|
| 1569 |
+
# https://datatracker.ietf.org/doc/html/rfc3986#section-7.4
|
| 1570 |
+
#
|
| 1571 |
+
# IP parsing is slow, so its wrapped in an LRU
|
| 1572 |
+
try:
|
| 1573 |
+
ip = ip_address(raw_ip)
|
| 1574 |
+
except ValueError:
|
| 1575 |
+
pass
|
| 1576 |
+
else:
|
| 1577 |
+
# These checks should not happen in the
|
| 1578 |
+
# LRU to keep the cache size small
|
| 1579 |
+
host = ip.compressed
|
| 1580 |
+
if ip.version == 6:
|
| 1581 |
+
return f"[{host}%{zone}]" if sep else f"[{host}]"
|
| 1582 |
+
return f"{host}%{zone}" if sep else host
|
| 1583 |
+
|
| 1584 |
+
# IDNA encoding is slow, skip it for ASCII-only strings
|
| 1585 |
+
if host.isascii():
|
| 1586 |
+
# Check for invalid characters explicitly; _idna_encode() does this
|
| 1587 |
+
# for non-ascii host names.
|
| 1588 |
+
host = host.lower()
|
| 1589 |
+
if validate_host and (invalid := NOT_REG_NAME.search(host)):
|
| 1590 |
+
value, pos, extra = invalid.group(), invalid.start(), ""
|
| 1591 |
+
if value == "@" or (value == ":" and "@" in host[pos:]):
|
| 1592 |
+
# this looks like an authority string
|
| 1593 |
+
extra = (
|
| 1594 |
+
", if the value includes a username or password, "
|
| 1595 |
+
"use 'authority' instead of 'host'"
|
| 1596 |
+
)
|
| 1597 |
+
raise ValueError(
|
| 1598 |
+
f"Host {host!r} cannot contain {value!r} (at position {pos}){extra}"
|
| 1599 |
+
) from None
|
| 1600 |
+
return host
|
| 1601 |
+
|
| 1602 |
+
return _idna_encode(host)
|
| 1603 |
+
|
| 1604 |
+
|
| 1605 |
+
@rewrite_module
|
| 1606 |
+
def cache_clear() -> None:
|
| 1607 |
+
"""Clear all LRU caches."""
|
| 1608 |
+
_idna_encode.cache_clear()
|
| 1609 |
+
_idna_decode.cache_clear()
|
| 1610 |
+
_encode_host.cache_clear()
|
| 1611 |
+
|
| 1612 |
+
|
| 1613 |
+
@rewrite_module
|
| 1614 |
+
def cache_info() -> CacheInfo:
|
| 1615 |
+
"""Report cache statistics."""
|
| 1616 |
+
return {
|
| 1617 |
+
"idna_encode": _idna_encode.cache_info(),
|
| 1618 |
+
"idna_decode": _idna_decode.cache_info(),
|
| 1619 |
+
"ip_address": _encode_host.cache_info(),
|
| 1620 |
+
"host_validate": _encode_host.cache_info(),
|
| 1621 |
+
"encode_host": _encode_host.cache_info(),
|
| 1622 |
+
}
|
| 1623 |
+
|
| 1624 |
+
|
| 1625 |
+
@rewrite_module
|
| 1626 |
+
def cache_configure(
|
| 1627 |
+
*,
|
| 1628 |
+
idna_encode_size: int | None = _DEFAULT_IDNA_SIZE,
|
| 1629 |
+
idna_decode_size: int | None = _DEFAULT_IDNA_SIZE,
|
| 1630 |
+
ip_address_size: int | None | UndefinedType = UNDEFINED,
|
| 1631 |
+
host_validate_size: int | None | UndefinedType = UNDEFINED,
|
| 1632 |
+
encode_host_size: int | None | UndefinedType = UNDEFINED,
|
| 1633 |
+
) -> None:
|
| 1634 |
+
"""Configure LRU cache sizes."""
|
| 1635 |
+
global _idna_decode, _idna_encode, _encode_host
|
| 1636 |
+
# ip_address_size, host_validate_size are no longer
|
| 1637 |
+
# used, but are kept for backwards compatibility.
|
| 1638 |
+
if ip_address_size is not UNDEFINED or host_validate_size is not UNDEFINED:
|
| 1639 |
+
warnings.warn(
|
| 1640 |
+
"cache_configure() no longer accepts the "
|
| 1641 |
+
"ip_address_size or host_validate_size arguments, "
|
| 1642 |
+
"they are used to set the encode_host_size instead "
|
| 1643 |
+
"and will be removed in the future",
|
| 1644 |
+
DeprecationWarning,
|
| 1645 |
+
stacklevel=2,
|
| 1646 |
+
)
|
| 1647 |
+
|
| 1648 |
+
if encode_host_size is not None:
|
| 1649 |
+
for size in (ip_address_size, host_validate_size):
|
| 1650 |
+
if size is None:
|
| 1651 |
+
encode_host_size = None
|
| 1652 |
+
elif encode_host_size is UNDEFINED:
|
| 1653 |
+
if size is not UNDEFINED:
|
| 1654 |
+
encode_host_size = size
|
| 1655 |
+
elif size is not UNDEFINED:
|
| 1656 |
+
if TYPE_CHECKING:
|
| 1657 |
+
assert isinstance(size, int)
|
| 1658 |
+
assert isinstance(encode_host_size, int)
|
| 1659 |
+
encode_host_size = max(size, encode_host_size)
|
| 1660 |
+
if encode_host_size is UNDEFINED:
|
| 1661 |
+
encode_host_size = _DEFAULT_ENCODE_SIZE
|
| 1662 |
+
|
| 1663 |
+
_encode_host = lru_cache(encode_host_size)(_encode_host.__wrapped__)
|
| 1664 |
+
_idna_decode = lru_cache(idna_decode_size)(_idna_decode.__wrapped__)
|
| 1665 |
+
_idna_encode = lru_cache(idna_encode_size)(_idna_encode.__wrapped__)
|
loan-advisor-agent/.venv/Lib/site-packages/yarl/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Placeholder
|