diff --git a/star_rail/__init__.py b/star_rail/__init__.py index 5a1d580..f3092be 100644 --- a/star_rail/__init__.py +++ b/star_rail/__init__.py @@ -1,6 +1,6 @@ from star_rail.utils.version import get_version -VERSION = (2, 2, 7) +VERSION = (2, 2, 7, "final", 0) __author__ = "cntvc" diff --git a/star_rail/module/record/srgf.py b/star_rail/module/record/srgf.py index d12824f..fecb803 100644 --- a/star_rail/module/record/srgf.py +++ b/star_rail/module/record/srgf.py @@ -17,7 +17,7 @@ __all__ = ["convert_to_gacha_record_data", "convert_to_srgf"] -SRGF_VERSION = (1, 0) +SRGF_VERSION = (1, 0, 0, "final", 0) def get_srgf_version(srgf_version): diff --git a/star_rail/utils/version.py b/star_rail/utils/version.py index b761722..35a8bd1 100644 --- a/star_rail/utils/version.py +++ b/star_rail/utils/version.py @@ -1,13 +1,44 @@ +import re from itertools import zip_longest __all__ = ["get_version", "compare_versions", "get_version_tuple"] -def get_version(version: tuple[int, ...] = None): - """(1, 0, 0) -> '1.0.0'""" +def get_version(version: tuple[int, int, int, str, int] = None): + """Return a PEP 440-compliant version number from VERSION.""" + + version = get_complete_version(version) + + # Build the two parts of the version number: + # main = X.Y[.Z] + # sub = [.devN] - for dev releases + + main = get_main_version(version) + + sub = "" + if version[3] == "dev": + sub = ".dev" + str(version[4]) + + return main + sub + + +def get_main_version(version: tuple[int, int, int, str, int] = None): + """Return main version (X.Y[.Z]) from VERSION.""" + version = get_complete_version(version) + parts = 2 if version[2] == 0 else 3 + return ".".join(str(x) for x in version[:parts]) + + +def get_complete_version(version: tuple[int, int, int, str, int] = None): + """ + check for correctness of the tuple provided. + """ if version is None: from star_rail import VERSION as version - return ".".join(str(x) for x in version) + assert len(version) == 5 + assert version[3] in ("dev", "final") + + return version def compare_versions(version1: str, version2: str): @@ -28,8 +59,20 @@ def compare_versions(version1: str, version2: str): return 0 +version_component_re = re.compile(r"(\d+|[a-z]+|\.)") + + def get_version_tuple(version: str) -> tuple[int, ...]: """ Return a tuple of version numbers (e.g. (1, 2, 3)) from the version string (e.g. '1.2.3'). """ - return tuple(map(int, version.split("."))) + numbers = [] + for item in version_component_re.split(version): + if item and item != ".": + try: + components = int(item) + except ValueError: + break + else: + numbers.append(components) + return tuple(numbers) diff --git a/tests/utils/test_version.py b/tests/utils/test_version.py index aa60f8a..9e84060 100644 --- a/tests/utils/test_version.py +++ b/tests/utils/test_version.py @@ -9,12 +9,19 @@ def test_compare_version(self): self.assertEqual(compare_versions("1.0.0", "1.0.11"), -1) self.assertEqual(compare_versions("1.0.0", "1.10.0"), -1) self.assertEqual(compare_versions("1.0.0", "11.0.11"), -1) + self.assertEqual(compare_versions("1.0.0", "1.1.0.dev1"), -1) + self.assertEqual(compare_versions("1.0.0dev1", "1.1.0.dev1"), -1) self.assertEqual(compare_versions("1.0.0", "1.0.0"), 0) self.assertEqual(compare_versions("11.0.0", "11.0.0"), 0) self.assertEqual(compare_versions("1.10.120", "1.10.120"), 0) + self.assertEqual(compare_versions("1.1.1", "1.1.1dev1"), 0) + self.assertEqual(compare_versions("1.1.1dev2", "1.1.1dev2"), 0) self.assertEqual(compare_versions("0.0.2", "0.0.1"), 1) self.assertEqual(compare_versions("0.10.0", "0.0.1"), 1) self.assertEqual(compare_versions("10.0.0", "0.0.1"), 1) self.assertEqual(compare_versions("100.10.2", "100.10.1"), 1) + self.assertEqual(compare_versions("100.10.2", "100.10.1"), 1) + self.assertEqual(compare_versions("1.1.1dev1", "1.1.0dev1"), 1) + self.assertEqual(compare_versions("1.1.1dev3", "1.1.0dev1"), 1)