error-translator-cli-v2 0.1.3__tar.gz → 1.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (16) hide show
  1. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/PKG-INFO +26 -8
  2. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/README.md +25 -7
  3. error_translator_cli_v2-1.0.1/error_translator/auto.py +22 -0
  4. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/error_translator/cli.py +12 -5
  5. error_translator_cli_v2-1.0.1/error_translator/core.py +66 -0
  6. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/error_translator_cli_v2.egg-info/PKG-INFO +26 -8
  7. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/error_translator_cli_v2.egg-info/SOURCES.txt +1 -0
  8. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/pyproject.toml +1 -1
  9. error_translator_cli_v2-0.1.3/error_translator/core.py +0 -45
  10. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/error_translator/__init__.py +0 -0
  11. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/error_translator/rules.py +0 -0
  12. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/error_translator_cli_v2.egg-info/dependency_links.txt +0 -0
  13. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/error_translator_cli_v2.egg-info/entry_points.txt +0 -0
  14. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/error_translator_cli_v2.egg-info/top_level.txt +0 -0
  15. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/setup.cfg +0 -0
  16. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.1}/tests/test_core.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: error-translator-cli-v2
3
- Version: 0.1.3
3
+ Version: 1.0.1
4
4
  Summary: A CLI tool that explains Python errors in simple human language.
5
5
  Author-email: Gourabananda Datta <gourabanandadatta@zohomail.com>
6
6
  Requires-Python: >=3.7
@@ -32,19 +32,37 @@ pip install error-translator-cli-v2
32
32
 
33
33
  🚀 Usage
34
34
 
35
- Simply pass your Python error message or traceback as a string to the explain-error command.
35
+ You can use the Error Translator in three different ways:
36
36
 
37
- Example 1: NameError with Full Traceback
37
+ ### 1. Magic Import (Recommended)
38
+ Simply add this single import at the top of your Python script. If your script crashes, it will automatically intercept and translate the error!
38
39
 
39
- explain-error "Traceback (most recent call last):
40
- File 'script.py', line 2, in <module>
41
- print(my_variable)
42
- NameError: name 'my_variable' is not defined"
40
+ ```python
41
+ import error_translator.auto
43
42
 
43
+ # Your normal code...
44
+ math_is_broken = 10 / 0 # This crash will be automatically translated!
45
+ ```
44
46
 
45
- Example 2: Quick TypeError
47
+ ### 2. Run Scripts via CLI
48
+ You can execute your python files directly through the CLI tool. It will run your program normally and intercept any crashes.
46
49
 
50
+ ```bash
51
+ explain-error run script.py
52
+ ```
53
+
54
+ ### 3. Translate Raw Error Strings
55
+ You can pass errors as a string or pipe them from another command.
56
+
57
+ **Pass directly:**
58
+ ```bash
47
59
  explain-error "TypeError: unsupported operand type(s) for +: 'int' and 'str'"
60
+ ```
61
+
62
+ **Pipe from a file:**
63
+ ```bash
64
+ cat error.log | explain-error
65
+ ```
48
66
 
49
67
 
50
68
  🧠 Supported Errors
@@ -24,19 +24,37 @@ pip install error-translator-cli-v2
24
24
 
25
25
  🚀 Usage
26
26
 
27
- Simply pass your Python error message or traceback as a string to the explain-error command.
27
+ You can use the Error Translator in three different ways:
28
28
 
29
- Example 1: NameError with Full Traceback
29
+ ### 1. Magic Import (Recommended)
30
+ Simply add this single import at the top of your Python script. If your script crashes, it will automatically intercept and translate the error!
30
31
 
31
- explain-error "Traceback (most recent call last):
32
- File 'script.py', line 2, in <module>
33
- print(my_variable)
34
- NameError: name 'my_variable' is not defined"
32
+ ```python
33
+ import error_translator.auto
35
34
 
35
+ # Your normal code...
36
+ math_is_broken = 10 / 0 # This crash will be automatically translated!
37
+ ```
36
38
 
37
- Example 2: Quick TypeError
39
+ ### 2. Run Scripts via CLI
40
+ You can execute your python files directly through the CLI tool. It will run your program normally and intercept any crashes.
38
41
 
42
+ ```bash
43
+ explain-error run script.py
44
+ ```
45
+
46
+ ### 3. Translate Raw Error Strings
47
+ You can pass errors as a string or pipe them from another command.
48
+
49
+ **Pass directly:**
50
+ ```bash
39
51
  explain-error "TypeError: unsupported operand type(s) for +: 'int' and 'str'"
52
+ ```
53
+
54
+ **Pipe from a file:**
55
+ ```bash
56
+ cat error.log | explain-error
57
+ ```
40
58
 
41
59
 
42
60
  🧠 Supported Errors
@@ -0,0 +1,22 @@
1
+ import sys
2
+ import traceback
3
+ from .core import translate_error
4
+ from .cli import print_result
5
+
6
+ def magic_hook(exc_type, exc_value, tb):
7
+ """
8
+ This function intercepts a Python crash right before it prints
9
+ to the terminal, formats the error, and translates it.
10
+ """
11
+ # 1. Convert the raw crash data into a standard traceback string
12
+ tb_lines = traceback.format_exception(exc_type, exc_value, tb)
13
+ tb_string = "".join(tb_lines)
14
+
15
+ # 2. Pass it through our translation engine
16
+ result = translate_error(tb_string)
17
+
18
+ # 3. Print our beautiful colorized output instead of the ugly default
19
+ print_result(result)
20
+
21
+ # The Magic Trick: Replace Python's default crash handler with ours
22
+ sys.excepthook = magic_hook
@@ -1,6 +1,5 @@
1
1
  import sys
2
2
  import argparse
3
- import subprocess
4
3
  from .core import translate_error
5
4
 
6
5
  # ANSI Color Codes
@@ -14,22 +13,30 @@ class Colors:
14
13
 
15
14
  def print_result(result: dict):
16
15
  """Prints the translated error to the terminal with colors."""
17
- print(f"\n{Colors.RED}{Colors.BOLD}🚨 Error Detected:{Colors.RESET}")
16
+ print(f"\n{Colors.RED}{Colors.BOLD} Error Detected:{Colors.RESET}")
18
17
  print(f"{result.get('matched_error', 'N/A')}")
19
18
 
20
19
  if "file" in result:
21
- print(f"{Colors.YELLOW}📍 Location: {result['file']} (Line {result['line']}){Colors.RESET}\n")
20
+ print(f"{Colors.YELLOW} Location: {result['file']} (Line {result['line']}){Colors.RESET}\n")
21
+ if result.get("code"):
22
+ print(f"{Colors.RESET} |")
23
+ print(f"{Colors.RESET} | {Colors.RED}{result['code']}{Colors.RESET}")
24
+ print(f"{Colors.RESET} |\n")
25
+ else:
26
+ print("\n")
27
+
22
28
  else:
23
29
  print()
24
30
 
25
- print(f"{Colors.CYAN}{Colors.BOLD}🧠 Explanation:{Colors.RESET}")
31
+ print(f"{Colors.CYAN}{Colors.BOLD} Explanation:{Colors.RESET}")
26
32
  print(f"{result['explanation']}\n")
27
33
 
28
- print(f"{Colors.GREEN}{Colors.BOLD}🛠️ Suggested Fix:{Colors.RESET}")
34
+ print(f"{Colors.GREEN}{Colors.BOLD} Suggested Fix:{Colors.RESET}")
29
35
  print(f"{result['fix']}\n")
30
36
 
31
37
  def run_script(script_name: str):
32
38
  """Runs a python script in the background and catches its errors."""
39
+ import subprocess
33
40
  try:
34
41
  # Run the script using the current Python environment
35
42
  result = subprocess.run(
@@ -0,0 +1,66 @@
1
+ import json
2
+ import os
3
+
4
+ def load_rules():
5
+ """Loads the error rules from the JSON file."""
6
+ # Find the absolute path to the json file next to this script
7
+ # current_dir = os.path.dirname(data\rules.json)
8
+ current_dir = 'data/'
9
+ json_path = os.path.join(current_dir, 'rules.json')
10
+
11
+ with open(json_path, 'r') as file:
12
+ return json.load(file)
13
+
14
+ def translate_error(traceback_text: str) -> dict:
15
+ import re
16
+ import linecache # <-- NEW: Lazy import the linecache module
17
+
18
+ data = load_rules()
19
+ rules = data["rules"]
20
+ default_error = data["default"]
21
+
22
+ lines = [line.strip() for line in traceback_text.strip().split('\n') if line.strip()]
23
+ if not lines:
24
+ return {"explanation": "No error text provided.", "fix": "Provide a valid Python error."}
25
+
26
+ actual_error_line = lines[-1]
27
+
28
+ location_match = re.search(r'File\s+[\'"]?(.*?)[\'"]?,\s+line\s+(\d+)', traceback_text)
29
+ file_name = location_match.group(1) if location_match else "Unknown File"
30
+ line_number = location_match.group(2) if location_match else "Unknown Line"
31
+
32
+ # --- NEW CONTEXT ENGINE LOGIC ---
33
+ code_context = ""
34
+ if file_name != "Unknown File" and line_number != "Unknown Line":
35
+ try:
36
+ # linecache safely fetches the exact line of code as a string
37
+ raw_line = linecache.getline(file_name, int(line_number))
38
+ if raw_line:
39
+ code_context = raw_line.strip() # Remove extra spaces/newlines
40
+ except Exception:
41
+ pass # If the file can't be read for any reason, fail silently
42
+ # --------------------------------
43
+
44
+ for rule in rules:
45
+ pattern = re.compile(rule["pattern"])
46
+ match = pattern.search(actual_error_line)
47
+
48
+ if match:
49
+ extracted_values = match.groups()
50
+ return {
51
+ "explanation": rule["explanation"].format(*extracted_values),
52
+ "fix": rule["fix"].format(*extracted_values),
53
+ "matched_error": actual_error_line,
54
+ "file": file_name,
55
+ "line": line_number,
56
+ "code": code_context
57
+ }
58
+
59
+ return {
60
+ "explanation": default_error["explanation"],
61
+ "fix": default_error["fix"],
62
+ "matched_error": actual_error_line,
63
+ "file": file_name,
64
+ "line": line_number,
65
+ "code": code_context
66
+ }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: error-translator-cli-v2
3
- Version: 0.1.3
3
+ Version: 1.0.1
4
4
  Summary: A CLI tool that explains Python errors in simple human language.
5
5
  Author-email: Gourabananda Datta <gourabanandadatta@zohomail.com>
6
6
  Requires-Python: >=3.7
@@ -32,19 +32,37 @@ pip install error-translator-cli-v2
32
32
 
33
33
  🚀 Usage
34
34
 
35
- Simply pass your Python error message or traceback as a string to the explain-error command.
35
+ You can use the Error Translator in three different ways:
36
36
 
37
- Example 1: NameError with Full Traceback
37
+ ### 1. Magic Import (Recommended)
38
+ Simply add this single import at the top of your Python script. If your script crashes, it will automatically intercept and translate the error!
38
39
 
39
- explain-error "Traceback (most recent call last):
40
- File 'script.py', line 2, in <module>
41
- print(my_variable)
42
- NameError: name 'my_variable' is not defined"
40
+ ```python
41
+ import error_translator.auto
43
42
 
43
+ # Your normal code...
44
+ math_is_broken = 10 / 0 # This crash will be automatically translated!
45
+ ```
44
46
 
45
- Example 2: Quick TypeError
47
+ ### 2. Run Scripts via CLI
48
+ You can execute your python files directly through the CLI tool. It will run your program normally and intercept any crashes.
46
49
 
50
+ ```bash
51
+ explain-error run script.py
52
+ ```
53
+
54
+ ### 3. Translate Raw Error Strings
55
+ You can pass errors as a string or pipe them from another command.
56
+
57
+ **Pass directly:**
58
+ ```bash
47
59
  explain-error "TypeError: unsupported operand type(s) for +: 'int' and 'str'"
60
+ ```
61
+
62
+ **Pipe from a file:**
63
+ ```bash
64
+ cat error.log | explain-error
65
+ ```
48
66
 
49
67
 
50
68
  🧠 Supported Errors
@@ -1,6 +1,7 @@
1
1
  README.md
2
2
  pyproject.toml
3
3
  error_translator/__init__.py
4
+ error_translator/auto.py
4
5
  error_translator/cli.py
5
6
  error_translator/core.py
6
7
  error_translator/rules.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "error-translator-cli-v2"
7
- version = "0.1.3"
7
+ version = "1.0.1"
8
8
  description = "A CLI tool that explains Python errors in simple human language."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.7"
@@ -1,45 +0,0 @@
1
- from .rules import ERROR_RULES, DEFAULT_ERROR
2
- import re
3
-
4
- def translate_error(traceback_text: str) -> dict:
5
- """
6
- Parses traceback text and returns a human-readable explanation and fix.
7
- """
8
- # Update this line inside your translate_error function:
9
- location_match = re.search(r'File\s+[\'"]?(.*?)[\'"]?,\s+line\s+(\d+)', traceback_text)
10
- file_name = location_match.group(1) if location_match else "unknown file"
11
- line_number = location_match.group(2) if location_match else "unknown line"
12
- # Grab the last non-empty line of the traceback, which usually contains the actual error
13
- # print(f"\n--- DEBUG RAW INPUT ---\n{repr(traceback_text)}\n-----------------------\n")
14
- lines = [line.strip() for line in traceback_text.strip().split('\n') if line.strip()]
15
- if not lines:
16
- return {"explanation": "No error text provided.", "fix": "Provide a valid Python error."}
17
-
18
- actual_error_line = lines[-1]
19
-
20
- for rule in ERROR_RULES:
21
- match = rule["pattern"].search(actual_error_line)
22
- if match:
23
- # Extract the captured regex groups (e.g., the variable name)
24
- extracted_values = match.groups()
25
-
26
- # Format the explanation and fix using the extracted values
27
- explanation = rule["explanation"].format(*extracted_values)
28
- fix = rule["fix"].format(*extracted_values)
29
-
30
- return {
31
- "explanation": explanation,
32
- "fix": fix,
33
- "matched_error": actual_error_line,
34
- "file": file_name,
35
- "line": line_number,
36
- }
37
-
38
- # If no rules match, return the default payload
39
- return {
40
- "explanation": DEFAULT_ERROR["explanation"],
41
- "fix": DEFAULT_ERROR["fix"],
42
- "matched_error": actual_error_line,
43
- "file": file_name,
44
- "line": line_number,
45
- }