Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,19 +8,29 @@ from tools.final_answer import FinalAnswerTool
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
import ast
|
|
|
|
| 11 |
import pyflakes.api
|
| 12 |
import pyflakes.reporter
|
| 13 |
-
import
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
issues = []
|
| 25 |
|
| 26 |
# Check for syntax errors
|
|
@@ -29,17 +39,31 @@ def detect_code_issues(code: str) -> list:
|
|
| 29 |
except SyntaxError as e:
|
| 30 |
issues.append(f"SyntaxError: {e.msg} at line {e.lineno}")
|
| 31 |
|
| 32 |
-
# Use Pyflakes to
|
| 33 |
output_stream = io.StringIO()
|
| 34 |
reporter = pyflakes.reporter.Reporter(output_stream, output_stream)
|
| 35 |
pyflakes.api.check(code, filename="<user_code>", reporter=reporter)
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
@tool
|
| 45 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
import ast
|
| 11 |
+
import io
|
| 12 |
import pyflakes.api
|
| 13 |
import pyflakes.reporter
|
| 14 |
+
from flake8.api import legacy as flake8
|
| 15 |
+
from mccabe import get_code_complexity
|
| 16 |
|
| 17 |
+
@tool
|
| 18 |
+
def analyze_code_complexity(code: str) -> list:
|
| 19 |
+
"""Analyzes the cyclomatic complexity of functions in the code."""
|
| 20 |
+
tree = ast.parse(code)
|
| 21 |
+
issues = []
|
| 22 |
|
| 23 |
+
for node in ast.walk(tree):
|
| 24 |
+
if isinstance(node, ast.FunctionDef):
|
| 25 |
+
complexity = get_code_complexity(node, max_complexity=10)
|
| 26 |
+
if complexity > 10:
|
| 27 |
+
issues.append(f"High complexity: Function '{node.name}' has a cyclomatic complexity of {complexity}")
|
| 28 |
+
|
| 29 |
+
return issues
|
| 30 |
+
|
| 31 |
+
@tool
|
| 32 |
+
def detect_code_issues(code: str) -> list:
|
| 33 |
+
"""Analyzes Python code for issues using AST, Pyflakes, Flake8, and McCabe."""
|
| 34 |
issues = []
|
| 35 |
|
| 36 |
# Check for syntax errors
|
|
|
|
| 39 |
except SyntaxError as e:
|
| 40 |
issues.append(f"SyntaxError: {e.msg} at line {e.lineno}")
|
| 41 |
|
| 42 |
+
# Use Pyflakes to detect undefined variables and unused imports
|
| 43 |
output_stream = io.StringIO()
|
| 44 |
reporter = pyflakes.reporter.Reporter(output_stream, output_stream)
|
| 45 |
pyflakes.api.check(code, filename="<user_code>", reporter=reporter)
|
| 46 |
+
pyflakes_output = output_stream.getvalue().strip()
|
| 47 |
+
|
| 48 |
+
if pyflakes_output:
|
| 49 |
+
issues.extend(pyflakes_output.split("\n"))
|
| 50 |
|
| 51 |
+
# Use Flake8 for style violations (PEP8)
|
| 52 |
+
flake8_style = flake8.get_style_guide(ignore=['E501']) # Ignore line length warnings
|
| 53 |
+
report = flake8_style.input_file(filename="<user_code>", lines=code.split("\n"))
|
| 54 |
+
if report.total_errors > 0:
|
| 55 |
+
issues.append(f"Flake8: Found {report.total_errors} style violations.")
|
| 56 |
|
| 57 |
+
# Analyze function complexity
|
| 58 |
+
issues.extend(analyze_code_complexity(code))
|
| 59 |
|
| 60 |
+
# Check for missing docstrings
|
| 61 |
+
tree = ast.parse(code)
|
| 62 |
+
for node in ast.walk(tree):
|
| 63 |
+
if isinstance(node, (ast.FunctionDef, ast.ClassDef)) and not ast.get_docstring(node):
|
| 64 |
+
issues.append(f"Missing docstring in {node.__class__.__name__}: '{node.name}'")
|
| 65 |
+
|
| 66 |
+
return issues
|
| 67 |
|
| 68 |
@tool
|
| 69 |
def get_current_time_in_timezone(timezone: str) -> str:
|