jpayne@68: # This file is dual licensed under the terms of the Apache License, Version jpayne@68: # 2.0, and the BSD License. See the LICENSE file in the root of this repository jpayne@68: # for complete details. jpayne@68: from __future__ import annotations jpayne@68: jpayne@68: from typing import Any, Iterator jpayne@68: jpayne@68: from ._parser import parse_requirement as _parse_requirement jpayne@68: from ._tokenizer import ParserSyntaxError jpayne@68: from .markers import Marker, _normalize_extra_values jpayne@68: from .specifiers import SpecifierSet jpayne@68: from .utils import canonicalize_name jpayne@68: jpayne@68: jpayne@68: class InvalidRequirement(ValueError): jpayne@68: """ jpayne@68: An invalid requirement was found, users should refer to PEP 508. jpayne@68: """ jpayne@68: jpayne@68: jpayne@68: class Requirement: jpayne@68: """Parse a requirement. jpayne@68: jpayne@68: Parse a given requirement string into its parts, such as name, specifier, jpayne@68: URL, and extras. Raises InvalidRequirement on a badly-formed requirement jpayne@68: string. jpayne@68: """ jpayne@68: jpayne@68: # TODO: Can we test whether something is contained within a requirement? jpayne@68: # If so how do we do that? Do we need to test against the _name_ of jpayne@68: # the thing as well as the version? What about the markers? jpayne@68: # TODO: Can we normalize the name and extra name? jpayne@68: jpayne@68: def __init__(self, requirement_string: str) -> None: jpayne@68: try: jpayne@68: parsed = _parse_requirement(requirement_string) jpayne@68: except ParserSyntaxError as e: jpayne@68: raise InvalidRequirement(str(e)) from e jpayne@68: jpayne@68: self.name: str = parsed.name jpayne@68: self.url: str | None = parsed.url or None jpayne@68: self.extras: set[str] = set(parsed.extras or []) jpayne@68: self.specifier: SpecifierSet = SpecifierSet(parsed.specifier) jpayne@68: self.marker: Marker | None = None jpayne@68: if parsed.marker is not None: jpayne@68: self.marker = Marker.__new__(Marker) jpayne@68: self.marker._markers = _normalize_extra_values(parsed.marker) jpayne@68: jpayne@68: def _iter_parts(self, name: str) -> Iterator[str]: jpayne@68: yield name jpayne@68: jpayne@68: if self.extras: jpayne@68: formatted_extras = ",".join(sorted(self.extras)) jpayne@68: yield f"[{formatted_extras}]" jpayne@68: jpayne@68: if self.specifier: jpayne@68: yield str(self.specifier) jpayne@68: jpayne@68: if self.url: jpayne@68: yield f"@ {self.url}" jpayne@68: if self.marker: jpayne@68: yield " " jpayne@68: jpayne@68: if self.marker: jpayne@68: yield f"; {self.marker}" jpayne@68: jpayne@68: def __str__(self) -> str: jpayne@68: return "".join(self._iter_parts(self.name)) jpayne@68: jpayne@68: def __repr__(self) -> str: jpayne@68: return f"" jpayne@68: jpayne@68: def __hash__(self) -> int: jpayne@68: return hash( jpayne@68: ( jpayne@68: self.__class__.__name__, jpayne@68: *self._iter_parts(canonicalize_name(self.name)), jpayne@68: ) jpayne@68: ) jpayne@68: jpayne@68: def __eq__(self, other: Any) -> bool: jpayne@68: if not isinstance(other, Requirement): jpayne@68: return NotImplemented jpayne@68: jpayne@68: return ( jpayne@68: canonicalize_name(self.name) == canonicalize_name(other.name) jpayne@68: and self.extras == other.extras jpayne@68: and self.specifier == other.specifier jpayne@68: and self.url == other.url jpayne@68: and self.marker == other.marker jpayne@68: )