|
1 | 1 | import os |
2 | 2 | from rich.console import Console |
| 3 | +from rich.markup import escape |
3 | 4 | from rich.panel import Panel |
4 | 5 | from rich.table import Table |
5 | 6 | from rich.text import Text |
6 | 7 | import sys |
7 | 8 | import traceback |
8 | 9 | from typing import Any, Dict, Optional |
9 | 10 |
|
| 11 | + |
| 12 | +def em(text: str) -> str: |
| 13 | + return escape(str(text)) |
| 14 | + |
| 15 | + |
10 | 16 | class DebugManager: |
11 | 17 | def __init__(self): |
12 | 18 | self.console = Console() |
13 | 19 | self._debug_enabled = os.getenv("AQC_DEBUG", "").lower() in ("1", "true", "yes") |
14 | | - self._verbose_enabled = os.getenv("AQC_VERBOSE", "").lower() in ("1", "true", "yes") |
15 | | - |
| 20 | + self._verbose_enabled = os.getenv("AQC_VERBOSE", "").lower() in ( |
| 21 | + "1", |
| 22 | + "true", |
| 23 | + "yes", |
| 24 | + ) |
| 25 | + |
16 | 26 | @property |
17 | 27 | def debug_enabled(self) -> bool: |
18 | 28 | return self._debug_enabled |
19 | | - |
| 29 | + |
20 | 30 | @property |
21 | 31 | def verbose_enabled(self) -> bool: |
22 | 32 | return self._verbose_enabled or self._debug_enabled |
23 | | - |
| 33 | + |
24 | 34 | def enable_debug(self): |
25 | 35 | self._debug_enabled = True |
26 | 36 | os.environ["AQC_DEBUG"] = "1" |
27 | | - |
| 37 | + |
28 | 38 | def enable_verbose(self): |
29 | 39 | self._verbose_enabled = True |
30 | 40 | os.environ["AQC_VERBOSE"] = "1" |
31 | | - |
| 41 | + |
32 | 42 | def debug(self, message: str, prefix: str = "DEBUG"): |
33 | 43 | if self.debug_enabled: |
34 | | - self.console.print(f"[dim cyan]{prefix}: {message}[/dim cyan]") |
35 | | - |
| 44 | + self.console.print(f"[dim cyan]{em(prefix)}: {em(message)}[/dim cyan]") |
| 45 | + |
36 | 46 | def verbose(self, message: str, prefix: str = "INFO"): |
37 | 47 | if self.verbose_enabled: |
38 | | - self.console.print(f"[dim]{prefix}: {message}[/dim]") |
39 | | - |
| 48 | + self.console.print(f"[dim]{em(prefix)}: {em(message)}[/dim]") |
| 49 | + |
40 | 50 | def success(self, message: str): |
41 | 51 | if self.verbose_enabled: |
42 | | - self.console.print(f"[dim green]SUCCESS: {message}[/dim green]") |
43 | | - |
| 52 | + self.console.print(f"[dim green]SUCCESS: {em(message)}[/dim green]") |
| 53 | + |
44 | 54 | def warning(self, message: str): |
45 | 55 | if self.verbose_enabled: |
46 | | - self.console.print(f"[dim yellow]WARNING: {message}[/dim yellow]") |
47 | | - |
| 56 | + self.console.print(f"[dim yellow]WARNING: {em(message)}[/dim yellow]") |
| 57 | + |
48 | 58 | def error(self, message: str, exception: Optional[Exception] = None): |
49 | 59 | if self.verbose_enabled: |
50 | | - self.console.print(f"[dim red]ERROR: {message}[/dim red]") |
| 60 | + self.console.print(f"[dim red]ERROR: {em(message)}[/dim red]") |
51 | 61 | if exception and self.debug_enabled: |
52 | | - self.console.print(f"[dim red]Exception: {str(exception)}[/dim red]") |
53 | | - if hasattr(exception, '__traceback__'): |
| 62 | + self.console.print( |
| 63 | + f"[dim red]Exception: {em(str(exception))}[/dim red]" |
| 64 | + ) |
| 65 | + if hasattr(exception, "__traceback__"): |
54 | 66 | tb_lines = traceback.format_tb(exception.__traceback__) |
55 | 67 | for line in tb_lines: |
56 | 68 | self.console.print(f"[dim red]{line.strip()}[/dim red]") |
57 | | - |
| 69 | + |
58 | 70 | def print_config_info(self, config_data: Dict[str, Any]): |
59 | 71 | if not self.verbose_enabled: |
60 | 72 | return |
61 | | - |
62 | | - table = Table(title="Configuration Information", show_header=True, header_style="bold blue") |
| 73 | + |
| 74 | + table = Table( |
| 75 | + title="Configuration Information", |
| 76 | + show_header=True, |
| 77 | + header_style="bold blue", |
| 78 | + ) |
63 | 79 | table.add_column("Setting", style="cyan") |
64 | 80 | table.add_column("Value", style="green") |
65 | | - |
| 81 | + |
66 | 82 | for key, value in config_data.items(): |
67 | 83 | table.add_row(str(key), str(value)) |
68 | | - |
| 84 | + |
69 | 85 | self.console.print(table) |
70 | | - |
| 86 | + |
71 | 87 | def print_environment_info(self): |
72 | 88 | if not self.debug_enabled: |
73 | 89 | return |
74 | | - |
| 90 | + |
75 | 91 | env_vars = {k: v for k, v in os.environ.items() if k.startswith("AQC_")} |
76 | | - |
77 | | - table = Table(title="Environment Variables", show_header=True, header_style="bold blue") |
| 92 | + |
| 93 | + table = Table( |
| 94 | + title="Environment Variables", show_header=True, header_style="bold blue" |
| 95 | + ) |
78 | 96 | table.add_column("Variable", style="cyan") |
79 | 97 | table.add_column("Value", style="green") |
80 | | - |
| 98 | + |
81 | 99 | for key, value in env_vars.items(): |
82 | 100 | table.add_row(key, value) |
83 | | - |
| 101 | + |
84 | 102 | if not env_vars: |
85 | 103 | table.add_row("No AQC_* variables found", "") |
86 | | - |
| 104 | + |
87 | 105 | self.console.print(table) |
88 | | - |
| 106 | + |
89 | 107 | def print_system_info(self): |
90 | 108 | if not self.debug_enabled: |
91 | 109 | return |
92 | | - |
| 110 | + |
93 | 111 | import platform |
94 | | - |
| 112 | + |
95 | 113 | info = { |
96 | 114 | "Python Version": platform.python_version(), |
97 | 115 | "Platform": platform.platform(), |
98 | 116 | "Architecture": platform.architecture()[0], |
99 | 117 | "Working Directory": os.getcwd(), |
100 | | - "Script Path": sys.argv[0] if sys.argv else "Unknown" |
| 118 | + "Script Path": sys.argv[0] if sys.argv else "Unknown", |
101 | 119 | } |
102 | | - |
103 | | - table = Table(title="System Information", show_header=True, header_style="bold blue") |
| 120 | + |
| 121 | + table = Table( |
| 122 | + title="System Information", show_header=True, header_style="bold blue" |
| 123 | + ) |
104 | 124 | table.add_column("Property", style="cyan") |
105 | 125 | table.add_column("Value", style="green") |
106 | | - |
| 126 | + |
107 | 127 | for key, value in info.items(): |
108 | 128 | table.add_row(key, str(value)) |
109 | | - |
| 129 | + |
110 | 130 | self.console.print(table) |
111 | | - |
| 131 | + |
112 | 132 | def print_translation_info(self, lang_code: str, translator_info: Dict[str, Any]): |
113 | 133 | if not self.debug_enabled: |
114 | 134 | return |
115 | | - |
116 | | - table = Table(title=f"Translation Information ({lang_code})", show_header=True, header_style="bold blue") |
| 135 | + |
| 136 | + table = Table( |
| 137 | + title=f"Translation Information ({lang_code})", |
| 138 | + show_header=True, |
| 139 | + header_style="bold blue", |
| 140 | + ) |
117 | 141 | table.add_column("Property", style="cyan") |
118 | 142 | table.add_column("Value", style="green") |
119 | | - |
| 143 | + |
120 | 144 | for key, value in translator_info.items(): |
121 | 145 | table.add_row(str(key), str(value)) |
122 | | - |
| 146 | + |
123 | 147 | self.console.print(table) |
124 | | - |
125 | | - def trace_function_call(self, func_name: str, args: tuple = (), kwargs: dict = None): |
| 148 | + |
| 149 | + def trace_function_call( |
| 150 | + self, func_name: str, args: tuple = (), kwargs: dict = None |
| 151 | + ): |
126 | 152 | if not self.debug_enabled: |
127 | 153 | return |
128 | | - |
| 154 | + |
129 | 155 | kwargs = kwargs or {} |
130 | 156 | args_str = ", ".join([repr(arg) for arg in args]) |
131 | 157 | kwargs_str = ", ".join([f"{k}={repr(v)}" for k, v in kwargs.items()]) |
132 | | - |
| 158 | + |
133 | 159 | all_args = ", ".join(filter(None, [args_str, kwargs_str])) |
134 | 160 | self.debug(f"Calling {func_name}({all_args})", "TRACE") |
135 | 161 |
|
| 162 | + |
136 | 163 | # Global debug manager instance |
137 | 164 | debug_manager = DebugManager() |
138 | 165 |
|
| 166 | + |
139 | 167 | # Convenience functions |
140 | 168 | def debug(message: str, prefix: str = "DEBUG"): |
141 | 169 | debug_manager.debug(message, prefix) |
142 | 170 |
|
| 171 | + |
143 | 172 | def verbose(message: str, prefix: str = "INFO"): |
144 | 173 | debug_manager.verbose(message, prefix) |
145 | 174 |
|
| 175 | + |
146 | 176 | def success(message: str): |
147 | 177 | debug_manager.success(message) |
148 | 178 |
|
| 179 | + |
149 | 180 | def warning(message: str): |
150 | 181 | debug_manager.warning(message) |
151 | 182 |
|
| 183 | + |
152 | 184 | def error(message: str, exception: Optional[Exception] = None): |
153 | 185 | debug_manager.error(message, exception) |
154 | 186 |
|
| 187 | + |
155 | 188 | def trace_call(func_name: str, args: tuple = (), kwargs: dict = None): |
156 | 189 | debug_manager.trace_function_call(func_name, args, kwargs) |
157 | 190 |
|
| 191 | + |
158 | 192 | def is_debug_enabled() -> bool: |
159 | 193 | return debug_manager.debug_enabled |
160 | 194 |
|
| 195 | + |
161 | 196 | def is_verbose_enabled() -> bool: |
162 | 197 | return debug_manager.verbose_enabled |
0 commit comments