annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/packaging/requirements.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 # This file is dual licensed under the terms of the Apache License, Version
jpayne@69 2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
jpayne@69 3 # for complete details.
jpayne@69 4 from __future__ import annotations
jpayne@69 5
jpayne@69 6 from typing import Any, Iterator
jpayne@69 7
jpayne@69 8 from ._parser import parse_requirement as _parse_requirement
jpayne@69 9 from ._tokenizer import ParserSyntaxError
jpayne@69 10 from .markers import Marker, _normalize_extra_values
jpayne@69 11 from .specifiers import SpecifierSet
jpayne@69 12 from .utils import canonicalize_name
jpayne@69 13
jpayne@69 14
jpayne@69 15 class InvalidRequirement(ValueError):
jpayne@69 16 """
jpayne@69 17 An invalid requirement was found, users should refer to PEP 508.
jpayne@69 18 """
jpayne@69 19
jpayne@69 20
jpayne@69 21 class Requirement:
jpayne@69 22 """Parse a requirement.
jpayne@69 23
jpayne@69 24 Parse a given requirement string into its parts, such as name, specifier,
jpayne@69 25 URL, and extras. Raises InvalidRequirement on a badly-formed requirement
jpayne@69 26 string.
jpayne@69 27 """
jpayne@69 28
jpayne@69 29 # TODO: Can we test whether something is contained within a requirement?
jpayne@69 30 # If so how do we do that? Do we need to test against the _name_ of
jpayne@69 31 # the thing as well as the version? What about the markers?
jpayne@69 32 # TODO: Can we normalize the name and extra name?
jpayne@69 33
jpayne@69 34 def __init__(self, requirement_string: str) -> None:
jpayne@69 35 try:
jpayne@69 36 parsed = _parse_requirement(requirement_string)
jpayne@69 37 except ParserSyntaxError as e:
jpayne@69 38 raise InvalidRequirement(str(e)) from e
jpayne@69 39
jpayne@69 40 self.name: str = parsed.name
jpayne@69 41 self.url: str | None = parsed.url or None
jpayne@69 42 self.extras: set[str] = set(parsed.extras or [])
jpayne@69 43 self.specifier: SpecifierSet = SpecifierSet(parsed.specifier)
jpayne@69 44 self.marker: Marker | None = None
jpayne@69 45 if parsed.marker is not None:
jpayne@69 46 self.marker = Marker.__new__(Marker)
jpayne@69 47 self.marker._markers = _normalize_extra_values(parsed.marker)
jpayne@69 48
jpayne@69 49 def _iter_parts(self, name: str) -> Iterator[str]:
jpayne@69 50 yield name
jpayne@69 51
jpayne@69 52 if self.extras:
jpayne@69 53 formatted_extras = ",".join(sorted(self.extras))
jpayne@69 54 yield f"[{formatted_extras}]"
jpayne@69 55
jpayne@69 56 if self.specifier:
jpayne@69 57 yield str(self.specifier)
jpayne@69 58
jpayne@69 59 if self.url:
jpayne@69 60 yield f"@ {self.url}"
jpayne@69 61 if self.marker:
jpayne@69 62 yield " "
jpayne@69 63
jpayne@69 64 if self.marker:
jpayne@69 65 yield f"; {self.marker}"
jpayne@69 66
jpayne@69 67 def __str__(self) -> str:
jpayne@69 68 return "".join(self._iter_parts(self.name))
jpayne@69 69
jpayne@69 70 def __repr__(self) -> str:
jpayne@69 71 return f"<Requirement('{self}')>"
jpayne@69 72
jpayne@69 73 def __hash__(self) -> int:
jpayne@69 74 return hash(
jpayne@69 75 (
jpayne@69 76 self.__class__.__name__,
jpayne@69 77 *self._iter_parts(canonicalize_name(self.name)),
jpayne@69 78 )
jpayne@69 79 )
jpayne@69 80
jpayne@69 81 def __eq__(self, other: Any) -> bool:
jpayne@69 82 if not isinstance(other, Requirement):
jpayne@69 83 return NotImplemented
jpayne@69 84
jpayne@69 85 return (
jpayne@69 86 canonicalize_name(self.name) == canonicalize_name(other.name)
jpayne@69 87 and self.extras == other.extras
jpayne@69 88 and self.specifier == other.specifier
jpayne@69 89 and self.url == other.url
jpayne@69 90 and self.marker == other.marker
jpayne@69 91 )