support using prefix and from_pattern/to_pattern at the same time

closes #249.
This commit is contained in:
lilydjwg 2024-02-27 13:50:53 +08:00
parent c944cbcac3
commit 1e5de6cbcb
4 changed files with 11 additions and 17 deletions

View File

@ -170,6 +170,9 @@ prefix
Strip the prefix string if the version string starts with it. Otherwise the Strip the prefix string if the version string starts with it. Otherwise the
version string is returned as-is. version string is returned as-is.
If both ``prefix`` and ``from_pattern``/``to_pattern`` are used, ``prefix``
is applied first.
from_pattern, to_pattern from_pattern, to_pattern
Both are Python-compatible regular expressions. If ``from_pattern`` is found Both are Python-compatible regular expressions. If ``from_pattern`` is found
in the version string, it will be replaced with ``to_pattern``. in the version string, it will be replaced with ``to_pattern``.
@ -212,13 +215,6 @@ httptoken
verify_cert verify_cert
Whether to verify the HTTPS certificate or not. Default is ``true``. 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:
List Options List Options

View File

@ -1,4 +1,4 @@
# MIT licensed # MIT licensed
# Copyright (c) 2013-2023 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2023 lilydjwg <lilydjwg@gmail.com>, et al.
__version__ = '2.13.1' __version__ = '2.14dev'

View File

@ -279,13 +279,12 @@ def substitute_version(
) -> str: ) -> str:
''' '''
Substitute the version string via defined rules in the configuration file. 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') prefix = conf.get('prefix')
if prefix: if prefix:
if version.startswith(prefix): if version.startswith(prefix):
version = version[len(prefix):] version = version[len(prefix):]
return version
from_pattern = conf.get('from_pattern') from_pattern = conf.get('from_pattern')
if from_pattern: if from_pattern:
@ -293,9 +292,8 @@ def substitute_version(
if to_pattern is None: if to_pattern is None:
raise ValueError("from_pattern exists but to_pattern doesn't") 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 return version
def apply_list_options( def apply_list_options(

View File

@ -45,8 +45,8 @@ async def test_substitute_regex_empty_to_pattern(get_version):
async def test_substitute_prefix_has_higher_priority(get_version): async def test_substitute_prefix_has_higher_priority(get_version):
assert await get_version("example", { assert await get_version("example", {
"source": "manual", "source": "manual",
"manual": "r15", "manual": "Version 1.2 Beta 3",
"prefix": "r", "prefix": "Version ",
"from_pattern": r"r(\d+)", "from_pattern": r" Beta ",
"to_pattern": r"R\1", "to_pattern": r"b",
}) == "15" }) == "1.2b3"