util.py 1.58 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

def dict_by_value_of_list(array, value):
27
28
    """Mapping function to convert list of dicts to an dict with value as key for each dict. (1 to 1 Mapping)
    Returns an generator object."""
uydwj's avatar
uydwj committed
29
30
31
32
    return ((item[value], item) for item in array)


def dict_of_lists_by_value_of_list(array, value):
33
34
    """Mapping function to convert list of dicts to an dict with values as key for each lists. (1 to n Mapping)
    Returns an generator object."""
uydwj's avatar
uydwj committed
35
36
37
38
39
    res = {}
    for item in array:
        if not item[value] in res:
            res[item[value]] = []
        res[item[value]].append(item)
40
41
    for key, value in res.items():
        yield key, value