aboutsummaryrefslogtreecommitdiff
blob: 89b4d203d4162761b262b1b3d7b6ccc20d2cc9c2 (plain)
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
43
from __future__ import print_function

import pprint as pp
from gentoostats import utils

def pprint(title, object):
    """
    Pretty printer for the decoded json data
    """
    print(title)
    pp.pprint(object)

def add_parser(subparsers):
    """
    Setup argparse parsers
    """
    search_parser = subparsers.add_parser('search')
    search_parser.add_argument('-c', '--category')
    search_parser.add_argument('-p', '--package')
    search_parser.add_argument('-v', '--version')
    search_parser.add_argument('-r', '--repo')
    search_parser.add_argument('--min_hosts', type=int)
    search_parser.add_argument('--max_hosts', type=int)
    search_parser.set_defaults(command=search)

def search(args):
    """
    /search
    """
    url_base = '/search'
    url_extra = ''

    url_extra += ('?', '&')[bool(url_extra)] + 'cat=' + args.category if args.category else ''
    url_extra += ('?', '&')[bool(url_extra)] + 'pkg=' + args.package if args.package else ''
    url_extra += ('?', '&')[bool(url_extra)] + 'ver=' + args.version if args.version else ''
    url_extra += ('?', '&')[bool(url_extra)] + 'repo=' + args.repo if args.repo else ''
    url_extra += ('?', '&')[bool(url_extra)] + 'min_hosts=' + str(args.min_hosts) if args.min_hosts else ''
    url_extra += ('?', '&')[bool(url_extra)] + 'max_hosts=' + str(args.max_hosts) if args.max_hosts else ''

    get_data = utils.GET(server = args.server, url = args.url + url_base + url_extra, headers = utils.headers)
    data = utils.deserialize(get_data)

    pprint ('Search results', data)