Files
upstream-aw-core/aw_core/decorators.py
upstream-mirror 8757f2fd5e
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
snapshot of https://github.com/ActivityWatch/aw-core.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:17:07 +05:00

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