Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
Link Class Reference
Inheritance diagram for Link:
Collaboration diagram for Link:

Public Member Functions

None __init__ (self, str url, Optional[Union[str, "IndexContent"]] comes_from=None, Optional[str] requires_python=None, Optional[str] yanked_reason=None, Optional[MetadataFile] metadata_file_data=None, bool cache_link_parsing=True, Optional[Mapping[str, str]] hashes=None)
 
Optional["Link"] from_json (cls, Dict[str, Any] file_data, str page_url)
 
Optional["Link"] from_element (cls, Dict[str, Optional[str]] anchor_attribs, str page_url, str base_url)
 
str __str__ (self)
 
str __repr__ (self)
 
str url (self)
 
str filename (self)
 
str file_path (self)
 
str scheme (self)
 
str netloc (self)
 
str path (self)
 
Tuple[str, str] splitext (self)
 
str ext (self)
 
str url_without_fragment (self)
 
Optional[str] subdirectory_fragment (self)
 
Optional["Link"] metadata_link (self)
 
Hashes as_hashes (self)
 
Optional[str] hash (self)
 
Optional[str] hash_name (self)
 
str show_url (self)
 
bool is_file (self)
 
bool is_existing_dir (self)
 
bool is_wheel (self)
 
bool is_vcs (self)
 
bool is_yanked (self)
 
bool has_hash (self)
 
bool is_hash_allowed (self, Optional[Hashes] hashes)
 
- Public Member Functions inherited from KeyBasedCompareMixin
int __hash__ (self)
 
bool __lt__ (self, Any other)
 
bool __le__ (self, Any other)
 
bool __gt__ (self, Any other)
 
bool __ge__ (self, Any other)
 
bool __eq__ (self, Any other)
 

Data Fields

 comes_from
 
 requires_python
 
 yanked_reason
 
 metadata_file_data
 
 cache_link_parsing
 
 egg_fragment
 
 url
 
 scheme
 
 file_path
 
 ext
 

Protected Member Functions

Optional[str] _egg_fragment (self)
 
- Protected Member Functions inherited from KeyBasedCompareMixin
bool _compare (self, Any other, Callable[[Any, Any], bool] method)
 

Protected Attributes

 _parsed_url
 
 _url
 
 _hashes
 
- Protected Attributes inherited from KeyBasedCompareMixin
 _compare_key
 
 _defining_class
 

Static Protected Attributes

 _egg_fragment_re = re.compile(r"[#&]egg=([^&]*)")
 
 _project_name_re
 
 _subdirectory_fragment_re = re.compile(r"[#&]subdirectory=([^&]*)")
 

Detailed Description

Represents a parsed link from a Package Index's simple URL

Definition at line 182 of file link.py.

Constructor & Destructor Documentation

◆ __init__()

None __init__ (   self,
str  url,
Optional[Union[str, "IndexContent"]]   comes_from = None,
Optional[str]   requires_python = None,
Optional[str]   yanked_reason = None,
Optional[MetadataFile]   metadata_file_data = None,
bool   cache_link_parsing = True,
Optional[Mapping[str, str]]   hashes = None 
)
:param url: url of the resource pointed to (href of the link)
:param comes_from: instance of IndexContent where the link was found,
    or string.
:param requires_python: String containing the `Requires-Python`
    metadata field, specified in PEP 345. This may be specified by
    a data-requires-python attribute in the HTML link tag, as
    described in PEP 503.
:param yanked_reason: the reason the file has been yanked, if the
    file has been yanked, or None if the file hasn't been yanked.
    This is the value of the "data-yanked" attribute, if present, in
    a simple repository HTML link. If the file has been yanked but
    no reason was provided, this should be the empty string. See
    PEP 592 for more information and the specification.
:param metadata_file_data: the metadata attached to the file, or None if
    no such metadata is provided. This argument, if not None, indicates
    that a separate metadata file exists, and also optionally supplies
    hashes for that file.
:param cache_link_parsing: A flag that is used elsewhere to determine
    whether resources retrieved from this link should be cached. PyPI
    URLs should generally have this set to False, for example.
:param hashes: A mapping of hash names to digests to allow us to
    determine the validity of a download.

Reimplemented from KeyBasedCompareMixin.

Definition at line 197 of file link.py.

206 ) -> None:
207 """
208 :param url: url of the resource pointed to (href of the link)
209 :param comes_from: instance of IndexContent where the link was found,
210 or string.
211 :param requires_python: String containing the `Requires-Python`
212 metadata field, specified in PEP 345. This may be specified by
213 a data-requires-python attribute in the HTML link tag, as
214 described in PEP 503.
215 :param yanked_reason: the reason the file has been yanked, if the
216 file has been yanked, or None if the file hasn't been yanked.
217 This is the value of the "data-yanked" attribute, if present, in
218 a simple repository HTML link. If the file has been yanked but
219 no reason was provided, this should be the empty string. See
220 PEP 592 for more information and the specification.
221 :param metadata_file_data: the metadata attached to the file, or None if
222 no such metadata is provided. This argument, if not None, indicates
223 that a separate metadata file exists, and also optionally supplies
224 hashes for that file.
225 :param cache_link_parsing: A flag that is used elsewhere to determine
226 whether resources retrieved from this link should be cached. PyPI
227 URLs should generally have this set to False, for example.
228 :param hashes: A mapping of hash names to digests to allow us to
229 determine the validity of a download.
230 """
231
232 # The comes_from, requires_python, and metadata_file_data arguments are
233 # only used by classmethods of this class, and are not used in client
234 # code directly.
235
236 # url can be a UNC windows share
237 if url.startswith("\\\\"):
238 url = path_to_url(url)
239
240 self._parsed_url = urllib.parse.urlsplit(url)
241 # Store the url as a private attribute to prevent accidentally
242 # trying to set a new value.
243 self._url = url
244
245 link_hash = LinkHash.find_hash_url_fragment(url)
246 hashes_from_link = {} if link_hash is None else link_hash.as_dict()
247 if hashes is None:
248 self._hashes = hashes_from_link
249 else:
250 self._hashes = {**hashes, **hashes_from_link}
251
252 self.comes_from = comes_from
253 self.requires_python = requires_python if requires_python else None
254 self.yanked_reason = yanked_reason
255 self.metadata_file_data = metadata_file_data
256
257 super().__init__(key=url, defining_class=Link)
258
259 self.cache_link_parsing = cache_link_parsing
260 self.egg_fragment = self._egg_fragment()
261
for i

References i.

Referenced by Protocol.__init_subclass__().

Here is the caller graph for this function:

Member Function Documentation

◆ __repr__()

str __repr__ (   self)

Definition at line 377 of file link.py.

377 def __repr__(self) -> str:
378 return f"<Link {self}>"
379

◆ __str__()

str __str__ (   self)

Definition at line 365 of file link.py.

365 def __str__(self) -> str:
366 if self.requires_python:
367 rp = f" (requires-python:{self.requires_python})"
368 else:
369 rp = ""
370 if self.comes_from:
371 return "{} (from {}){}".format(
372 redact_auth_from_url(self._url), self.comes_from, rp
373 )
374 else:
375 return redact_auth_from_url(str(self._url))
376

References Link._url, LazyZipOverHTTP._url, Link.comes_from, ParsedRequirement.comes_from, InstallRequirement.comes_from, BaseDistribution.requires_python(), and Link.requires_python.

Here is the call graph for this function:

◆ _egg_fragment()

Optional[str] _egg_fragment (   self)
protected

Definition at line 436 of file link.py.

436 def _egg_fragment(self) -> Optional[str]:
437 match = self._egg_fragment_re.search(self._url)
438 if not match:
439 return None
440
441 # An egg fragment looks like a PEP 508 project name, along with
442 # an optional extras specifier. Anything else is invalid.
443 project_name = match.group(1)
444 if not self._project_name_re.match(project_name):
445 deprecated(
446 reason=f"{self} contains an egg fragment with a non-PEP 508 name",
447 replacement="to use the req @ url syntax, and remove the egg fragment",
448 gone_in="25.0",
449 issue=11617,
450 )
451
452 return project_name
453

References Link._egg_fragment_re, Link._project_name_re, Link._url, LazyZipOverHTTP._url, and i.

◆ as_hashes()

Hashes as_hashes (   self)

Definition at line 472 of file link.py.

472 def as_hashes(self) -> Hashes:
473 return Hashes({k: [v] for k, v in self._hashes.items()})
474

References CandidateEvaluator._hashes, and Link._hashes.

◆ ext()

str ext (   self)

Definition at line 421 of file link.py.

421 def ext(self) -> str:
422 return self.splitext()[1]
423

References Link.splitext().

Here is the call graph for this function:

◆ file_path()

str file_path (   self)

Definition at line 399 of file link.py.

399 def file_path(self) -> str:
400 return url_to_path(self.url)
401

◆ filename()

str filename (   self)

Definition at line 385 of file link.py.

385 def filename(self) -> str:
386 path = self.path.rstrip("/")
387 name = posixpath.basename(path)
388 if not name:
389 # Make sure we don't leak auth information if the netloc
390 # includes a username and password.
391 netloc, user_pass = split_auth_from_netloc(self.netloc)
392 return netloc
393
394 name = urllib.parse.unquote(name)
395 assert name, f"URL {self._url!r} produced no filename"
396 return name
397

References i, PackageIndex.netloc, Link.netloc(), Url.netloc(), _Prefix.path, Link.path(), File.path, TempDirectory.path(), _Cache.path, DistributionPath.path, BaseInstalledDistribution.path, InstalledDistribution.path, EggInfoDistribution.path, and FileMetadata.path.

Referenced by Wheel._get_extensions(), HtmlFormatter._wrap_pre(), HtmlFormatter._wrap_tablelinenos(), Wheel.exists(), Wheel.info(), Wheel.install(), Wheel.metadata(), Wheel.mount(), PyPIRCFile.read(), Wheel.unmount(), Wheel.update(), PyPIRCFile.update(), and Wheel.verify().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_element()

Optional["Link"] from_element (   cls,
Dict[str, Optional[str]]  anchor_attribs,
str  page_url,
str  base_url 
)
Convert an anchor element's attributes in a simple repository page to a Link.

Definition at line 314 of file link.py.

319 ) -> Optional["Link"]:
320 """
321 Convert an anchor element's attributes in a simple repository page to a Link.
322 """
323 href = anchor_attribs.get("href")
324 if not href:
325 return None
326
327 url = _ensure_quoted_url(urllib.parse.urljoin(base_url, href))
328 pyrequire = anchor_attribs.get("data-requires-python")
329 yanked_reason = anchor_attribs.get("data-yanked")
330
331 # PEP 714: Indexes must use the name data-core-metadata, but
332 # clients should support the old name as a fallback for compatibility.
333 metadata_info = anchor_attribs.get("data-core-metadata")
334 if metadata_info is None:
335 metadata_info = anchor_attribs.get("data-dist-info-metadata")
336 # The metadata info value may be the string "true", or a string of
337 # the form "hashname=hashval"
338 if metadata_info == "true":
339 # The file exists, but there are no hashes
340 metadata_file_data = MetadataFile(None)
341 elif metadata_info is None:
342 # The file does not exist
343 metadata_file_data = None
344 else:
345 # The file exists, and hashes have been supplied
346 hashname, sep, hashval = metadata_info.partition("=")
347 if sep == "=":
348 metadata_file_data = MetadataFile(supported_hashes({hashname: hashval}))
349 else:
350 # Error - data is wrong. Treat as no hashes supplied.
352 "Index returned invalid data-dist-info-metadata value: %s",
353 metadata_info,
354 )
355 metadata_file_data = MetadataFile(None)
356
357 return cls(
358 url,
359 comes_from=page_url,
360 requires_python=pyrequire,
361 yanked_reason=yanked_reason,
362 metadata_file_data=metadata_file_data,
363 )
364

References pip._internal.models.link._ensure_quoted_url(), i, and pip._internal.models.link.supported_hashes().

Here is the call graph for this function:

◆ from_json()

Optional["Link"] from_json (   cls,
Dict[str, Any]  file_data,
str  page_url 
)
Convert an pypi json document from a simple repository page into a Link.

Definition at line 263 of file link.py.

267 ) -> Optional["Link"]:
268 """
269 Convert an pypi json document from a simple repository page into a Link.
270 """
271 file_url = file_data.get("url")
272 if file_url is None:
273 return None
274
275 url = _ensure_quoted_url(urllib.parse.urljoin(page_url, file_url))
276 pyrequire = file_data.get("requires-python")
277 yanked_reason = file_data.get("yanked")
278 hashes = file_data.get("hashes", {})
279
280 # PEP 714: Indexes must use the name core-metadata, but
281 # clients should support the old name as a fallback for compatibility.
282 metadata_info = file_data.get("core-metadata")
283 if metadata_info is None:
284 metadata_info = file_data.get("dist-info-metadata")
285
286 # The metadata info value may be a boolean, or a dict of hashes.
287 if isinstance(metadata_info, dict):
288 # The file exists, and hashes have been supplied
289 metadata_file_data = MetadataFile(supported_hashes(metadata_info))
290 elif metadata_info:
291 # The file exists, but there are no hashes
292 metadata_file_data = MetadataFile(None)
293 else:
294 # False or not present: the file does not exist
295 metadata_file_data = None
296
297 # The Link.yanked_reason expects an empty string instead of a boolean.
298 if yanked_reason and not isinstance(yanked_reason, str):
299 yanked_reason = ""
300 # The Link.yanked_reason expects None instead of False.
301 elif not yanked_reason:
302 yanked_reason = None
303
304 return cls(
305 url,
306 comes_from=page_url,
307 requires_python=pyrequire,
308 yanked_reason=yanked_reason,
309 hashes=hashes,
310 metadata_file_data=metadata_file_data,
311 )
312

References pip._internal.models.link._ensure_quoted_url(), i, and pip._internal.models.link.supported_hashes().

Here is the call graph for this function:

◆ has_hash()

bool has_hash (   self)

Definition at line 509 of file link.py.

509 def has_hash(self) -> bool:
510 return bool(self._hashes)
511

References CandidateEvaluator._hashes, and Link._hashes.

◆ hash()

Optional[str] hash (   self)

Definition at line 476 of file link.py.

476 def hash(self) -> Optional[str]:
477 return next(iter(self._hashes.values()), None)
478

References CandidateEvaluator._hashes, and Link._hashes.

Referenced by ArchiveInfo._to_dict().

Here is the caller graph for this function:

◆ hash_name()

Optional[str] hash_name (   self)

Definition at line 480 of file link.py.

480 def hash_name(self) -> Optional[str]:
481 return next(iter(self._hashes), None)
482

References CandidateEvaluator._hashes, and Link._hashes.

◆ is_existing_dir()

bool is_existing_dir (   self)

Definition at line 491 of file link.py.

491 def is_existing_dir(self) -> bool:
492 return self.is_file and os.path.isdir(self.file_path)
493

◆ is_file()

bool is_file (   self)

Definition at line 488 of file link.py.

488 def is_file(self) -> bool:
489 return self.scheme == "file"
490

Referenced by BaseDistribution.requested().

Here is the caller graph for this function:

◆ is_hash_allowed()

bool is_hash_allowed (   self,
Optional[Hashes hashes 
)
Return True if the link has a hash and it is allowed by `hashes`.

Definition at line 512 of file link.py.

512 def is_hash_allowed(self, hashes: Optional[Hashes]) -> bool:
513 """
514 Return True if the link has a hash and it is allowed by `hashes`.
515 """
516 if hashes is None:
517 return False
518 return any(hashes.is_hash_allowed(k, v) for k, v in self._hashes.items())
519
520

References CandidateEvaluator._hashes, Link._hashes, and i.

Referenced by Hashes.has_one_of().

Here is the caller graph for this function:

◆ is_vcs()

bool is_vcs (   self)

Definition at line 499 of file link.py.

499 def is_vcs(self) -> bool:
500 from pip._internal.vcs import vcs
501
502 return self.scheme in vcs.all_schemes
503

References i, Link.scheme(), Link.scheme, Locator.scheme, AggregatingLocator.scheme, LegacyMetadata.scheme, Metadata.scheme, ConnectionPool.scheme, HTTPConnectionPool.scheme, HTTPSConnectionPool.scheme, NTLMConnectionPool.scheme, and URLSchemeUnknown.scheme.

Here is the call graph for this function:

◆ is_wheel()

bool is_wheel (   self)

Definition at line 495 of file link.py.

495 def is_wheel(self) -> bool:
496 return self.ext == WHEEL_EXTENSION
497

Referenced by InstallRequirement.get_dist(), and InstallRequirement.install().

Here is the caller graph for this function:

◆ is_yanked()

bool is_yanked (   self)

Definition at line 505 of file link.py.

505 def is_yanked(self) -> bool:
506 return self.yanked_reason is not None
507

References Link.yanked_reason.

◆ metadata_link()

Optional["Link"] metadata_link (   self)
Return a link to the associated core metadata file (if any).

Definition at line 463 of file link.py.

463 def metadata_link(self) -> Optional["Link"]:
464 """Return a link to the associated core metadata file (if any)."""
465 if self.metadata_file_data is None:
466 return None
467 metadata_url = f"{self.url_without_fragment}.metadata"
468 if self.metadata_file_data.hashes is None:
469 return Link(metadata_url)
470 return Link(metadata_url, hashes=self.metadata_file_data.hashes)
471

References Link.metadata_file_data.

◆ netloc()

str netloc (   self)
This can contain auth information.

Definition at line 407 of file link.py.

407 def netloc(self) -> str:
408 """
409 This can contain auth information.
410 """
411 return self._parsed_url.netloc
412

References Link._parsed_url.

Referenced by Link.filename().

Here is the caller graph for this function:

◆ path()

◆ scheme()

◆ show_url()

str show_url (   self)

Definition at line 484 of file link.py.

484 def show_url(self) -> str:
485 return posixpath.basename(self._url.split("#", 1)[0].split("?", 1)[0])
486

References Link._url, LazyZipOverHTTP._url, and i.

◆ splitext()

Tuple[str, str] splitext (   self)

Definition at line 417 of file link.py.

417 def splitext(self) -> Tuple[str, str]:
418 return splitext(posixpath.basename(self.path.rstrip("/")))
419

References i, _Prefix.path, Link.path(), File.path, TempDirectory.path(), _Cache.path, DistributionPath.path, BaseInstalledDistribution.path, InstalledDistribution.path, EggInfoDistribution.path, and FileMetadata.path.

Referenced by Link.ext().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ subdirectory_fragment()

Optional[str] subdirectory_fragment (   self)

Definition at line 457 of file link.py.

457 def subdirectory_fragment(self) -> Optional[str]:
458 match = self._subdirectory_fragment_re.search(self._url)
459 if not match:
460 return None
461 return match.group(1)
462

References Link._subdirectory_fragment_re, Link._url, LazyZipOverHTTP._url, and i.

◆ url()

◆ url_without_fragment()

str url_without_fragment (   self)

Definition at line 425 of file link.py.

425 def url_without_fragment(self) -> str:
426 scheme, netloc, path, query, fragment = self._parsed_url
427 return urllib.parse.urlunsplit((scheme, netloc, path, query, ""))
428

References Link._parsed_url, and i.

Field Documentation

◆ _egg_fragment_re

_egg_fragment_re = re.compile(r"[#&]egg=([^&]*)")
staticprotected

Definition at line 429 of file link.py.

Referenced by Link._egg_fragment().

◆ _hashes

◆ _parsed_url

_parsed_url
protected

Definition at line 240 of file link.py.

Referenced by Link.netloc(), Link.path(), Link.scheme(), and Link.url_without_fragment().

◆ _project_name_re

_project_name_re
staticprotected
Initial value:
r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.IGNORECASE
)

Definition at line 432 of file link.py.

Referenced by Link._egg_fragment().

◆ _subdirectory_fragment_re

_subdirectory_fragment_re = re.compile(r"[#&]subdirectory=([^&]*)")
staticprotected

Definition at line 454 of file link.py.

Referenced by Link.subdirectory_fragment().

◆ _url

◆ cache_link_parsing

cache_link_parsing

Definition at line 259 of file link.py.

◆ comes_from

comes_from

Definition at line 252 of file link.py.

Referenced by Link.__str__(), InstallRequirement.__str__(), and InstallRequirement.from_path().

◆ egg_fragment

egg_fragment

Definition at line 260 of file link.py.

◆ ext

ext

Definition at line 496 of file link.py.

◆ file_path

file_path

Definition at line 492 of file link.py.

◆ metadata_file_data

metadata_file_data

Definition at line 255 of file link.py.

Referenced by Link.metadata_link().

◆ requires_python

requires_python

Definition at line 253 of file link.py.

Referenced by Link.__str__().

◆ scheme

◆ url

◆ yanked_reason

yanked_reason

Definition at line 254 of file link.py.

Referenced by Link.is_yanked().


The documentation for this class was generated from the following file: