comparison env/lib/python3.7/site-packages/packaging/requirements.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
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 __future__ import absolute_import, division, print_function
5
6 import string
7 import re
8
9 from pyparsing import stringStart, stringEnd, originalTextFor, ParseException
10 from pyparsing import ZeroOrMore, Word, Optional, Regex, Combine
11 from pyparsing import Literal as L # noqa
12 from six.moves.urllib import parse as urlparse
13
14 from ._typing import MYPY_CHECK_RUNNING
15 from .markers import MARKER_EXPR, Marker
16 from .specifiers import LegacySpecifier, Specifier, SpecifierSet
17
18 if MYPY_CHECK_RUNNING: # pragma: no cover
19 from typing import List
20
21
22 class InvalidRequirement(ValueError):
23 """
24 An invalid requirement was found, users should refer to PEP 508.
25 """
26
27
28 ALPHANUM = Word(string.ascii_letters + string.digits)
29
30 LBRACKET = L("[").suppress()
31 RBRACKET = L("]").suppress()
32 LPAREN = L("(").suppress()
33 RPAREN = L(")").suppress()
34 COMMA = L(",").suppress()
35 SEMICOLON = L(";").suppress()
36 AT = L("@").suppress()
37
38 PUNCTUATION = Word("-_.")
39 IDENTIFIER_END = ALPHANUM | (ZeroOrMore(PUNCTUATION) + ALPHANUM)
40 IDENTIFIER = Combine(ALPHANUM + ZeroOrMore(IDENTIFIER_END))
41
42 NAME = IDENTIFIER("name")
43 EXTRA = IDENTIFIER
44
45 URI = Regex(r"[^ ]+")("url")
46 URL = AT + URI
47
48 EXTRAS_LIST = EXTRA + ZeroOrMore(COMMA + EXTRA)
49 EXTRAS = (LBRACKET + Optional(EXTRAS_LIST) + RBRACKET)("extras")
50
51 VERSION_PEP440 = Regex(Specifier._regex_str, re.VERBOSE | re.IGNORECASE)
52 VERSION_LEGACY = Regex(LegacySpecifier._regex_str, re.VERBOSE | re.IGNORECASE)
53
54 VERSION_ONE = VERSION_PEP440 ^ VERSION_LEGACY
55 VERSION_MANY = Combine(
56 VERSION_ONE + ZeroOrMore(COMMA + VERSION_ONE), joinString=",", adjacent=False
57 )("_raw_spec")
58 _VERSION_SPEC = Optional(((LPAREN + VERSION_MANY + RPAREN) | VERSION_MANY))
59 _VERSION_SPEC.setParseAction(lambda s, l, t: t._raw_spec or "")
60
61 VERSION_SPEC = originalTextFor(_VERSION_SPEC)("specifier")
62 VERSION_SPEC.setParseAction(lambda s, l, t: t[1])
63
64 MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker")
65 MARKER_EXPR.setParseAction(
66 lambda s, l, t: Marker(s[t._original_start : t._original_end])
67 )
68 MARKER_SEPARATOR = SEMICOLON
69 MARKER = MARKER_SEPARATOR + MARKER_EXPR
70
71 VERSION_AND_MARKER = VERSION_SPEC + Optional(MARKER)
72 URL_AND_MARKER = URL + Optional(MARKER)
73
74 NAMED_REQUIREMENT = NAME + Optional(EXTRAS) + (URL_AND_MARKER | VERSION_AND_MARKER)
75
76 REQUIREMENT = stringStart + NAMED_REQUIREMENT + stringEnd
77 # pyparsing isn't thread safe during initialization, so we do it eagerly, see
78 # issue #104
79 REQUIREMENT.parseString("x[]")
80
81
82 class Requirement(object):
83 """Parse a requirement.
84
85 Parse a given requirement string into its parts, such as name, specifier,
86 URL, and extras. Raises InvalidRequirement on a badly-formed requirement
87 string.
88 """
89
90 # TODO: Can we test whether something is contained within a requirement?
91 # If so how do we do that? Do we need to test against the _name_ of
92 # the thing as well as the version? What about the markers?
93 # TODO: Can we normalize the name and extra name?
94
95 def __init__(self, requirement_string):
96 # type: (str) -> None
97 try:
98 req = REQUIREMENT.parseString(requirement_string)
99 except ParseException as e:
100 raise InvalidRequirement(
101 'Parse error at "{0!r}": {1}'.format(
102 requirement_string[e.loc : e.loc + 8], e.msg
103 )
104 )
105
106 self.name = req.name
107 if req.url:
108 parsed_url = urlparse.urlparse(req.url)
109 if parsed_url.scheme == "file":
110 if urlparse.urlunparse(parsed_url) != req.url:
111 raise InvalidRequirement("Invalid URL given")
112 elif not (parsed_url.scheme and parsed_url.netloc) or (
113 not parsed_url.scheme and not parsed_url.netloc
114 ):
115 raise InvalidRequirement("Invalid URL: {0}".format(req.url))
116 self.url = req.url
117 else:
118 self.url = None
119 self.extras = set(req.extras.asList() if req.extras else [])
120 self.specifier = SpecifierSet(req.specifier)
121 self.marker = req.marker if req.marker else None
122
123 def __str__(self):
124 # type: () -> str
125 parts = [self.name] # type: List[str]
126
127 if self.extras:
128 parts.append("[{0}]".format(",".join(sorted(self.extras))))
129
130 if self.specifier:
131 parts.append(str(self.specifier))
132
133 if self.url:
134 parts.append("@ {0}".format(self.url))
135 if self.marker:
136 parts.append(" ")
137
138 if self.marker:
139 parts.append("; {0}".format(self.marker))
140
141 return "".join(parts)
142
143 def __repr__(self):
144 # type: () -> str
145 return "<Requirement({0!r})>".format(str(self))