File: argparse_addons.py

package info (click to toggle)
python-argparse-addons 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96 kB
  • sloc: python: 116; makefile: 26
file content (62 lines) | stat: -rwxr-xr-x 1,550 bytes parent folder | download
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import argparse

__version__ = '0.12.0'


class Integer:

    def __init__(self, minimum=None, maximum=None):
        self.minimum = minimum
        self.maximum = maximum

    def __call__(self, string):
        value = int(string, 0)

        if self.minimum is not None and self.maximum is not None:
            if not self.minimum <= value <= self.maximum:
                raise argparse.ArgumentTypeError(
                    f'{string} is not in the range {self.minimum}..{self.maximum}')
        elif self.minimum is not None:
            if value < self.minimum:
                raise argparse.ArgumentTypeError(
                    f'{string} is not in the range {self.minimum}..inf')
        elif self.maximum is not None:
            if value > self.maximum:
                raise argparse.ArgumentTypeError(
                    f'{string} is not in the range -inf..{self.maximum}')

        return value

    def __repr__(self):
        return 'integer'


def parse_log_level(value):
    """Parse given set log level value.

    Examples:

    <logger-name>=<level>

    <logger-1-name>=<level-1>:<logger-2-name>=<level-2>

    <logger-1-name>,<logger-2-name>=<level>

    """

    result = []

    if value is None:
        return result

    for part in value.split(':'):
        if '=' in part:
            names, level = part.split('=')

            for name in names.split(','):
                result.append((name, level))
        else:
            # None for root logger.
            result.append((None, part))

    return result