From 1e5de6cbcb21e280878bee3fa41322ba8ef7103b Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 27 Feb 2024 13:50:53 +0800 Subject: [PATCH] support using prefix and from_pattern/to_pattern at the same time closes #249. --- docs/usage.rst | 10 +++------- nvchecker/__init__.py | 2 +- nvchecker/core.py | 6 ++---- tests/test_substitute.py | 10 +++++----- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index c8e4701..e892432 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -170,6 +170,9 @@ prefix Strip the prefix string if the version string starts with it. Otherwise the version string is returned as-is. + If both ``prefix`` and ``from_pattern``/``to_pattern`` are used, ``prefix`` + is applied first. + from_pattern, to_pattern Both are Python-compatible regular expressions. If ``from_pattern`` is found in the version string, it will be replaced with ``to_pattern``. @@ -212,13 +215,6 @@ httptoken verify_cert Whether to verify the HTTPS certificate or not. Default is ``true``. -If both ``prefix`` and ``from_pattern``/``to_pattern`` are used, -``from_pattern``/``to_pattern`` are ignored. If you want to strip the prefix -and then do something special, just use ``from_pattern``/``to_pattern``. For -example, the transformation of ``v1_1_0`` => ``1.1.0`` can be achieved with -``from_pattern = 'v(\d+)_(\d+)_(\d+)'`` and ``to_pattern = '\1.\2.\3'``. -(Note that in TOML it's easiler to write regexes in single quotes so you don't need to escape ``\``.) - .. _list options: List Options diff --git a/nvchecker/__init__.py b/nvchecker/__init__.py index b561382..3400a6a 100644 --- a/nvchecker/__init__.py +++ b/nvchecker/__init__.py @@ -1,4 +1,4 @@ # MIT licensed # Copyright (c) 2013-2023 lilydjwg , et al. -__version__ = '2.13.1' +__version__ = '2.14dev' diff --git a/nvchecker/core.py b/nvchecker/core.py index 973e5f7..330fb2d 100644 --- a/nvchecker/core.py +++ b/nvchecker/core.py @@ -279,13 +279,12 @@ def substitute_version( ) -> str: ''' Substitute the version string via defined rules in the configuration file. - See README.rst#global-options for details. + See usage.rst#global-options for details. ''' prefix = conf.get('prefix') if prefix: if version.startswith(prefix): version = version[len(prefix):] - return version from_pattern = conf.get('from_pattern') if from_pattern: @@ -293,9 +292,8 @@ def substitute_version( if to_pattern is None: raise ValueError("from_pattern exists but to_pattern doesn't") - return re.sub(from_pattern, to_pattern, version) + version = re.sub(from_pattern, to_pattern, version) - # No substitution rules found. Just return the original version string. return version def apply_list_options( diff --git a/tests/test_substitute.py b/tests/test_substitute.py index cf5c212..1cdf9c6 100644 --- a/tests/test_substitute.py +++ b/tests/test_substitute.py @@ -45,8 +45,8 @@ async def test_substitute_regex_empty_to_pattern(get_version): async def test_substitute_prefix_has_higher_priority(get_version): assert await get_version("example", { "source": "manual", - "manual": "r15", - "prefix": "r", - "from_pattern": r"r(\d+)", - "to_pattern": r"R\1", - }) == "15" + "manual": "Version 1.2 Beta 3", + "prefix": "Version ", + "from_pattern": r" Beta ", + "to_pattern": r"b", + }) == "1.2b3"