# This file is dual licensed under the terms of the Apache License, Version # 2.0, and the BSD License. See the LICENSE file in the root of this repository # for complete details. from __future__ import absolute_import, division, print_function import string import re from pip._vendor.pyparsing import stringStart, stringEnd, originalTextFor, ParseException from pip._vendor.pyparsing import ZeroOrMore, Word, Optional, Regex, Combine from pip._vendor.pyparsing import Literal as L # noqa from pip._vendor.six.moves.urllib import parse as urlparse from ._typing import TYPE_CHECKING from .markers import MARKER_EXPR, Marker from .specifiers import LegacySpecifier, Specifier, SpecifierSet if TYPE_CHECKING: # pragma: no cover from typing import List class InvalidRequirement(ValueError): """ An invalid requirement was found, users should refer to PEP 508. """ ALPHANUM = Word(string.ascii_letters + string.digits) LBRACKET = L("[").suppress() RBRACKET = L("]").suppress() LPAREN = L("(").suppress() RPAREN = L(")").suppress() COMMA = L(",").suppress() SEMICOLON = L(";").suppress() AT = L("@").suppress() PUNCTUATION = Word("-_.") IDENTIFIER_END = ALPHANUM | (ZeroOrMore(PUNCTUATION) + ALPHANUM) IDENTIFIER = Combine(ALPHANUM + ZeroOrMore(IDENTIFIER_END)) NAME = IDENTIFIER("name") EXTRA = IDENTIFIER URI = Regex(r"[^ ]+")("url") URL = AT + URI EXTRAS_LIST = EXTRA + ZeroOrMore(COMMA + EXTRA) EXTRAS = (LBRACKET + Optional(EXTRAS_LIST) + RBRACKET)("extras") VERSION_PEP440 = Regex(Specifier._regex_str, re.VERBOSE | re.IGNORECASE) VERSION_LEGACY = Regex(LegacySpecifier._regex_str, re.VERBOSE | re.IGNORECASE) VERSION_ONE = VERSION_PEP440 ^ VERSION_LEGACY VERSION_MANY = Combine( VERSION_ONE + ZeroOrMore(COMMA + VERSION_ONE), joinString=",", adjacent=False )("_raw_spec") _VERSION_SPEC = Optional(((LPAREN + VERSION_MANY + RPAREN) | VERSION_MANY)) _VERSION_SPEC.setParseAction(lambda s, l, t: t._raw_spec or "") VERSION_SPEC = originalTextFor(_VERSION_SPEC)("specifier") VERSION_SPEC.setParseAction(lambda s, l, t: t[1]) MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker") MARKER_EXPR.setParseAction( lambda s, l, t: Marker(s[t._original_start : t._original_end]) ) MARKER_SEPARATOR = SEMICOLON MARKER = MARKER_SEPARATOR + MARKER_EXPR VERSION_AND_MARKER = VERSION_SPEC + Optional(MARKER) URL_AND_MARKER = URL + Optional(MARKER) NAMED_REQUIREMENT = NAME + Optional(EXTRAS) + (URL_AND_MARKER | VERSION_AND_MARKER) REQUIREMENT = stringStart + NAMED_REQUIREMENT + stringEnd # pyparsing isn't thread safe during initialization, so we do it eagerly, see # issue #104 REQUIREMENT.parseString("x[]") class Requirement(object): """Parse a requirement. Parse a given requirement string into its parts, such as name, specifier, URL, and extras. Raises InvalidRequirement on a badly-formed requirement string. """ # TODO: Can we test whether something is contained within a requirement? # If so how do we do that? Do we need to test against the _name_ of # the thing as well as the version? What about the markers? # TODO: Can we normalize the name and extra name? def __init__(self, requirement_string): # type: (str) -> None try: req = REQUIREMENT.parseString(requirement_string) except ParseException as e: raise InvalidRequirement( 'Parse error at "{0!r}": {1}'.format( requirement_string[e.loc : e.loc + 8], e.msg ) ) self.name = req.name if req.url: parsed_url = urlparse.urlparse(req.url) if parsed_url.scheme == "file": if urlparse.urlunparse(parsed_url) != req.url: raise InvalidRequirement("Invalid URL given") elif not (parsed_url.scheme and parsed_url.netloc) or ( not parsed_url.scheme and not parsed_url.netloc ): raise InvalidRequirement("Invalid URL: {0}".format(req.url)) self.url = req.url else: self.url = None self.extras = set(req.extras.asList() if req.extras else []) self.specifier = SpecifierSet(req.specifier) self.marker = req.marker if req.marker else None def __str__(self): # type: () -> str parts = [self.name] # type: List[str] if self.extras: parts.append("[{0}]".format(",".join(sorted(self.extras)))) if self.specifier: parts.append(str(self.specifier)) if self.url: parts.append("@ {0}".format(self.url)) if self.marker: parts.append(" ") if self.marker: parts.append("; {0}".format(self.marker)) return "".join(parts) def __repr__(self): # type: () -> str return "<Requirement({0!r})>".format(str(self))
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
.__about__.pyo.40009 | File | 799 B | 0644 |
|
.__init__.pyo.40009 | File | 629 B | 0644 |
|
._compat.pyo.40009 | File | 1.42 KB | 0644 |
|
._structures.pyo.40009 | File | 4.24 KB | 0644 |
|
._typing.pyo.40009 | File | 1.6 KB | 0644 |
|
.requirements.pyo.40009 | File | 5.38 KB | 0644 |
|
.specifiers.pyo.40009 | File | 26.07 KB | 0644 |
|
.utils.pyo.40009 | File | 2.18 KB | 0644 |
|
.version.pyo.40009 | File | 18.13 KB | 0644 |
|
__about__.py | File | 736 B | 0644 |
|
__about__.pyc | File | 799 B | 0644 |
|
__about__.pyo | File | 799 B | 0644 |
|
__init__.py | File | 562 B | 0644 |
|
__init__.pyc | File | 629 B | 0644 |
|
__init__.pyo | File | 629 B | 0644 |
|
_compat.py | File | 1.1 KB | 0644 |
|
_compat.pyc | File | 1.42 KB | 0644 |
|
_compat.pyo | File | 1.42 KB | 0644 |
|
_structures.py | File | 1.97 KB | 0644 |
|
_structures.pyc | File | 4.24 KB | 0644 |
|
_structures.pyo | File | 4.24 KB | 0644 |
|
_typing.py | File | 1.78 KB | 0644 |
|
_typing.pyc | File | 1.6 KB | 0644 |
|
_typing.pyo | File | 1.6 KB | 0644 |
|
markers.py | File | 9.27 KB | 0644 |
|
markers.pyc | File | 12.43 KB | 0644 |
|
markers.pyo | File | 12.3 KB | 0644 |
|
requirements.py | File | 4.79 KB | 0644 |
|
requirements.pyc | File | 5.38 KB | 0644 |
|
requirements.pyo | File | 5.38 KB | 0644 |
|
specifiers.py | File | 31.2 KB | 0644 |
|
specifiers.pyc | File | 26.07 KB | 0644 |
|
specifiers.pyo | File | 26.07 KB | 0644 |
|
tags.py | File | 23.5 KB | 0644 |
|
tags.pyc | File | 22.5 KB | 0644 |
|
tags.pyo | File | 22.48 KB | 0644 |
|
utils.py | File | 1.77 KB | 0644 |
|
utils.pyc | File | 2.18 KB | 0644 |
|
utils.pyo | File | 2.18 KB | 0644 |
|
version.py | File | 15.11 KB | 0644 |
|
version.pyc | File | 18.13 KB | 0644 |
|
version.pyo | File | 18.13 KB | 0644 |
|