# -*- coding: ascii -*- # # Copyright 2018 - 2025 # Andr\xe9 Malo or his licensors, as applicable # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Regular expressions ~~~~~~~~~~~~~~~~~~~ :See: https://www.python.org/dev/peps/pep-0508/ """ import functools as _ft import re as _re # pylint: disable = import-outside-toplevel class regex(object): """Collection of regexes""" wsp = r"[^\S\r\n]" idre = r"([a-zA-Z\d]+(?:[._-]+[a-zA-Z\d]+)*)" extra = r"""(?: \[ %(wsp)s* %(idre)s+ %(wsp)s* (?:,%(wsp)s* %(idre)s %(wsp)s*)* \] )""" % dict( idre=idre, wsp=wsp ) version_cmp = r"(?:===|[~]=|[<>])" version_str = r"[a-zA-Z0-9_.*+!-]+" version_release = r"""(?x)(?: ([vV]? (?:[0-9]+!)?) ([0-9]+(?:\.[0-9]+)*) )""" version1 = r"(?: %(cmp)s %(wsp)s* %(version)s )" % dict( cmp=version_cmp, wsp=wsp, version=version_str ) version = r"""(?: %(version1)s %(wsp)s* (?:, %(wsp)s* %(version1)s %(wsp)s* )* )""" % dict( version1=version1, wsp=wsp ) marker = r"""(?: (?: ; %(wsp)s* (?: [a-zA-Z0-9"'<>=~!().{}_*#\\:,/?[\]`@$%%^&+|-]+ %(wsp)s* )+ )+ )""" % dict( wsp=wsp ) # the # character is enclosed in a character class, so it can be used # with re.X line_comment = r"(?:[#][^\r\n]*)" py_str_double = r'(?:"[^"\\\r\n]*(?:\\(?:[^\r\n]|\r?\n|\r)[^"\\\r\n]*)*")' py_mstr_double = r'(?:"""[^"\\]*(?:(?:\\.|""?[^"])[^"\\]*)*""")' py_str_single = r"(?:'[^'\\\r\n]*(?:\\(?:[^\r\n]|\r?\n|\r)[^'\\\r\n]*)*')" py_mstr_single = r"(?:'''[^'\\]*(?:(?:\\.|''?[^'])[^'\\]*)*''')" toml_str_double = py_str_double toml_mstr_double = ( r'(?:"""[^"\\]*(?:(?:\\.|""(?!"""")|"(?!"""""))[^"\\]*)*""")' ) toml_str_single = r"(?:'[^'\r\n]*(?:[^'\r\n]*)*')" toml_mstr_single = r"(?:'''[^']*(?:''?[^'][^']*)*''')" @classmethod def full_spec(cls, name=None, require_version=False): """ Full version spec regex Parameters: name (str): The package name to match against. If omitted or ``None`` all valid names are matched require_version (bool): Require the version to be present? Returns: str: The regex """ return r"""(?xm) (?P %(wsp)s* ) (?P (?P %(name)s ) (?P (?: %(wsp)s* %(extra)s ) ? ) (?P (?: %(wsp)s* %(version)s ) %(opt_version)s ) (?P (?: %(wsp)s* %(marker)s ) ? ) ) """ % dict( wsp=cls.wsp, name=cls.idre if name is None else name_as_regex(name), extra=cls.extra, version=cls.version, opt_version="" if require_version else "?", marker=cls.marker, ) def name_as_regex(name): """ Transform package name into a regex Letters will be matched case insensitively and interpunction chars will be matched as a sequence. >>> name_as_regex("foo-b4r") '(?