Coverage for src/bluetooth_sig/gatt/characteristics/utils/parse_trace.py: 54%

13 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-30 00:10 +0000

1"""Parse trace utility for debugging characteristic parsing.""" 

2 

3from __future__ import annotations 

4 

5 

6class ParseTrace: 

7 """Manages parse traces with built-in enable/disable logic. 

8 

9 This class encapsulates the trace collection logic to avoid manual 

10 if checks throughout the parsing code, improving performance when 

11 tracing is disabled. 

12 

13 Example: 

14 trace = ParseTrace(enabled=True) 

15 trace.append("Starting parse") 

16 trace.append("Validation complete") 

17 result = trace.get_trace() # Returns list of strings 

18 

19 """ 

20 

21 def __init__(self, enabled: bool = True) -> None: 

22 """Initialize parse trace collector. 

23 

24 Args: 

25 enabled: Whether to collect trace messages (default: True) 

26 

27 """ 

28 self._enabled = enabled 

29 self._trace: list[str] = [] 

30 

31 def append(self, message: str) -> None: 

32 """Append a message to the trace if tracing is enabled. 

33 

34 Args: 

35 message: Trace message to append 

36 

37 """ 

38 if self._enabled: 

39 self._trace.append(message) 

40 

41 def get_trace(self) -> list[str]: 

42 """Get the collected trace messages. 

43 

44 Returns: 

45 List of trace messages if enabled, empty list otherwise 

46 

47 """ 

48 return self._trace if self._enabled else [] 

49 

50 @property 

51 def enabled(self) -> bool: 

52 """Check if tracing is enabled. 

53 

54 Returns: 

55 True if tracing is enabled, False otherwise 

56 

57 """ 

58 return self._enabled