Skip to content

Commit 0d1a262

Browse files
committed
feat: create generic timeout
1 parent b02cc25 commit 0d1a262

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

utilities/constants.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
from datetime import timedelta
13
from typing import Any, Dict
24

35
from ocp_resources.resource import Resource
@@ -210,18 +212,38 @@ class Kueue:
210212
MANAGED: str = "kueue.openshift.io/managed"
211213

212214

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.")
225247

226248

227249
class OpenshiftRouteTimeout:

0 commit comments

Comments
 (0)