Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
"""
|
|
LS tool - Directory listing
|
|
"""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
import fnmatch
|
|
from typing import Dict, Any, List
|
|
from .base import BaseTool
|
|
|
|
|
|
class LSTool(BaseTool):
|
|
"""Lists files and directories"""
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return "LS"
|
|
|
|
def _execute_impl(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""
|
|
List directory contents
|
|
|
|
- The path parameter must be an absolute path
|
|
- You can optionally provide an array of glob patterns to ignore
|
|
"""
|
|
path = Path(params["path"]).expanduser().resolve()
|
|
ignore_patterns = params.get("ignore")
|
|
if ignore_patterns is None:
|
|
ignore_patterns = []
|
|
|
|
if not path.exists():
|
|
return {"error": f"Path not found: {path}"}
|
|
|
|
if not path.is_dir():
|
|
return {"error": f"Not a directory: {path}"}
|
|
|
|
try:
|
|
entries = []
|
|
|
|
for entry in sorted(path.iterdir()):
|
|
# Skip hidden files (starting with .)
|
|
if entry.name.startswith('.'):
|
|
continue
|
|
|
|
# Check ignore patterns
|
|
should_ignore = False
|
|
for pattern in ignore_patterns:
|
|
if fnmatch.fnmatch(entry.name, pattern):
|
|
should_ignore = True
|
|
break
|
|
|
|
if should_ignore:
|
|
continue
|
|
|
|
# Get entry info
|
|
entry_type = "dir" if entry.is_dir() else "file"
|
|
size = entry.stat().st_size if entry.is_file() else 0
|
|
|
|
entries.append({
|
|
"name": entry.name,
|
|
"type": entry_type,
|
|
"size": size,
|
|
"path": str(entry)
|
|
})
|
|
|
|
return {
|
|
"path": str(path),
|
|
"entries": entries,
|
|
"total_entries": len(entries)
|
|
}
|
|
|
|
except PermissionError:
|
|
return {"error": f"Permission denied: {path}"}
|
|
except Exception as e:
|
|
return {"error": f"Error listing directory: {str(e)}"}
|
|
|