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
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

View File

@ -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.
'''

View File

@ -1,10 +1,10 @@
# MIT licensed
# 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 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