Spaces:
Sleeping
Sleeping
Download tools/code_search.py from KrilinZ/neuronalis: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/KrilinZ/neuronalis/resolve/main/tools/code_search.py
- Command line
-
hf download hf://spaces/KrilinZ/neuronalis/tools/code_search.py
-
curl -L -o code_search.py https://huggingface.co/spaces/KrilinZ/neuronalis/resolve/main/tools/code_search.py
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}" | |