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
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-30 00:10 +0000
1"""Parse trace utility for debugging characteristic parsing."""
3from __future__ import annotations
6class ParseTrace:
7 """Manages parse traces with built-in enable/disable logic.
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.
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
19 """
21 def __init__(self, enabled: bool = True) -> None:
22 """Initialize parse trace collector.
24 Args:
25 enabled: Whether to collect trace messages (default: True)
27 """
28 self._enabled = enabled
29 self._trace: list[str] = []
31 def append(self, message: str) -> None:
32 """Append a message to the trace if tracing is enabled.
34 Args:
35 message: Trace message to append
37 """
38 if self._enabled:
39 self._trace.append(message)
41 def get_trace(self) -> list[str]:
42 """Get the collected trace messages.
44 Returns:
45 List of trace messages if enabled, empty list otherwise
47 """
48 return self._trace if self._enabled else []
50 @property
51 def enabled(self) -> bool:
52 """Check if tracing is enabled.
54 Returns:
55 True if tracing is enabled, False otherwise
57 """
58 return self._enabled