Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pkg_resources/_vendor/packaging/requirements.py: 87%
61 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
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.
4from __future__ import absolute_import, division, print_function
6import string
7import re
9from pkg_resources.extern.pyparsing import stringStart, stringEnd, originalTextFor, ParseException
10from pkg_resources.extern.pyparsing import ZeroOrMore, Word, Optional, Regex, Combine
11from pkg_resources.extern.pyparsing import Literal as L # noqa
12from pkg_resources.extern.six.moves.urllib import parse as urlparse
14from .markers import MARKER_EXPR, Marker
15from .specifiers import LegacySpecifier, Specifier, SpecifierSet
18class InvalidRequirement(ValueError):
19 """
20 An invalid requirement was found, users should refer to PEP 508.
21 """
24ALPHANUM = Word(string.ascii_letters + string.digits)
26LBRACKET = L("[").suppress()
27RBRACKET = L("]").suppress()
28LPAREN = L("(").suppress()
29RPAREN = L(")").suppress()
30COMMA = L(",").suppress()
31SEMICOLON = L(";").suppress()
32AT = L("@").suppress()
34PUNCTUATION = Word("-_.")
35IDENTIFIER_END = ALPHANUM | (ZeroOrMore(PUNCTUATION) + ALPHANUM)
36IDENTIFIER = Combine(ALPHANUM + ZeroOrMore(IDENTIFIER_END))
38NAME = IDENTIFIER("name")
39EXTRA = IDENTIFIER
41URI = Regex(r'[^ ]+')("url")
42URL = (AT + URI)
44EXTRAS_LIST = EXTRA + ZeroOrMore(COMMA + EXTRA)
45EXTRAS = (LBRACKET + Optional(EXTRAS_LIST) + RBRACKET)("extras")
47VERSION_PEP440 = Regex(Specifier._regex_str, re.VERBOSE | re.IGNORECASE)
48VERSION_LEGACY = Regex(LegacySpecifier._regex_str, re.VERBOSE | re.IGNORECASE)
50VERSION_ONE = VERSION_PEP440 ^ VERSION_LEGACY
51VERSION_MANY = Combine(VERSION_ONE + ZeroOrMore(COMMA + VERSION_ONE),
52 joinString=",", adjacent=False)("_raw_spec")
53_VERSION_SPEC = Optional(((LPAREN + VERSION_MANY + RPAREN) | VERSION_MANY))
54_VERSION_SPEC.setParseAction(lambda s, l, t: t._raw_spec or '')
56VERSION_SPEC = originalTextFor(_VERSION_SPEC)("specifier")
57VERSION_SPEC.setParseAction(lambda s, l, t: t[1])
59MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker")
60MARKER_EXPR.setParseAction(
61 lambda s, l, t: Marker(s[t._original_start:t._original_end])
62)
63MARKER_SEPERATOR = SEMICOLON
64MARKER = MARKER_SEPERATOR + MARKER_EXPR
66VERSION_AND_MARKER = VERSION_SPEC + Optional(MARKER)
67URL_AND_MARKER = URL + Optional(MARKER)
69NAMED_REQUIREMENT = \
70 NAME + Optional(EXTRAS) + (URL_AND_MARKER | VERSION_AND_MARKER)
72REQUIREMENT = stringStart + NAMED_REQUIREMENT + stringEnd
75class Requirement(object):
76 """Parse a requirement.
78 Parse a given requirement string into its parts, such as name, specifier,
79 URL, and extras. Raises InvalidRequirement on a badly-formed requirement
80 string.
81 """
83 # TODO: Can we test whether something is contained within a requirement?
84 # If so how do we do that? Do we need to test against the _name_ of
85 # the thing as well as the version? What about the markers?
86 # TODO: Can we normalize the name and extra name?
88 def __init__(self, requirement_string):
89 try:
90 req = REQUIREMENT.parseString(requirement_string)
91 except ParseException as e:
92 raise InvalidRequirement(
93 "Invalid requirement, parse error at \"{0!r}\"".format(
94 requirement_string[e.loc:e.loc + 8]))
96 self.name = req.name
97 if req.url: 97 ↛ 98line 97 didn't jump to line 98, because the condition on line 97 was never true
98 parsed_url = urlparse.urlparse(req.url)
99 if not (parsed_url.scheme and parsed_url.netloc) or (
100 not parsed_url.scheme and not parsed_url.netloc):
101 raise InvalidRequirement("Invalid URL given")
102 self.url = req.url
103 else:
104 self.url = None
105 self.extras = set(req.extras.asList() if req.extras else [])
106 self.specifier = SpecifierSet(req.specifier)
107 self.marker = req.marker if req.marker else None
109 def __str__(self):
110 parts = [self.name]
112 if self.extras:
113 parts.append("[{0}]".format(",".join(sorted(self.extras))))
115 if self.specifier:
116 parts.append(str(self.specifier))
118 if self.url:
119 parts.append("@ {0}".format(self.url))
121 if self.marker:
122 parts.append("; {0}".format(self.marker))
124 return "".join(parts)
126 def __repr__(self):
127 return "<Requirement({0!r})>".format(str(self))