requirements.py 2.9 KB

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