Coverage for /Users/buh/.pyenv/versions/3.12.9/envs/es-testbed/lib/python3.12/site-packages/es_testbed/debug.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-17 22:23 -0600

1"""Tiered Debugging module""" 

2 

3import typing as t 

4from functools import wraps 

5from tiered_debug import TieredDebug 

6 

7debug = TieredDebug() 

8 

9 

10def begin_end(begin: t.Optional[int] = 2, end: t.Optional[int] = 3) -> t.Callable: 

11 """Decorator to log the beginning and end of a function. 

12 

13 This decorator will log the beginning and end of a function call at the 

14 specified levels, defaulting to 2 for the beginning and 3 for the ending. 

15 

16 :return: The decorated function. 

17 :rtype: Callable 

18 """ 

19 mmap = { 

20 1: debug.lv1, 

21 2: debug.lv2, 

22 3: debug.lv3, 

23 4: debug.lv4, 

24 5: debug.lv5, 

25 } 

26 

27 def decorator(func: t.Callable) -> t.Callable: 

28 @wraps(func) 

29 def wrapper(*args, **kwargs): 

30 common = f"CALL: {func.__name__}()" 

31 mmap[begin](f"BEGIN {common}", stklvl=debug.stacklevel + 1) 

32 result = func(*args, **kwargs) 

33 mmap[end](f"END {common}", stklvl=debug.stacklevel + 1) 

34 return result 

35 

36 return wrapper 

37 

38 return decorator