annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/packaging/requirements.py @ 68:5028fdace37b

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