nvchecker/nvchecker_source/regex.py
Chih-Hsuan Yen 9acae25c0f
regex: make sure regex fails if nothing is found
This is a regression since 2019 [1].
Before that commit, max() raises ValuError if re.findall returns an
empty list. After that commit, regex fails silently if nothing is found.

[1] 7897317294 (diff-fd40f1295f9ddece86df8d2c385ddb02bd4e2a10cd20bd9a95306d3d35a9b601)
2021-08-31 20:07:06 +08:00

29 lines
773 B
Python

# MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
import re
import sre_constants
from nvchecker.api import session, GetVersionError
async def get_version(name, conf, *, cache, **kwargs):
key = tuple(sorted(conf.items()))
return await cache.get(key, get_version_impl)
async def get_version_impl(info):
conf = dict(info)
try:
regex = re.compile(conf['regex'])
except sre_constants.error as e:
raise GetVersionError('bad regex', exc_info=e)
encoding = conf.get('encoding', 'latin1')
res = await session.get(conf['url'])
body = res.body.decode(encoding)
versions = regex.findall(body)
if not versions and not conf.get('missing_ok', False):
raise GetVersionError('version string not found.')
return versions