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

View file

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

View file

@ -1,11 +1,7 @@
# MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
import structlog
from nvchecker.httpclient import session # type: ignore
logger = structlog.get_logger(logger_name=__name__)
from nvchecker.api import session, GetVersionError
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)
if not data['results']:
logger.error('Arch package not found', name=name)
return
raise GetVersionError('Arch package not found')
r = [r for r in data['results'] if r['repo'] != 'testing'][0]

View file

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

View file

@ -1,7 +1,7 @@
# MIT licensed
# 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
BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s'

View file

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

View file

@ -2,7 +2,7 @@
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, 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'

View file

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

View file

@ -1,7 +1,7 @@
# MIT licensed
# 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):
referree = conf.get('pacman') or name

View file

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

View file

@ -1,7 +1,7 @@
# MIT licensed
# 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/{}'

View file

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

View file

@ -2,7 +2,7 @@
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, 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'

View file

@ -4,11 +4,8 @@
import asyncio
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__))
_cmd_prefix = ['/bin/bash', _path.join(_self_path, 'vcs.sh')]