|
17 | 17 | """
|
18 | 18 |
|
19 | 19 | import logging
|
| 20 | +import re |
20 | 21 | import time
|
21 | 22 | from datetime import timedelta
|
22 | 23 | from typing import Any, Dict, Optional, Union
|
@@ -50,6 +51,8 @@ class Timer(Callback):
|
50 | 51 | verbose: Set this to ``False`` to suppress logging messages.
|
51 | 52 |
|
52 | 53 | Raises:
|
| 54 | + MisconfigurationException: |
| 55 | + If ``duration`` is not in the expected format. |
53 | 56 | MisconfigurationException:
|
54 | 57 | If ``interval`` is not one of the supported choices.
|
55 | 58 |
|
@@ -86,10 +89,19 @@ def __init__(
|
86 | 89 | ) -> None:
|
87 | 90 | super().__init__()
|
88 | 91 | if isinstance(duration, str):
|
89 |
| - dhms = duration.strip().split(":") |
90 |
| - dhms = [int(i) for i in dhms] |
91 |
| - duration = timedelta(days=dhms[0], hours=dhms[1], minutes=dhms[2], seconds=dhms[3]) |
92 |
| - if isinstance(duration, dict): |
| 92 | + duration_match = re.fullmatch(r"(\d+):(\d\d):(\d\d):(\d\d)", duration.strip()) |
| 93 | + if not duration_match: |
| 94 | + raise MisconfigurationException( |
| 95 | + f"`Timer(duration={duration!r})` is not a valid duration. " |
| 96 | + "Expected a string in the format DD:HH:MM:SS." |
| 97 | + ) |
| 98 | + duration = timedelta( |
| 99 | + days=int(duration_match.group(1)), |
| 100 | + hours=int(duration_match.group(2)), |
| 101 | + minutes=int(duration_match.group(3)), |
| 102 | + seconds=int(duration_match.group(4)), |
| 103 | + ) |
| 104 | + elif isinstance(duration, dict): |
93 | 105 | duration = timedelta(**duration)
|
94 | 106 | if interval not in set(Interval):
|
95 | 107 | raise MisconfigurationException(
|
|
0 commit comments