add file watcher to sync command

This commit is contained in:
phernandez
2025-02-02 20:07:04 -06:00
parent b7b69ecd84
commit a88fc99663
5 changed files with 45 additions and 24 deletions
+2 -1
View File
@@ -95,7 +95,7 @@ class FileChangeScanner:
current_files = scan_result.files
# Build report
report = SyncReport()
report = SyncReport(total=len(current_files))
# Track potentially moved files by checksum
files_by_checksum = {} # checksum -> file_path
@@ -127,6 +127,7 @@ class FileChangeScanner:
report.deleted.add(db_file_path)
# Log summary
logger.debug(f"Total files: {report.total}")
logger.debug(f"Changes found: {report.total_changes}")
logger.debug(f" New: {len(report.new)}")
logger.debug(f" Modified: {len(report.modified)}")
+3 -1
View File
@@ -41,12 +41,14 @@ class SyncReport:
"""Report of file changes found compared to database state.
Attributes:
total: Total number of files in directory being synced
new: Files that exist on disk but not in database
modified: Files that exist in both but have different checksums
deleted: Files that exist in database but not on disk
moves: Files that have been moved from one location to another
checksums: Current checksums for files on disk
"""
total: int = 0
new: Set[str] = field(default_factory=set)
modified: Set[str] = field(default_factory=set)
deleted: Set[str] = field(default_factory=set)
@@ -59,6 +61,6 @@ class SyncReport:
return len(self.new) + len(self.modified) + len(self.deleted) + len(self.moves)
@property
def total_files(self) -> int:
def syned_files(self) -> int:
"""Total number of files synced."""
return len(self.new) + len(self.modified) + len(self.moves)
+5 -3
View File
@@ -3,6 +3,7 @@
import json
import dataclasses
from loguru import logger
from pydantic import BaseModel
from pydantic.dataclasses import dataclass
from datetime import datetime
@@ -37,8 +38,7 @@ class WatchServiceState(BaseModel):
last_scan: Optional[datetime] = None
# File counts
total_files: int = 0
markdown_files: int = 0
synced_files: int = 0
# Recent activity
recent_events: List[WatchEvent] = dataclasses.field(default_factory=list)
@@ -78,6 +78,7 @@ class WatchService:
debounce=self.config.sync_delay,
recursive=True,
):
# just sync the whole dir
await self.handle_changes(self.config.home)
except Exception as e:
@@ -100,10 +101,11 @@ class WatchService:
"""Process a batch of file changes"""
try:
logger.debug(f"handling change in directory: {directory} ...")
# Process changes with timeout
report = await self.sync_service.sync(directory)
self.state.last_scan = datetime.now()
self.state.total_files = report.total_files
self.state.synced_files = report.total
# Update stats
for path in report.new: