util.py 1.25 KB
Newer Older
janis.streib's avatar
janis.streib committed
1
2
import argparse

3
4
default_server = 'www-net-devel.scc.kit.edu'
default_api_version = '3.0'
janis.streib's avatar
janis.streib committed
5
6
7
8
9
10
11
12
13
14


class ArgumentParser(argparse.ArgumentParser):
    """Argument parser with default common arguments for NetDB-api cli
    tools. Includes default values."""
    def __init__(self, formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                 **kwargs):
        super(ArgumentParser, self).__init__(formatter_class=formatter_class,
                                             **kwargs)
        self.add_argument('--token', '-t',
janis.streib's avatar
janis.streib committed
15
                          help='user API token', required=True)
16
        self.add_argument('--server', '-s', default=default_server,
janis.streib's avatar
janis.streib committed
17
                          help='webapi server')
18
        self.add_argument('--api-version', default=default_api_version,
janis.streib's avatar
janis.streib committed
19
20
                          help='webapi version')

uydwj's avatar
uydwj committed
21
22
    def parse_args(self, args=None, namespace=None):
        args = super(ArgumentParser, self).parse_args(args, namespace)
janis.streib's avatar
janis.streib committed
23
24
        return args

uydwj's avatar
uydwj committed
25
26
27
28
29
30
31
32
33
34
35
36

def dict_by_value_of_list(array, value):
    return ((item[value], item) for item in array)


def dict_of_lists_by_value_of_list(array, value):
    res = {}
    for item in array:
        if not item[value] in res:
            res[item[value]] = []
        res[item[value]].append(item)
    return res