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