2024-07-28 19:33:16 -07:00
|
|
|
from collections.abc import Callable
|
2023-04-07 16:33:23 -07:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from ..error import OneShotError
|
|
|
|
|
|
|
|
|
|
|
|
def oneshot(func: Callable) -> Callable:
|
|
|
|
"""
|
|
|
|
Raises a OneShotError if the decorated function is called more than once
|
|
|
|
"""
|
|
|
|
expired = False
|
|
|
|
|
|
|
|
@wraps(func)
|
2024-07-28 20:14:55 -07:00
|
|
|
def wrapper(*args, **kwargs): # noqa: ANN202
|
2023-04-07 16:33:23 -07:00
|
|
|
nonlocal expired
|
|
|
|
if expired:
|
|
|
|
raise OneShotError(func.__name__)
|
|
|
|
expired = True
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|