From 390b7bf07d4f7b3a547f5933e2cc86b7711c5952 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 28 May 2023 14:21:47 +0800 Subject: [PATCH] sync MCDR --- libs/MCDR/version.py | 60 +++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/libs/MCDR/version.py b/libs/MCDR/version.py index fa29b02..54ce239 100644 --- a/libs/MCDR/version.py +++ b/libs/MCDR/version.py @@ -1,18 +1,17 @@ # 本文件以 GNU Lesser General Public License v3.0(GNU LGPL v3) 开源协议进行授权 (谢谢狐狸写出这么好的MCDR) # 顺便说一句,我把所有的tab都改成了空格,因为我觉得空格比tab更好看(草,后半句是github copilot自动填充的) +""" +This part of code come from MCDReforged(https://github.com/Fallen-Breath/MCDReforged) +Thanks a lot to Fallen_Breath and MCDR contributors +GNU Lesser General Public License v3.0 (GNU LGPL v3) +""" + """ Plugin Version """ import re -from typing import List, Callable, Tuple, Optional - -""" -This part of code come from MCDReforged(https://github.com/Fallen-Breath/MCDReforged) -Very thanks to Fallen_Breath and other coder who helped MCDR worked better -GNU Lesser General Public License v3.0(GNU LGPL v3) -# (hasn't some changes) -""" +from typing import List, Callable, Tuple, Optional, Union # beta.3 -> (beta, 3), random -> (random, None) @@ -43,6 +42,15 @@ class ExtraElement: class Version: + """ + A version container that stores semver like version string + + Example: + + * ``"1.2.3"`` + * ``"1.0.*"`` + * ``"1.2.3-pre4+build.5"`` + """ EXTRA_ID_PATTERN = re.compile(r'|[-+0-9A-Za-z]+(\.[-+0-9A-Za-z]+)*') WILDCARDS = ('*', 'x', 'X') WILDCARD = -1 @@ -52,9 +60,10 @@ class Version: pre: Optional[ExtraElement] build: Optional[ExtraElement] - def __init__(self, version_str: str, allow_wildcard=True): + def __init__(self, version_str: str, *, allow_wildcard: bool = True): """ - :param str version_str: the version str like '1.2.3-pre4+build.5' + :param version_str: The version string to be parsed + :keyword allow_wildcard: If wildcard (``"*"``, ``"x"``, ``"X"``) is allowed. Default: ``True`` """ if not isinstance(version_str, str): raise VersionParsingError('Invalid input version string') @@ -102,10 +111,7 @@ class Version: version_str += '+' + str(self.build) return version_str - def __repr__(self): - return 'Version({})'.format(self.__str__()) - - def __getitem__(self, index): + def __getitem__(self, index: int) -> int: if index < len(self.component): return self.component[index] else: @@ -152,7 +158,7 @@ class Criterion: self.base_version = base_version self.criterion = criterion - def test(self, target: str or Version): + def test(self, target: Union[Version, str]): return self.criterion(self.base_version, target) def __str__(self): @@ -160,6 +166,11 @@ class Criterion: class VersionRequirement: + """ + A version requirement tester + + It can test if a given :class:`Version` object matches its requirement + """ CRITERIONS = { '<=': lambda base, ver: ver <= base, '>=': lambda base, ver: ver >= base, @@ -171,6 +182,10 @@ class VersionRequirement: } def __init__(self, requirements: str): + """ + :param requirements: The requirement string, which contains several version predicates connected by space character. + e.g. ``">=1.0.x"``, ``"^2.9"``, ``">=1.2.0 <1.4.3"`` + """ if not isinstance(requirements, str): raise VersionParsingError('Requirements should be a str, not {}'.format(type(requirements).__name__)) self.criterions = [] # type: List[Criterion] @@ -186,7 +201,7 @@ class VersionRequirement: base_version = requirement self.criterions.append(Criterion(opt, Version(base_version), self.CRITERIONS[opt])) - def accept(self, version: Version or str): + def accept(self, version: Union[Version, str]): if isinstance(version, str): version = Version(version) for criterion in self.criterions: @@ -199,15 +214,4 @@ class VersionRequirement: class VersionParsingError(ValueError): - pass - - -if __name__ == '__main__': - from objprint import op - test_list = ['1.1.1', - '1.0', - '1.11.1.11.1'] - - for a_test in test_list: - version_a = Version(a_test) - op(version_a) + pass \ No newline at end of file