sync MCDR

This commit is contained in:
shenjack 2023-05-28 14:21:47 +08:00
parent 27c9efede6
commit 390b7bf07d

View File

@ -1,18 +1,17 @@
# 本文件以 GNU Lesser General Public License v3.0GNU 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.0GNU 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:
@ -200,14 +215,3 @@ 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)