44
55import structlog
66from src .core .library import Entry , Library
7-
8- IGNORE_ITEMS = [
9- "$recycle.bin" ,
10- ]
7+ from src .core .utils .refresh_dir import GLOBAL_IGNORE_SET
118
129logger = structlog .get_logger ()
1310
@@ -18,49 +15,73 @@ class MissingRegistry:
1815
1916 library : Library
2017 files_fixed_count : int = 0
21- missing_files : list [Entry ] = field (default_factory = list )
18+ missing_file_entries : list [Entry ] = field (default_factory = list )
2219
2320 @property
24- def missing_files_count (self ) -> int :
25- return len (self .missing_files )
21+ def missing_file_entries_count (self ) -> int :
22+ return len (self .missing_file_entries )
2623
2724 def refresh_missing_files (self ) -> Iterator [int ]:
28- """Track the number of Entries that point to an invalid file path ."""
29- logger .info ("refresh_missing_files running " )
30- self .missing_files = []
25+ """Track the number of entries that point to an invalid filepath ."""
26+ logger .info ("[ refresh_missing_files] Refreshing missing files... " )
27+ self .missing_file_entries = []
3128 for i , entry in enumerate (self .library .get_entries ()):
3229 full_path = self .library .library_dir / entry .path
3330 if not full_path .exists () or not full_path .is_file ():
34- self .missing_files .append (entry )
31+ self .missing_file_entries .append (entry )
3532 yield i
3633
37- def match_missing_file (self , match_item : Entry ) -> list [Path ]:
38- """Try to find missing entry files within the library directory.
34+ def match_missing_file_entry (self , match_entry : Entry ) -> list [Path ]:
35+ """Try and match unlinked file entries with matching results in the library directory.
3936
4037 Works if files were just moved to different subfolders and don't have duplicate names.
4138 """
4239 matches = []
43- for item in self .library .library_dir .glob (f"**/{ match_item .path .name } " ):
44- if item .name == match_item .path .name : # TODO - implement IGNORE_ITEMS
45- new_path = Path (item ).relative_to (self .library .library_dir )
40+ for path in self .library .library_dir .glob (f"**/{ match_entry .path .name } " ):
41+ # Ensure matched file isn't in a globally ignored folder
42+ skip : bool = False
43+ for part in path .parts :
44+ if part in GLOBAL_IGNORE_SET :
45+ skip = True
46+ break
47+ if skip :
48+ continue
49+ if path .name == match_entry .path .name :
50+ new_path = Path (path ).relative_to (self .library .library_dir )
4651 matches .append (new_path )
4752
53+ logger .info ("[MissingRegistry] Matches" , matches = matches )
4854 return matches
4955
50- def fix_missing_files (self ) -> Iterator [int ]:
51- """Attempt to fix missing files by finding a match in the library directory."""
56+ def fix_unlinked_entries (self ) -> Iterator [int ]:
57+ """Attempt to fix unlinked file entries by finding a match in the library directory."""
5258 self .files_fixed_count = 0
53- for i , entry in enumerate (self .missing_files , start = 1 ):
54- item_matches = self .match_missing_file (entry )
59+ matched_entries : list [Entry ] = []
60+ for i , entry in enumerate (self .missing_file_entries ):
61+ item_matches = self .match_missing_file_entry (entry )
5562 if len (item_matches ) == 1 :
56- logger .info ("fix_missing_files" , entry = entry , item_matches = item_matches )
57- self .library .update_entry_path (entry .id , item_matches [0 ])
63+ logger .info (
64+ "[fix_unlinked_entries]" ,
65+ entry = entry .path .as_posix (),
66+ item_matches = item_matches [0 ].as_posix (),
67+ )
68+ if not self .library .update_entry_path (entry .id , item_matches [0 ]):
69+ try :
70+ match = self .library .get_entry_full_by_path (item_matches [0 ])
71+ entry_full = self .library .get_entry_full (entry .id )
72+ self .library .merge_entries (entry_full , match )
73+ except AttributeError :
74+ continue
5875 self .files_fixed_count += 1
59- # remove fixed file
60- self .missing_files .remove (entry )
76+ matched_entries .append (entry )
6177 yield i
6278
79+ for entry in matched_entries :
80+ self .missing_file_entries .remove (entry )
81+
6382 def execute_deletion (self ) -> None :
64- self .library .remove_entries (list (map (lambda missing : missing .id , self .missing_files )))
83+ self .library .remove_entries (
84+ list (map (lambda missing : missing .id , self .missing_file_entries ))
85+ )
6586
66- self .missing_files = []
87+ self .missing_file_entries = []
0 commit comments