Coverage for src/hardlink_registry.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-16 04:41 +0000

1"""Hardlink registry management module. 

2 

3This module contains utilities for managing hardlink registry 

4during photo synchronization to avoid duplicate downloads. 

5""" 

6 

7___author___ = "Mandar Patil <mandarons@pm.me>" 

8 

9 

10from src import get_logger 

11 

12LOGGER = get_logger() 

13 

14 

15class HardlinkRegistry: 

16 """Registry to track downloaded photos for hardlink creation. 

17 

18 This class manages a registry of downloaded photos to enable hardlink 

19 creation for duplicate photos across different albums. 

20 """ 

21 

22 def __init__(self): 

23 """Initialize hardlink registry.""" 

24 self._registry: dict[str, str] = {} 

25 

26 def get_existing_path(self, photo_id: str, file_size: str) -> str | None: 

27 """Get existing path for photo if it was already downloaded. 

28 

29 Args: 

30 photo_id: Unique photo identifier 

31 file_size: File size variant (original, medium, thumb, etc.) 

32 

33 Returns: 

34 Path to existing file if found, None otherwise 

35 """ 

36 photo_key = f"{photo_id}_{file_size}" 

37 return self._registry.get(photo_key) 

38 

39 def register_photo_path(self, photo_id: str, file_size: str, file_path: str) -> None: 

40 """Register a downloaded photo path for future hardlink creation. 

41 

42 Args: 

43 photo_id: Unique photo identifier 

44 file_size: File size variant (original, medium, thumb, etc.) 

45 file_path: Path where photo was downloaded 

46 """ 

47 photo_key = f"{photo_id}_{file_size}" 

48 self._registry[photo_key] = file_path 

49 

50 def get_registry_size(self) -> int: 

51 """Get number of registered photos. 

52 

53 Returns: 

54 Number of photos in the registry 

55 """ 

56 return len(self._registry) 

57 

58 def clear(self) -> None: 

59 """Clear the registry.""" 

60 self._registry.clear() 

61 

62 

63def create_hardlink_registry(use_hardlinks: bool) -> HardlinkRegistry | None: 

64 """Create hardlink registry if hardlinks are enabled. 

65 

66 Args: 

67 use_hardlinks: Whether hardlinks are enabled in configuration 

68 

69 Returns: 

70 HardlinkRegistry instance if hardlinks enabled, None otherwise 

71 """ 

72 return HardlinkRegistry() if use_hardlinks else None