File: jq.py

package info (click to toggle)
python-nvchecker 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: python: 4,801; makefile: 25
file content (42 lines) | stat: -rw-r--r-- 1,181 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# MIT licensed
# Copyright (c) 2024 Rocket Aaron <i@rocka.me>, et al.

import json
import jq

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:
    program = jq.compile(conf.get('filter', '.'))
  except ValueError as e:
    raise GetVersionError('bad jq filter', exc_info=e)

  data = conf.get('post_data')
  if data is None:
    res = await session.get(conf['url'])
  else:
    res = await session.post(conf['url'], body = data, headers = {
        'Content-Type': conf.get('post_data_type', 'application/json')
      })

  try:
    obj = json.loads(res.body)
  except json.decoder.JSONDecodeError as e:
    raise GetVersionError('bad json string', exc_info=e)

  try:
    version = program.input(obj).all()
    if version == [None] and not conf.get('missing_ok', False):
      raise GetVersionError('version string not found.')
    version = [str(v) for v in version]
  except ValueError as e:
    raise GetVersionError('failed to filter json', exc_info=e)

  return version