code cleanup

This commit is contained in:
phernandez
2025-02-21 22:11:54 -06:00
parent 51b4eb32c5
commit eb4a55a5e7
8 changed files with 21 additions and 67 deletions
+2 -3
View File
@@ -14,13 +14,12 @@ from basic_memory.cli.app import app
from basic_memory.cli.commands.sync import get_sync_service
from basic_memory.config import config
from basic_memory.sync import SyncService
from basic_memory.sync.utils import SyncReport
from basic_memory.sync.sync_service import SyncReport
# Create rich console
console = Console()
def add_files_to_tree(
tree: Tree, paths: Set[str], style: str, checksums: Dict[str, str] | None = None
):
@@ -141,4 +140,4 @@ def status(
asyncio.run(run_status(sync_service, verbose)) # pragma: no cover
except Exception as e:
logger.exception(f"Error checking status: {e}")
raise typer.Exit(code=1) # pragma: no cover
raise typer.Exit(code=1) # pragma: no cover
+2 -3
View File
@@ -26,7 +26,7 @@ from basic_memory.services import EntityService, FileService
from basic_memory.services.link_resolver import LinkResolver
from basic_memory.services.search_service import SearchService
from basic_memory.sync import SyncService
from basic_memory.sync.utils import SyncReport
from basic_memory.sync.sync_service import SyncReport
from basic_memory.sync.watch_service import WatchService
console = Console()
@@ -58,7 +58,6 @@ async def get_sync_service(): # pragma: no cover
search_service = SearchService(search_repository, entity_repository, file_service)
link_resolver = LinkResolver(entity_repository, search_service)
# Initialize services
entity_service = EntityService(
entity_parser,
@@ -76,7 +75,7 @@ async def get_sync_service(): # pragma: no cover
entity_repository=entity_repository,
relation_repository=relation_repository,
search_service=search_service,
file_service=file_service
file_service=file_service,
)
return sync_service
+1 -1
View File
@@ -196,7 +196,7 @@ class FileService:
return await file_utils.compute_checksum(full_path.read_text())
async def file_stats(self, path: Union[Path, str]) -> stat_result:
def file_stats(self, path: Union[Path, str]) -> stat_result:
"""
Return file stats for a given path.
:param path:
+8 -18
View File
@@ -59,14 +59,6 @@ class ScanResult:
errors: Dict[str, str] = field(default_factory=dict)
@dataclass
class FileState:
"""State of a file including file path, permalink and checksum info."""
file_path: str
permalink: str
checksum: str
class SyncService:
"""Syncs documents and knowledge files with database."""
@@ -87,7 +79,7 @@ class SyncService:
self.search_service = search_service
self.file_service = file_service
async def get_db_file_state(self) -> Dict[str, FileState]:
async def get_db_file_state(self) -> Dict[str, str]:
"""Get file_path and checksums from database.
Args:
db_records: database records
@@ -97,9 +89,7 @@ class SyncService:
"""
db_records = await self.entity_repository.find_all()
return {
r.file_path: FileState(
file_path=r.file_path, permalink=r.permalink, checksum=r.checksum or ""
)
r.file_path: r.checksum or ""
for r in db_records
}
@@ -108,7 +98,7 @@ class SyncService:
with logfire.span("sync", directory=directory):
# initial paths from db to sync
# path -> FileState
# path -> checksum
db_paths = await self.get_db_file_state()
# Track potentially moved files by checksum
@@ -124,24 +114,24 @@ class SyncService:
report.checksums[file_path] = checksum
# Now detect moves and deletions
for db_path, db_state in db_paths.items():
for db_path, db_checksum in db_paths.items():
report.checksums[file_path] = checksum
local_checksum_for_db_path = scan_result.files.get(db_path)
# file not modified
if db_state.checksum == local_checksum_for_db_path:
if db_checksum == local_checksum_for_db_path:
pass
# if checksums don't match for the same path, its modified
if local_checksum_for_db_path and db_state.checksum != local_checksum_for_db_path:
if local_checksum_for_db_path and db_checksum != local_checksum_for_db_path:
report.modified.add(db_path)
# check if it's moved or deleted
if not local_checksum_for_db_path:
# if we find the checksum in another file, it's a move
if db_state.checksum in scan_result.checksums:
new_path = scan_result.checksums[db_state.checksum]
if db_checksum in scan_result.checksums:
new_path = scan_result.checksums[db_checksum]
report.moves[db_path] = new_path
# Remove from new files since it's a move
report.new.remove(new_path)
-31
View File
@@ -1,31 +0,0 @@
"""Types and utilities for file sync."""
from dataclasses import dataclass, field
from typing import Set, Dict
@dataclass
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
# We keep paths as strings in sets/dicts for easier serialization
new: Set[str] = field(default_factory=set)
modified: Set[str] = field(default_factory=set)
deleted: Set[str] = field(default_factory=set)
moves: Dict[str, str] = field(default_factory=dict) # old_path -> new_path
checksums: Dict[str, str] = field(default_factory=dict) # path -> checksum
@property
def total_changes(self) -> int:
"""Total number of changes."""
return len(self.new) + len(self.modified) + len(self.deleted) + len(self.moves)