comparison env/lib/python3.9/site-packages/packaging/requirements.py @ 0:4f3585e2f14b draft default tip

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