Some checks failed
Build / Test on ubuntu-latest, py-3.8 (push) Successful in 4m21s
Lint / lint (push) Successful in 25s
Build / Test on macOS-latest, py-3.8 (push) Has been cancelled
Build / Test on windows-latest, py-3.8 (push) Has been cancelled
CodeQL / Analyze (python) (push) Failing after 1s
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import functools
|
|
import logging
|
|
import time
|
|
import warnings
|
|
|
|
|
|
def deprecated(f): # pragma: no cover
|
|
"""
|
|
This is a decorator which can be used to mark functions
|
|
as deprecated. It will result in a warning being emitted
|
|
when the function is used.
|
|
|
|
Taken from: http://stackoverflow.com/a/30253848/965332
|
|
"""
|
|
# Warn only once per deprecated function
|
|
warned_for = False
|
|
|
|
@functools.wraps(f)
|
|
def g(*args, **kwargs):
|
|
# TODO: Use logging module instead?
|
|
nonlocal warned_for
|
|
if not warned_for:
|
|
warnings.simplefilter("always", DeprecationWarning) # turn off filter
|
|
warnings.warn(
|
|
f"Call to deprecated function {f.__name__}, "
|
|
"this warning will only show once per function.",
|
|
category=DeprecationWarning,
|
|
stacklevel=2,
|
|
)
|
|
warnings.simplefilter("default", DeprecationWarning) # reset filter
|
|
warned_for = True
|
|
return f(*args, **kwargs)
|
|
|
|
return g
|
|
|
|
|
|
def restart_on_exception(f, delay=1, exception=Exception): # pragma: no cover
|
|
@functools.wraps(f)
|
|
def g(*args, **kwargs):
|
|
while True:
|
|
try:
|
|
f(*args, **kwargs)
|
|
except exception as e:
|
|
# TODO: Use warnings module instead?
|
|
logging.error(f"{f.__name__} crashed due to exception, restarting.")
|
|
logging.error(e)
|
|
time.sleep(
|
|
delay
|
|
) # To prevent extremely fast restarts in case of bad state.
|
|
|
|
return g
|