create an api for modules

This commit is contained in:
lilydjwg 2020-08-14 20:04:05 +08:00
parent 72d1d27f89
commit 19553c3564
16 changed files with 38 additions and 43 deletions

1
NEW
View file

@ -3,4 +3,3 @@ TODO:
* update tests * update tests
* update README * update README
* create source plugin documentation * create source plugin documentation
* move things to a seperate `api.py`

9
nvchecker/api.py Normal file
View file

@ -0,0 +1,9 @@
# MIT licensed
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
from .httpclient import session # type: ignore
from .core import GetVersionError
from .util import (
Entry, BaseWorker, RawResult, VersionResult,
)
from .sortversion import sort_version_keys

View file

@ -16,7 +16,7 @@ from pathlib import Path
import toml import toml
import structlog import structlog
from .httpclient import session from .httpclient import session # type: ignore
logger = structlog.get_logger(logger_name=__name__) logger = structlog.get_logger(logger_name=__name__)
@ -92,7 +92,7 @@ class AsyncCache(Generic[T, S]):
async def get_json(self, url: str) -> Any: async def get_json(self, url: str) -> Any:
return await self.get( return await self.get(
('_jsonurl', url), self._get_json) ('_jsonurl', url), self._get_json) # type: ignore
async def get( async def get(
self, self,

View file

@ -6,7 +6,7 @@ import os
import re import re
from xml.etree import ElementTree from xml.etree import ElementTree
from nvchecker.httpclient import session from nvchecker.api import session
_ANDROID_REPO_MANIFESTS = { _ANDROID_REPO_MANIFESTS = {
'addon': 'https://dl.google.com/android/repository/addon2-1.xml', 'addon': 'https://dl.google.com/android/repository/addon2-1.xml',

View file

@ -1,11 +1,7 @@
# MIT licensed # MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
import structlog from nvchecker.api import session, GetVersionError
from nvchecker.httpclient import session # type: ignore
logger = structlog.get_logger(logger_name=__name__)
URL = 'https://www.archlinux.org/packages/search/json/' URL = 'https://www.archlinux.org/packages/search/json/'
@ -21,8 +17,7 @@ async def get_version(name, conf, *, cache, **kwargs):
data = await cache.get(pkg, request) data = await cache.get(pkg, request)
if not data['results']: if not data['results']:
logger.error('Arch package not found', name=name) raise GetVersionError('Arch package not found')
return
r = [r for r in data['results'] if r['repo'] != 'testing'][0] r = [r for r in data['results'] if r['repo'] != 'testing'][0]

View file

@ -1,15 +1,14 @@
# MIT licensed # MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
import structlog
from datetime import datetime from datetime import datetime
import asyncio import asyncio
from typing import Iterable, Dict, List, Tuple, Any, Optional from typing import Iterable, Dict, List, Tuple, Any, Optional
from nvchecker.util import Entry, BaseWorker, RawResult from nvchecker.api import (
from nvchecker.httpclient import session # type: ignore session, GetVersionError, VersionResult,
Entry, BaseWorker, RawResult,
logger = structlog.get_logger(logger_name=__name__) )
AUR_URL = 'https://aur.archlinux.org/rpc/' AUR_URL = 'https://aur.archlinux.org/rpc/'
@ -77,21 +76,21 @@ class Worker(BaseWorker):
async def _run_batch_impl( async def _run_batch_impl(
batch: List[Tuple[str, Entry]], batch: List[Tuple[str, Entry]],
aur_results: AurResults, aur_results: AurResults,
) -> Dict[str, str]: ) -> Dict[str, VersionResult]:
aurnames = {conf.get('aur', name) for name, conf in batch} aurnames = {conf.get('aur', name) for name, conf in batch}
results = await aur_results.get_multiple(aurnames) results = await aur_results.get_multiple(aurnames)
ret = {} ret: Dict[str, VersionResult] = {}
for name, conf in batch: for name, conf in batch:
aurname = conf.get('aur', name) aurname = conf.get('aur', name)
use_last_modified = conf.get('use_last_modified', False) use_last_modified = conf.get('use_last_modified', False)
strip_release = conf.get('strip-release', False) strip_release = conf.get('strip_release', False)
result = results.get(aurname) result = results.get(aurname)
if result is None: if result is None:
logger.error('AUR upstream not found', name=name) ret[name] = GetVersionError('AUR upstream not found')
continue continue
version = result['Version'] version = result['Version']

View file

@ -1,7 +1,7 @@
# 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 nvchecker.sortversion import sort_version_keys from nvchecker.api import sort_version_keys
# doc: https://confluence.atlassian.com/display/BITBUCKET/commits+or+commit+Resource # doc: https://confluence.atlassian.com/display/BITBUCKET/commits+or+commit+Resource
BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s' BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s'

View file

@ -5,7 +5,7 @@ import asyncio
import structlog import structlog
from nvchecker.util import GetVersionError from nvchecker.api import GetVersionError
logger = structlog.get_logger(logger_name=__name__) logger = structlog.get_logger(logger_name=__name__)

View file

@ -2,7 +2,7 @@
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
# Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al. # Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al.
from nvchecker.util import GetVersionError from nvchecker.api import GetVersionError
URL = 'https://sources.debian.org/api/src/%(pkgname)s/?suite=%(suite)s' URL = 'https://sources.debian.org/api/src/%(pkgname)s/?suite=%(suite)s'

View file

@ -3,13 +3,14 @@
from __future__ import annotations from __future__ import annotations
import structlog from nvchecker.api import (
from nvchecker.util import BaseWorker BaseWorker, GetVersionError, RawResult,
)
logger = structlog.get_logger(logger_name=__name__)
class Worker(BaseWorker): class Worker(BaseWorker):
async def run(self) -> None: async def run(self) -> None:
exc = GetVersionError('no source specified')
async with self.acquire_token(): async with self.acquire_token():
for name, _ in self.tasks: for name, conf in self.tasks:
logger.error('no source specified', name=name) self.result_q.put(
RawResult(name, exc, conf))

View file

@ -1,7 +1,7 @@
# 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 . import cmd from nvchecker_source import cmd # type: ignore
async def get_version(name, conf, **kwargs): async def get_version(name, conf, **kwargs):
referree = conf.get('pacman') or name referree = conf.get('pacman') or name

View file

@ -4,12 +4,7 @@
import re import re
import sre_constants import sre_constants
import structlog from nvchecker.api import session, GetVersionError
from nvchecker.httpclient import session
from nvchecker.util import GetVersionError
logger = structlog.get_logger(logger_name=__name__)
async def get_version(name, conf, *, cache, **kwargs): async def get_version(name, conf, *, cache, **kwargs):
key = sorted(conf.items()) key = sorted(conf.items())

View file

@ -1,7 +1,7 @@
# MIT licensed # MIT licensed
# Copyright (c) 2019 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2019 lilydjwg <lilydjwg@gmail.com>, et al.
from nvchecker.util import GetVersionError from nvchecker.api import GetVersionError
API_URL = 'https://repology.org/api/v1/project/{}' API_URL = 'https://repology.org/api/v1/project/{}'

View file

@ -4,7 +4,7 @@
from xml.etree import ElementTree from xml.etree import ElementTree
from nvchecker.httpclient import session from nvchecker.api import session
async def get_version(name, conf, *, cache, **kwargs): async def get_version(name, conf, *, cache, **kwargs):
sparkle = conf['sparkle'] sparkle = conf['sparkle']

View file

@ -2,7 +2,7 @@
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
# Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al. # Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al.
from nvchecker.util import GetVersionError from nvchecker.api import GetVersionError
URL = 'https://api.launchpad.net/1.0/ubuntu/+archive/primary?ws.op=getPublishedSources&source_name=%s&exact_match=true' URL = 'https://api.launchpad.net/1.0/ubuntu/+archive/primary?ws.op=getPublishedSources&source_name=%s&exact_match=true'

View file

@ -4,11 +4,8 @@
import asyncio import asyncio
import os.path as _path import os.path as _path
import structlog from nvchecker.api import GetVersionError
from nvchecker.util import GetVersionError
logger = structlog.get_logger(logger_name=__name__)
_self_path = _path.dirname(_path.abspath(__file__)) _self_path = _path.dirname(_path.abspath(__file__))
_cmd_prefix = ['/bin/bash', _path.join(_self_path, 'vcs.sh')] _cmd_prefix = ['/bin/bash', _path.join(_self_path, 'vcs.sh')]