|
5 | 5 | i tworzenie ticketów w projektorze. |
6 | 6 |
|
7 | 7 | Użycie: |
8 | | - # W głównym pliku nlp2cmd (np. __main__.py lub cli.py): |
| 8 | + # Automatyczna instalacja (zalecane) - w __init__.py lub main.py: |
| 9 | + try: |
| 10 | + from projektor import install |
| 11 | + install() |
| 12 | + except ImportError: |
| 13 | + pass # projektor nie jest zainstalowany |
| 14 | + |
| 15 | + # Lub przez ten moduł: |
9 | 16 | from projektor_integration import setup_projektor |
10 | 17 | setup_projektor() |
11 | | - |
12 | | - # Lub importuj z projektor bezpośrednio: |
13 | | - from projektor.integration import install_global_handler |
14 | | - install_global_handler(auto_fix=False) |
15 | 18 | """ |
16 | 19 |
|
17 | 20 | import sys |
|
23 | 26 | sys.path.insert(0, str(PROJEKTOR_PATH)) |
24 | 27 |
|
25 | 28 |
|
26 | | -def setup_projektor(auto_fix: bool = False) -> None: |
| 29 | +def setup_projektor(auto_fix: bool = False) -> bool: |
27 | 30 | """ |
28 | 31 | Konfiguruje integrację projektora z nlp2cmd. |
29 | 32 | |
30 | 33 | Args: |
31 | 34 | auto_fix: Czy automatycznie próbować naprawiać błędy |
| 35 | + |
| 36 | + Returns: |
| 37 | + True jeśli instalacja się powiodła |
32 | 38 | """ |
33 | 39 | try: |
34 | | - from projektor.integration import install_global_handler |
35 | | - from projektor.integration.config_loader import load_integration_config |
36 | | - |
37 | | - # Załaduj konfigurację z projektor.yaml lub pyproject.toml |
38 | | - config = load_integration_config(Path(__file__).parent) |
39 | | - |
40 | | - if not config.enabled: |
41 | | - print("[projektor] Integration disabled in config") |
42 | | - return |
43 | | - |
44 | | - # Użyj ustawień z konfiguracji |
45 | | - install_global_handler(auto_fix=config.auto_fix or auto_fix) |
46 | | - |
47 | | - print("[projektor] Error tracking enabled for nlp2cmd") |
48 | | - if config.auto_fix or auto_fix: |
49 | | - print("[projektor] Auto-fix is ON") |
50 | | - |
| 40 | + from projektor import install |
| 41 | + return install(auto_fix=auto_fix) |
51 | 42 | except ImportError as e: |
52 | 43 | print(f"[projektor] Not available: {e}") |
53 | 44 | print("[projektor] Install with: pip install projektor") |
| 45 | + return False |
| 46 | + |
54 | 47 |
|
| 48 | +# ==================== Dekoratory ==================== |
55 | 49 |
|
56 | | -def catch_nlp2cmd_errors(func): |
| 50 | +def track_errors(func=None, **kwargs): |
57 | 51 | """ |
58 | | - Dekorator do przechwytywania błędów w nlp2cmd. |
| 52 | + Dekorator do śledzenia błędów w funkcjach. |
59 | 53 | |
60 | 54 | Użycie: |
61 | | - @catch_nlp2cmd_errors |
| 55 | + @track_errors |
62 | 56 | def parse_command(text): |
63 | 57 | ... |
| 58 | + |
| 59 | + @track_errors(context={"component": "parser"}) |
| 60 | + def process(data): |
| 61 | + ... |
64 | 62 | """ |
65 | 63 | try: |
66 | | - from projektor.integration import catch_errors |
67 | | - from projektor.core.ticket import Priority |
| 64 | + from projektor import track_errors as _track_errors |
| 65 | + return _track_errors(func, **kwargs) |
| 66 | + except ImportError: |
| 67 | + # Fallback - zwróć oryginalną funkcję |
| 68 | + if func is None: |
| 69 | + return lambda f: f |
| 70 | + return func |
| 71 | + |
| 72 | + |
| 73 | +def track_async_errors(func=None, **kwargs): |
| 74 | + """ |
| 75 | + Dekorator do śledzenia błędów w funkcjach async. |
| 76 | + |
| 77 | + Użycie: |
| 78 | + @track_async_errors |
| 79 | + async def fetch_data(url): |
| 80 | + ... |
68 | 81 | |
69 | | - return catch_errors( |
70 | | - func, |
71 | | - auto_fix=False, |
72 | | - priority=Priority.HIGH, |
73 | | - labels=["nlp2cmd", "runtime-error"], |
74 | | - ) |
| 82 | + @track_async_errors(context={"component": "thermodynamic"}) |
| 83 | + async def generate(text): |
| 84 | + ... |
| 85 | + """ |
| 86 | + try: |
| 87 | + from projektor import track_async_errors as _track_async_errors |
| 88 | + return _track_async_errors(func, **kwargs) |
75 | 89 | except ImportError: |
76 | 90 | # Fallback - zwróć oryginalną funkcję |
| 91 | + if func is None: |
| 92 | + return lambda f: f |
77 | 93 | return func |
78 | 94 |
|
79 | 95 |
|
80 | | -# ==================== Przykłady użycia ==================== |
| 96 | +# ==================== Context Manager ==================== |
| 97 | + |
| 98 | +class ErrorTracker: |
| 99 | + """ |
| 100 | + Context manager do śledzenia błędów. |
| 101 | + |
| 102 | + Użycie: |
| 103 | + with ErrorTracker(context={"input": text[:50]}) as tracker: |
| 104 | + result = process(text) |
| 105 | + |
| 106 | + if tracker.had_error: |
| 107 | + print(f"Error: {tracker.error}") |
| 108 | + """ |
| 109 | + |
| 110 | + def __init__(self, **kwargs): |
| 111 | + self.kwargs = kwargs |
| 112 | + self.had_error = False |
| 113 | + self.error = None |
| 114 | + self.ticket = None |
| 115 | + self._real_tracker = None |
| 116 | + |
| 117 | + def __enter__(self): |
| 118 | + try: |
| 119 | + from projektor import ErrorTracker as _ErrorTracker |
| 120 | + self._real_tracker = _ErrorTracker(**self.kwargs) |
| 121 | + return self._real_tracker.__enter__() |
| 122 | + except ImportError: |
| 123 | + return self |
| 124 | + |
| 125 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 126 | + if self._real_tracker: |
| 127 | + return self._real_tracker.__exit__(exc_type, exc_val, exc_tb) |
| 128 | + if exc_val: |
| 129 | + self.had_error = True |
| 130 | + self.error = exc_val |
| 131 | + return False |
| 132 | + |
| 133 | + |
| 134 | +# ==================== Test ==================== |
81 | 135 |
|
82 | 136 | if __name__ == "__main__": |
| 137 | + print("=== Testing Projektor Integration for nlp2cmd ===\n") |
| 138 | + |
83 | 139 | # Włącz integrację |
84 | | - setup_projektor(auto_fix=False) |
| 140 | + if setup_projektor(auto_fix=False): |
| 141 | + print("[OK] Projektor installed\n") |
| 142 | + else: |
| 143 | + print("[SKIP] Projektor not available\n") |
85 | 144 |
|
86 | | - # Przykład funkcji z dekoratorem |
87 | | - @catch_nlp2cmd_errors |
| 145 | + # Przykład 1: Dekorator |
| 146 | + print("1. Testing @track_errors decorator...") |
| 147 | + |
| 148 | + @track_errors |
88 | 149 | def example_function(): |
89 | | - # Ta funkcja automatycznie utworzy ticket przy błędzie |
90 | | - raise ValueError("Example error for testing projektor integration") |
| 150 | + raise ValueError("Example error for testing") |
91 | 151 |
|
92 | | - print("Testing projektor integration...") |
93 | 152 | try: |
94 | 153 | example_function() |
95 | 154 | except ValueError as e: |
96 | | - print(f"Error caught: {e}") |
97 | | - print("Check .projektor/tickets/ for the created bug ticket") |
| 155 | + print(f" Error caught: {e}") |
| 156 | + |
| 157 | + # Przykład 2: Context manager |
| 158 | + print("\n2. Testing ErrorTracker context manager...") |
| 159 | + |
| 160 | + with ErrorTracker(context={"test": "example"}) as tracker: |
| 161 | + # To nie rzuci błędu |
| 162 | + x = 1 + 1 |
| 163 | + |
| 164 | + print(f" Had error: {tracker.had_error}") |
| 165 | + |
| 166 | + print("\n=== Done ===") |
| 167 | + print("Check .projektor/tickets/ for created bug tickets") |
0 commit comments