# 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. """
.. testsetup::
from packaging.version import parse, Version """
import collections import itertools import re from typing import Any, Callable, Optional, SupportsInt, Tuple, Union
from ._structures import Infinity, InfinityType, NegativeInfinity, NegativeInfinityType
# Please keep the duplicated `isinstance` check # in the six comparisons hereunder # unless you find a way to avoid adding overhead function calls. def __lt__(self, other: "_BaseVersion") -> bool: ifnot isinstance(other, _BaseVersion): return NotImplemented
# Deliberately not anchored to the start and end of the string, to make it # easier for 3rd party code to reuse
_VERSION_PATTERN = r"""
v?
(?:
(?:(?P<epoch>[0-9]+)!)? # epoch
(?P<release>[0-9]+(?:\.[0-9]+)*) # release segment
(?P<pre> # pre-release
[-_\.]?
(?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
[-_\.]?
(?P<pre_n>[0-9]+)?
)?
(?P<post> # post release
(?:-(?P<post_n1>[0-9]+))
|
(?:
[-_\.]?
(?P<post_l>post|rev|r)
[-_\.]?
(?P<post_n2>[0-9]+)?
)
)?
(?P<dev> # dev release
[-_\.]?
(?P<dev_l>dev)
[-_\.]?
(?P<dev_n>[0-9]+)?
)?
)
(?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))? # local version """
VERSION_PATTERN = _VERSION_PATTERN """
A string containing the regular expression used to match a valid version.
The pattern isnot anchored at either end, andis intended for embedding in larger
expressions (for example, matching a version number as part of a file name). The
regular expression should be compiled with the ``re.VERBOSE`` and ``re.IGNORECASE``
flags set.
:meta hide-value: """
class Version(_BaseVersion): """This class abstracts handling of a project's versions.
A :class:`Version` instance is comparison aware and can be compared and
sorted using the standard Python interfaces.
def __init__(self, version: str) -> None: """Initialize a Version object.
:param version:
The string representation of a version which will be parsed and normalized
before use.
:raises InvalidVersion: If the ``version`` does not conform to PEP 440 in any way then this
exception will be raised. """
# Validate the version and parse it into pieces
match = self._regex.search(version) ifnot match: raise InvalidVersion(f"Invalid version: '{version}'")
# Store the parsed out pieces of the version
self._version = _Version(
epoch=int(match.group("epoch")) if match.group("epoch") else 0,
release=tuple(int(i) for i in match.group("release").split(".")),
pre=_parse_letter_version(match.group("pre_l"), match.group("pre_n")),
post=_parse_letter_version(
match.group("post_l"), match.group("post_n1") or match.group("post_n2")
),
dev=_parse_letter_version(match.group("dev_l"), match.group("dev_n")),
local=_parse_local_version(match.group("local")),
)
# Generate a key which will be used for sorting
self._key = _cmpkey(
self._version.epoch,
self._version.release,
self._version.pre,
self._version.post,
self._version.dev,
self._version.local,
)
def __repr__(self) -> str: """A representation of the Version that shows all internal state.
Includes trailing zeroes but not the epoch or any pre-release / development /
post-release suffixes. """
_release: Tuple[int, ...] = self._version.release return _release
@property def pre(self) -> Optional[Tuple[str, int]]: """The pre-release segment of the version.
@property def local(self) -> Optional[str]: """The local version segment of the version.
>>> print(Version("1.2.3").local) None
>>> Version("1.2.3+abc").local 'abc' """ if self._version.local: return".".join(str(x) for x in self._version.local) else: returnNone
@property def public(self) -> str: """The public portion of the version.
if letter: # We consider there to be an implicit 0 in a pre-release if there is # not a numeral associated with it. if number isNone:
number = 0
# We normalize any letters to their lower case form
letter = letter.lower()
# We consider some words to be alternate spellings of other words and # in those cases we want to normalize the spellings to our preferred # spelling. if letter == "alpha":
letter = "a" elif letter == "beta":
letter = "b" elif letter in ["c", "pre", "preview"]:
letter = "rc" elif letter in ["rev", "r"]:
letter = "post"
return letter, int(number) ifnot letter and number: # We assume if we are given a number, but we are not given a letter # then this is using the implicit post release syntax (e.g. 1.0-1)
letter = "post"
return letter, int(number)
returnNone
_local_version_separators = re.compile(r"[\._-]")
def _parse_local_version(local: str) -> Optional[LocalType]: """
Takes a string like abc.1.twelve and turns it into ("abc", 1, "twelve"). """ if local isnotNone: return tuple(
part.lower() ifnot part.isdigit() else int(part) for part in _local_version_separators.split(local)
) returnNone
# When we compare a release version, we want to compare it with all of the # trailing zeros removed. So we'll use a reverse the list, drop all the now # leading zeros until we come to something non zero, then take the rest # re-reverse it back into the correct order and make it a tuple and use # that for our sorting key.
_release = tuple(
reversed(list(itertools.dropwhile(lambda x: x == 0, reversed(release))))
)
# We need to "trick" the sorting algorithm to put 1.0.dev0 before 1.0a0. # We'll do this by abusing the pre segment, but we _only_ want to do this # if there is not a pre or a post segment. If we have one of those then # the normal sorting rules will handle this case correctly. if pre isNoneand post isNoneand dev isnotNone:
_pre: PrePostDevType = NegativeInfinity # Versions without a pre-release (except as noted above) should sort after # those with one. elif pre isNone:
_pre = Infinity else:
_pre = pre
# Versions without a post segment should sort before those with one. if post isNone:
_post: PrePostDevType = NegativeInfinity
else:
_post = post
# Versions without a development segment should sort after those with one. if dev isNone:
_dev: PrePostDevType = Infinity
else:
_dev = dev
if local isNone: # Versions without a local segment should sort before those with one.
_local: LocalType = NegativeInfinity else: # Versions with a local segment need that segment parsed to implement # the sorting rules in PEP440. # - Alpha numeric segments sort before numeric segments # - Alpha numeric segments sort lexicographically # - Numeric segments sort numerically # - Shorter versions sort before longer versions when the prefixes # match exactly
_local = tuple(
(i, "") if isinstance(i, int) else (NegativeInfinity, i) for i in local
)
return epoch, _release, _pre, _post, _dev, _local
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.