refactor: improve code readability in environment tools

Refactored the file_search_tool.py, grep_tool.py, and list_dir_tool.py to enhance code readability. Changes include formatting list comprehensions for better clarity, adjusting error handling for consistency, and removing unnecessary imports. These improvements aim to streamline the codebase and maintain a clean structure for future development.
This commit is contained in:
lorenzejay
2026-01-08 16:13:07 -08:00
parent 6c2dfdff56
commit d0e4c356e1
3 changed files with 12 additions and 10 deletions

View File

@@ -91,7 +91,9 @@ Examples:
matches = [m for m in matches if m.is_dir()]
# Filter out hidden files
matches = [m for m in matches if not any(part.startswith(".") for part in m.parts)]
matches = [
m for m in matches if not any(part.startswith(".") for part in m.parts)
]
# Sort alphabetically
matches.sort(key=lambda x: str(x).lower())

View File

@@ -120,7 +120,8 @@ Examples:
output = result.stdout
# Count actual match lines (not context lines)
match_lines = [
line for line in output.split("\n")
line
for line in output.split("\n")
if line and not line.startswith("--")
]
match_count = len(match_lines)
@@ -129,18 +130,19 @@ Examples:
header += "=" * 60 + "\n"
return header + output
elif result.returncode == 1:
if result.returncode == 1:
# No matches found (grep returns 1 for no matches)
return f"No matches found for '{pattern}' in {path}"
else:
# Error occurred
error_msg = result.stderr.strip() if result.stderr else "Unknown error"
return f"Error: {error_msg}"
# Error occurred
error_msg = result.stderr.strip() if result.stderr else "Unknown error"
return f"Error: {error_msg}"
except subprocess.TimeoutExpired:
return "Error: Search timed out (>30s). Try narrowing the search path."
except FileNotFoundError:
return "Error: grep command not found. Ensure grep is installed on the system."
return (
"Error: grep command not found. Ensure grep is installed on the system."
)
except Exception as e:
return f"Error during search: {e}"

View File

@@ -2,8 +2,6 @@
from __future__ import annotations
from pathlib import Path
from pydantic import BaseModel, Field
from crewai.experimental.environment_tools.base_environment_tool import (