Spaces:
Sleeping

neuronalis / tools /code_search.py
Nacho
HF deploy
5a6158c
Raw History Blame Contribute Delete
1.71 kB
import os
import re
def grep_search(directory, pattern, case_insensitive=True):
"""Searches for a regex pattern in files."""
results = []
flags = re.IGNORECASE if case_insensitive else 0
try:
for root, _, filenames in os.walk(directory):
if '.git' in root or '__pycache__' in root or 'node_modules' in root:
continue
for filename in filenames:
filepath = os.path.join(root, filename)
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
for i, line in enumerate(lines):
if re.search(pattern, line, flags):
results.append(f"{filepath}:{i+1}: {line.strip()}")
except Exception:
continue # Skip binary or unreadable files
return "\n".join(results[:50]) # Limit results
except Exception as e:
return f"Error searching: {e}"
def find_files(directory, pattern):
"""Finds files matching a glob pattern."""
# Simple implementation using glob not actually needed since we have walker
# Let's just fuzzy match name
results = []
try:
for root, _, filenames in os.walk(directory):
if '.git' in root or '__pycache__' in root:
continue
for filename in filenames:
if pattern.lower() in filename.lower():
results.append(os.path.join(root, filename))
return "\n".join(results[:50])
except Exception as e:
return f"Error finding files: {e}"