error-translator-cli-v2 0.1.3__tar.gz → 1.0.0__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.0}/PKG-INFO +1 -1
  2. error_translator_cli_v2-1.0.0/error_translator/auto.py +22 -0
  3. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/error_translator/cli.py +5 -5
  4. error_translator_cli_v2-1.0.0/error_translator/core.py +53 -0
  5. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/error_translator_cli_v2.egg-info/PKG-INFO +1 -1
  6. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/error_translator_cli_v2.egg-info/SOURCES.txt +1 -0
  7. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/pyproject.toml +1 -1
  8. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/tests/test_core.py +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.0}/README.md +0 -0
  11. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/error_translator/__init__.py +0 -0
  12. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/error_translator/rules.py +0 -0
  13. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/error_translator_cli_v2.egg-info/dependency_links.txt +0 -0
  14. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/error_translator_cli_v2.egg-info/entry_points.txt +0 -0
  15. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/error_translator_cli_v2.egg-info/top_level.txt +0 -0
  16. {error_translator_cli_v2-0.1.3 → error_translator_cli_v2-1.0.0}/setup.cfg +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.0
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
@@ -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,23 @@ 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")
22
21
  else:
23
22
  print()
24
23
 
25
- print(f"{Colors.CYAN}{Colors.BOLD}🧠 Explanation:{Colors.RESET}")
24
+ print(f"{Colors.CYAN}{Colors.BOLD} Explanation:{Colors.RESET}")
26
25
  print(f"{result['explanation']}\n")
27
26
 
28
- print(f"{Colors.GREEN}{Colors.BOLD}🛠️ Suggested Fix:{Colors.RESET}")
27
+ print(f"{Colors.GREEN}{Colors.BOLD} Suggested Fix:{Colors.RESET}")
29
28
  print(f"{result['fix']}\n")
30
29
 
31
30
  def run_script(script_name: str):
32
31
  """Runs a python script in the background and catches its errors."""
32
+ import subprocess
33
33
  try:
34
34
  # Run the script using the current Python environment
35
35
  result = subprocess.run(
@@ -0,0 +1,53 @@
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 # Lazy import
16
+
17
+ data = load_rules()
18
+ rules = data["rules"]
19
+ default_error = data["default"]
20
+
21
+ lines = [line.strip() for line in traceback_text.strip().split('\n') if line.strip()]
22
+ if not lines:
23
+ return {"explanation": "No error text provided.", "fix": "Provide a valid Python error."}
24
+
25
+ actual_error_line = lines[-1]
26
+
27
+ # Flexible regex to catch single or double quotes, and handle missing ones
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
+ for rule in rules:
33
+ # Compile the regex pattern on the fly
34
+ pattern = re.compile(rule["pattern"])
35
+ match = pattern.search(actual_error_line)
36
+
37
+ if match:
38
+ extracted_values = match.groups()
39
+ return {
40
+ "explanation": rule["explanation"].format(*extracted_values),
41
+ "fix": rule["fix"].format(*extracted_values),
42
+ "matched_error": actual_error_line,
43
+ "file": file_name,
44
+ "line": line_number
45
+ }
46
+
47
+ return {
48
+ "explanation": default_error["explanation"],
49
+ "fix": default_error["fix"],
50
+ "matched_error": actual_error_line,
51
+ "file": file_name,
52
+ "line": line_number
53
+ }
@@ -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.0
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
@@ -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.0"
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,4 +1,4 @@
1
- from error_translator.core import translate_error
1
+ import error_translator.auto
2
2
 
3
3
  def test_name_error_translation_double_quotes():
4
4
  """Test standard traceback with double quotes around the filename."""
@@ -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
- }