sp-hydra-veil-core/core/utils/search_tools.py

49 lines
1.3 KiB
Python

import time
def search_for_phrase(
phrase: str,
textfile_path: str,
timeout: int) -> bool:
"""
Monitor a text file for a phrase.
Args:
phrase: The phrase to search for
textfile_path: Path to the text file to monitor
timeout: Maximum time in seconds to search
Returns:
True if phrase is found, False if timeout exceeded
"""
start_time = time.monotonic()
last_position = 0
while True:
# Check if timeout has been exceeded
elapsed = time.monotonic() - start_time
if elapsed >= timeout:
return False
try:
# Open file and read from last known position
with open(textfile_path, 'r') as f:
f.seek(last_position)
new_content = f.read()
# Check if phrase is in new content
if phrase in new_content:
return True
# Update position for next iteration
last_position = f.tell()
except FileNotFoundError:
# File doesn't exist yet, continue trying
pass
except Exception as e:
# Handle other file errors gracefully
pass
# Wait before checking again
time.sleep(0.2)