Skip to content

Commit

Permalink
feat: update version number to comply with PEP-440 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
cntvc authored Jul 17, 2024
1 parent b8808bd commit 514a807
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion star_rail/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from star_rail.utils.version import get_version

VERSION = (2, 2, 7)
VERSION = (2, 2, 7, "final", 0)


__author__ = "cntvc"
Expand Down
2 changes: 1 addition & 1 deletion star_rail/module/record/srgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
51 changes: 47 additions & 4 deletions star_rail/utils/version.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
7 changes: 7 additions & 0 deletions tests/utils/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 514a807

Please sign in to comment.