implement rich result support for list-based sources

This commit is contained in:
Daniel Peukert 2023-10-18 01:59:30 +02:00
parent 5dcb3bc36a
commit a04d6b0fc6
No known key found for this signature in database
3 changed files with 22 additions and 13 deletions

View file

@ -298,30 +298,31 @@ def substitute_version(
return version return version
def apply_list_options( def apply_list_options(
versions: List[str], conf: Entry, versions: List[Union[str, RichResult]], conf: Entry,
) -> Optional[str]: ) -> Optional[Union[str, RichResult]]:
pattern = conf.get('include_regex') pattern = conf.get('include_regex')
if pattern: if pattern:
re_pat = re.compile(pattern) re_pat = re.compile(pattern)
versions = [x for x in versions versions = [x for x in versions
if re_pat.fullmatch(x)] if re_pat.fullmatch(str(x))]
pattern = conf.get('exclude_regex') pattern = conf.get('exclude_regex')
if pattern: if pattern:
re_pat = re.compile(pattern) re_pat = re.compile(pattern)
versions = [x for x in versions 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()) ignored = set(conf.get('ignored', '').split())
if ignored: 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: if not versions:
return None return None
sort_version_key = sort_version_keys[ sort_version_key = sort_version_keys[
conf.get("sort_version_key", "parse_version")] 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] return versions[-1]
@ -342,6 +343,9 @@ def _process_result(r: RawResult) -> Union[Result, Exception]:
return version return version
elif isinstance(version, list): elif isinstance(version, list):
version_str = apply_list_options(version, conf) 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): elif isinstance(version, RichResult):
version_str = version.version version_str = version.version
url = version.url url = version.url

View file

@ -45,19 +45,25 @@ if sys.version_info[:2] >= (3, 10):
class RichResult: class RichResult:
version: str version: str
url: Optional[str] = None url: Optional[str] = None
def __str__(self):
return self.version
else: else:
@dataclass @dataclass
class RichResult: class RichResult:
version: str version: str
url: Optional[str] = None 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. VersionResult.__doc__ = '''The result of a `get_version` check.
* `None` - No version found. * `None` - No version found.
* `str` - A single version string is 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. * `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. * `Exception` - An error occurred.
''' '''

View file

@ -1,10 +1,10 @@
# MIT licensed # MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
from typing import Any, List from typing import Any, List, Union
from urllib.parse import urlencode 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 # 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' BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s'
@ -54,8 +54,8 @@ async def _get_tags(
url: str, *, url: str, *,
max_page: int, max_page: int,
cache: AsyncCache, cache: AsyncCache,
) -> List[str]: ) -> VersionResult:
ret: List[str] = [] ret: List[Union[str, RichResult]] = []
for _ in range(max_page): for _ in range(max_page):
data = await cache.get_json(url) data = await cache.get_json(url)
@ -66,4 +66,3 @@ async def _get_tags(
break break
return ret return ret