From a04d6b0fc6372015034836b3581bf2eb6a1b8dea Mon Sep 17 00:00:00 2001 From: Daniel Peukert Date: Wed, 18 Oct 2023 01:59:30 +0200 Subject: [PATCH] implement rich result support for list-based sources --- nvchecker/core.py | 16 ++++++++++------ nvchecker/util.py | 10 ++++++++-- nvchecker_source/bitbucket.py | 9 ++++----- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/nvchecker/core.py b/nvchecker/core.py index ea97e81..89c62f4 100644 --- a/nvchecker/core.py +++ b/nvchecker/core.py @@ -298,30 +298,31 @@ def substitute_version( return version def apply_list_options( - versions: List[str], conf: Entry, -) -> Optional[str]: + versions: List[Union[str, RichResult]], conf: Entry, +) -> Optional[Union[str, RichResult]]: pattern = conf.get('include_regex') if pattern: re_pat = re.compile(pattern) versions = [x for x in versions - if re_pat.fullmatch(x)] + if re_pat.fullmatch(str(x))] pattern = conf.get('exclude_regex') if pattern: re_pat = re.compile(pattern) versions = [x for x in versions - if not re_pat.fullmatch(x)] + if not re_pat.fullmatch(str(x))] ignored = set(conf.get('ignored', '').split()) if ignored: - versions = [x for x in versions if x not in ignored] + versions = [x for x in versions + if str(x) not in ignored] if not versions: return None sort_version_key = sort_version_keys[ conf.get("sort_version_key", "parse_version")] - versions.sort(key=sort_version_key) # type: ignore + versions.sort(key=lambda version: sort_version_key(str(version))) # type: ignore return versions[-1] @@ -342,6 +343,9 @@ def _process_result(r: RawResult) -> Union[Result, Exception]: return version elif isinstance(version, list): version_str = apply_list_options(version, conf) + if isinstance(version_str, RichResult): + url = version_str.url + version_str = version_str.version elif isinstance(version, RichResult): version_str = version.version url = version.url diff --git a/nvchecker/util.py b/nvchecker/util.py index b6463b3..e9f4709 100644 --- a/nvchecker/util.py +++ b/nvchecker/util.py @@ -45,19 +45,25 @@ if sys.version_info[:2] >= (3, 10): class RichResult: version: str url: Optional[str] = None + + def __str__(self): + return self.version else: @dataclass class RichResult: version: str url: Optional[str] = None -VersionResult = Union[None, str, List[str], RichResult, Exception] + def __str__(self): + return self.version + +VersionResult = Union[None, str, RichResult, List[Union[str, RichResult]], Exception] VersionResult.__doc__ = '''The result of a `get_version` check. * `None` - No version found. * `str` - A single version string is found. -* `List[str]` - Multiple version strings are found. :ref:`list options` will be applied. * `RichResult` - A version string with additional information. +* `List[Union[str, RichResult]]` - Multiple version strings with or without additional information are found. :ref:`list options` will be applied. * `Exception` - An error occurred. ''' diff --git a/nvchecker_source/bitbucket.py b/nvchecker_source/bitbucket.py index 6039454..211ff04 100644 --- a/nvchecker_source/bitbucket.py +++ b/nvchecker_source/bitbucket.py @@ -1,10 +1,10 @@ # MIT licensed # Copyright (c) 2013-2020 lilydjwg , et al. -from typing import Any, List +from typing import Any, List, Union from urllib.parse import urlencode -from nvchecker.api import VersionResult, Entry, AsyncCache +from nvchecker.api import VersionResult, RichResult, Entry, AsyncCache # doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commits-get BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s' @@ -54,8 +54,8 @@ async def _get_tags( url: str, *, max_page: int, cache: AsyncCache, -) -> List[str]: - ret: List[str] = [] +) -> VersionResult: + ret: List[Union[str, RichResult]] = [] for _ in range(max_page): data = await cache.get_json(url) @@ -66,4 +66,3 @@ async def _get_tags( break return ret -