|
| 1 | +import re |
| 2 | +from datetime import timedelta |
1 | 3 | from typing import Any, Dict |
2 | 4 |
|
3 | 5 | from ocp_resources.resource import Resource |
@@ -210,18 +212,38 @@ class Kueue: |
210 | 212 | MANAGED: str = "kueue.openshift.io/managed" |
211 | 213 |
|
212 | 214 |
|
213 | | -class Timeout: |
214 | | - TIMEOUT_15_SEC: int = 15 |
215 | | - TIMEOUT_30SEC: int = 30 |
216 | | - TIMEOUT_1MIN: int = 60 |
217 | | - TIMEOUT_2MIN: int = 2 * TIMEOUT_1MIN |
218 | | - TIMEOUT_4MIN: int = 4 * TIMEOUT_1MIN |
219 | | - TIMEOUT_5MIN: int = 5 * TIMEOUT_1MIN |
220 | | - TIMEOUT_10MIN: int = 10 * TIMEOUT_1MIN |
221 | | - TIMEOUT_15MIN: int = 15 * TIMEOUT_1MIN |
222 | | - TIMEOUT_20MIN: int = 20 * TIMEOUT_1MIN |
223 | | - TIMEOUT_30MIN: int = 30 * TIMEOUT_1MIN |
224 | | - TIMEOUT_40MIN: int = 40 * TIMEOUT_1MIN |
| 215 | +class TimeoutMeta(type): |
| 216 | + def __getattribute__(self, timeout: str) -> int: |
| 217 | + if "TIMEOUT_" not in timeout: |
| 218 | + raise AttributeError("Invalid timeout specified, it should be in the format TIMEOUT_XXYY.") |
| 219 | + delta = timedelta() |
| 220 | + for split_timeout in timeout.split("_")[1:]: |
| 221 | + if parsed := re.search( |
| 222 | + r"(\d+)(SEC$|MIN$|HR$|DAY$|WEEK$|SECS$|MINS$|HRS$|DAYS$|MINS$|WEEKS$)", split_timeout |
| 223 | + ): |
| 224 | + number, duration = parsed.groups() |
| 225 | + number = int(number) |
| 226 | + match duration: |
| 227 | + case "SEC" | "SECS": |
| 228 | + delta += timedelta(seconds=number) |
| 229 | + case "MIN" | "MINS": |
| 230 | + delta += timedelta(minutes=number) |
| 231 | + case "HR" | "HRS": |
| 232 | + delta += timedelta(hours=number) |
| 233 | + case "DAY" | "DAYS": |
| 234 | + delta += timedelta(days=number) |
| 235 | + case "WEEK" | "WEEKS": |
| 236 | + delta += timedelta(weeks=number) |
| 237 | + else: |
| 238 | + raise AttributeError( |
| 239 | + f"Invalid time duration {split_timeout} specified. It must be in the format NUMBERTIME e.g. 10MIN." |
| 240 | + ) |
| 241 | + return int(delta.total_seconds()) |
| 242 | + |
| 243 | + |
| 244 | +class Timeout(metaclass=TimeoutMeta): |
| 245 | + def __init__(self) -> None: |
| 246 | + raise AttributeError("cannot instantiate class of type Timeout.") |
225 | 247 |
|
226 | 248 |
|
227 | 249 | class OpenshiftRouteTimeout: |
|
0 commit comments