feat: use interactive notifications in file watcher with deferral support
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
import threading
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEventHandler
|
||||||
from git_monitor.logger import logger
|
from git_monitor.logger import logger
|
||||||
from git_monitor.git_manager import git_manager
|
from git_monitor.git_manager import git_manager
|
||||||
|
from git_monitor.notifier import notifier
|
||||||
|
|
||||||
class RepositoryWatcher:
|
class RepositoryWatcher:
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
@@ -43,27 +45,35 @@ class GitEventHandler(FileSystemEventHandler):
|
|||||||
self.handle_event(event, action, custom_file_name=file_name)
|
self.handle_event(event, action, custom_file_name=file_name)
|
||||||
|
|
||||||
def handle_event(self, event, action, custom_file_name=None):
|
def handle_event(self, event, action, custom_file_name=None):
|
||||||
# Ignore .git directory and the log file
|
|
||||||
file_path = event.src_path if action != "rename" else event.dest_path
|
file_path = event.src_path if action != "rename" else event.dest_path
|
||||||
file_name = os.path.basename(file_path)
|
file_name = os.path.basename(file_path)
|
||||||
|
|
||||||
# Check if any part of the path is .git or if it's the log file
|
|
||||||
if ".git" in file_path.split(os.sep) or file_name == "git_monitor.log":
|
if ".git" in file_path.split(os.sep) or file_name == "git_monitor.log":
|
||||||
return
|
return
|
||||||
|
|
||||||
# NEW: Respect .gitignore
|
|
||||||
if git_manager.is_ignored(file_path):
|
if git_manager.is_ignored(file_path):
|
||||||
logger.info(f"Ignored: {file_path} (matches .gitignore)")
|
logger.info(f"Ignored: {file_path} (matches .gitignore)")
|
||||||
return
|
return
|
||||||
|
|
||||||
display_name = custom_file_name if custom_file_name else file_name
|
display_name = custom_file_name if custom_file_name else file_name
|
||||||
logger.info(f"File event: {action} on {display_name}")
|
logger.info(f"Change detected: {action} on {display_name}")
|
||||||
|
|
||||||
repo_root = git_manager.repo_path
|
repo_root = git_manager.repo_path
|
||||||
if repo_root:
|
if repo_root:
|
||||||
if custom_file_name:
|
rel_path = os.path.relpath(file_path, repo_root) if not custom_file_name else custom_file_name
|
||||||
# For renames, we use the custom format provided
|
|
||||||
git_manager.commit_change(action, custom_file_name)
|
# Interactive notification instead of immediate commit
|
||||||
else:
|
def save_now():
|
||||||
rel_path = os.path.relpath(file_path, repo_root)
|
|
||||||
git_manager.commit_change(action, rel_path)
|
git_manager.commit_change(action, rel_path)
|
||||||
|
|
||||||
|
def ask_later():
|
||||||
|
logger.info(f"User deferred change: {rel_path} for 5 minutes.")
|
||||||
|
# Reschedule the same event after 5 minutes (300 seconds)
|
||||||
|
threading.Timer(300, self.handle_event, [event, action, custom_file_name]).start()
|
||||||
|
|
||||||
|
notifier.notify_interactive(
|
||||||
|
"Zmiana wykryta!",
|
||||||
|
f"Wykryto zmianę: {action}: {display_name}. Co chcesz zrobić?",
|
||||||
|
on_save=save_now,
|
||||||
|
on_later=ask_later
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user