• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gforcada / haproxy_log_analysis / 3609859985

pending completion
3609859985

push

github

GitHub
Merge pull request #54 from gforcada/better-help-text

12 of 12 new or added lines in 3 files covered. (100.0%)

1477 of 1478 relevant lines covered (99.93%)

5.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.44
/haproxy/utils.py
1
import re
6✔
2
from datetime import datetime, timedelta
6✔
3

4
DELTA_REGEX = re.compile(r'\A(?P<value>\d+)(?P<time_unit>[smhd])\Z')
6✔
5

6
START_REGEX = re.compile(
6✔
7
    r'(?P<day>\d+)/(?P<month>\w+)/(?P<year>\d+)'
8
    r'(:(?P<hour>\d+)|)(:(?P<minute>\d+)|)(:(?P<second>\d+)|)'
9
)
10

11
DELTA_KEYS = {'s': 'seconds', 'm': 'minutes', 'h': 'hours', 'd': 'days'}
6✔
12

13

14
def date_str_to_datetime(date):
6✔
15
    """Convert a string to a datetime object.
16

17
    The format is `day/month/year[[[:hour]:minute]:second]` being:
18
    - day a number
19
    - month a three letter representation of the month (i.e. Dec, Jan, etc)
20
    - year as a 4 digits value
21
    - hour/minute/second as 2 digits value, each of them being optional
22
    """
23
    matches = START_REGEX.match(date)
6✔
24
    data = matches.group('day'), matches.group('month'), matches.group('year')
6✔
25
    raw_date_input = f'{data[0]}/{data[1]}/{data[2]}'
6✔
26
    date_format = '%d/%b/%Y'
6✔
27
    for variable, percent in (('hour', ':%H'), ('minute', ':%M'), ('second', ':%S')):
6✔
28
        match = matches.group(variable)
6✔
29
        if match:
6✔
30
            date_format += percent
6✔
31
            raw_date_input = f'{raw_date_input}:{match}'
6✔
32

33
    return datetime.strptime(raw_date_input, date_format)
6✔
34

35

36
def delta_str_to_timedelta(delta):
6✔
37
    """Convert a string to a timedelta representation.
38

39
    Format is NUMBER followed by one of the following letters: `s`, `m`, `h`, `d`.
40
    Each of them meaning, second, minute, hour and day.
41
    """
42
    matches = DELTA_REGEX.match(delta)
6✔
43
    value = int(matches.group('value'))
6✔
44
    time_unit = matches.group('time_unit')
6✔
45
    key = DELTA_KEYS[time_unit]
6✔
46
    return timedelta(**{key: value})
6✔
47

48

49
def validate_arg_date(start):
6✔
50
    """Check that date argument is valid."""
51
    try:
6✔
52
        date_str_to_datetime(start)
6✔
53
    except (AttributeError, ValueError):
6✔
54
        raise ValueError('--start argument is not valid')
6✔
55

56

57
def validate_arg_delta(delta):
6✔
58
    """Check that the delta argument is valid."""
59
    try:
6✔
60
        delta_str_to_timedelta(delta)
6✔
61
    except (AttributeError, ValueError):
6✔
62
        raise ValueError('--delta argument is not valid')
6✔
63

64

65
def list_filters():
6✔
66
    """Return the information of existing filters.
67

68
    Data returned:
69
    - their names as the user is expected to use them from the command line
70
    - the object itself
71
    - its description
72
    """
73
    from haproxy import filters
6✔
74

75
    data = {}
6✔
76
    for full_name in dir(filters):
6✔
77
        if not full_name.startswith('filter_'):
6✔
78
            continue
6✔
79
        name = full_name[7:]
6✔
80
        obj = getattr(filters, full_name)
6✔
81

82
        description = _strip_description(obj.__doc__)
6✔
83
        data[name] = {'obj': obj, 'description': f'{name}:\n\t{description}'}
6✔
84
    return data
6✔
85

86

87
def list_commands():
6✔
88
    """Return the information of existing commands.
89

90
    Data returned:
91
    - their names as the user is expected to use them from the command line
92
    - the object itself
93
    - its description
94
    """
95
    from haproxy import commands
6✔
96

97
    data = {}
6✔
98
    for cmd in dir(commands):
6✔
99
        if cmd.endswith('Mixin'):
6✔
100
            continue
6✔
101
        klass = getattr(commands, cmd)
6✔
102
        try:
6✔
103
            name = klass.command_line_name()
6✔
104
        except AttributeError:
6✔
105
            continue
6✔
106

107
        description = _strip_description(klass.__doc__)
6✔
108
        data[name] = {'klass': klass, 'description': f'{name}:\n\t{description}'}
6✔
109
    return data
6✔
110

111

112
def _strip_description(raw_text):
6✔
113
    if not raw_text:
6✔
114
        return ''
×
115
    text = '\n\t'.join([line.strip() for line in raw_text.split('\n') if line.strip()])
6✔
116
    return text
6✔
117

118

119
VALID_COMMANDS = list_commands()
6✔
120
VALID_FILTERS = list_filters()
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc