@@ -737,7 +737,9 @@ def _map_filenames_to_entry_ids(self):
737
737
"""Maps a full filepath to its corresponding Entry's ID."""
738
738
self .filename_to_entry_id_map .clear ()
739
739
for entry in self .entries :
740
- self .filename_to_entry_id_map [(entry .path / entry .filename )] = entry .id
740
+ self .filename_to_entry_id_map [
741
+ (self .library_dir / entry .path / entry .filename )
742
+ ] = entry .id
741
743
742
744
# def _map_filenames_to_entry_ids(self):
743
745
# """Maps the file paths of entries to their index in the library list."""
@@ -884,59 +886,71 @@ def refresh_dir(self) -> Generator:
884
886
885
887
# Scans the directory for files, keeping track of:
886
888
# - Total file count
887
- # - Files without library entries
888
- # for type in TYPES:
889
- start_time = time .time ()
889
+ # - Files without Library entries
890
+ start_time_total = time .time ()
891
+ start_time_loop = time .time ()
892
+ ext_set = set (self .ext_list ) # Should be slightly faster
890
893
for f in self .library_dir .glob ("**/*" ):
894
+ end_time_loop = time .time ()
895
+ # Yield output every 1/30 of a second
896
+ if (end_time_loop - start_time_loop ) > 0.034 :
897
+ yield self .dir_file_count
898
+ start_time_loop = time .time ()
891
899
try :
900
+ # Skip this file if it should be excluded
901
+ ext : str = f .suffix .lower ()
902
+ if (ext in ext_set and self .is_exclude_list ) or (
903
+ ext not in ext_set and not self .is_exclude_list
904
+ ):
905
+ continue
906
+
907
+ # Finish if the file/path is already mapped in the Library
908
+ if self .filename_to_entry_id_map .get (f ) is not None :
909
+ # No other checks are required.
910
+ self .dir_file_count += 1
911
+ continue
912
+
913
+ # If the file is new, check for validity
892
914
if (
893
- "$RECYCLE.BIN" not in f .parts
894
- and TS_FOLDER_NAME not in f .parts
895
- and "tagstudio_thumbs" not in f .parts
896
- and not f .is_dir ()
915
+ "$RECYCLE.BIN" in f .parts
916
+ or TS_FOLDER_NAME in f .parts
917
+ or "tagstudio_thumbs" in f .parts
918
+ or f .is_dir ()
897
919
):
898
- if f .suffix .lower () not in self .ext_list and self .is_exclude_list :
899
- self .dir_file_count += 1
900
- file = f .relative_to (self .library_dir )
901
- if file not in self .filename_to_entry_id_map :
902
- self .files_not_in_library .append (file )
903
- elif f .suffix .lower () in self .ext_list and not self .is_exclude_list :
904
- self .dir_file_count += 1
905
- file = f .relative_to (self .library_dir )
906
- try :
907
- _ = self .filename_to_entry_id_map [file ]
908
- except KeyError :
909
- # print(file)
910
- self .files_not_in_library .append (file )
920
+ continue
921
+
922
+ # Add the validated new file to the Library
923
+ self .dir_file_count += 1
924
+ self .files_not_in_library .append (f )
925
+
911
926
except PermissionError :
912
- logging .info (
913
- f"The File/Folder { f } cannot be accessed, because it requires higher permission!"
914
- )
915
- end_time = time .time ()
916
- # Yield output every 1/30 of a second
917
- if (end_time - start_time ) > 0.034 :
918
- yield self .dir_file_count
919
- start_time = time .time ()
920
- # Sorts the files by date modified, descending.
927
+ logging .info (f'[LIBRARY] Cannot access "{ f } ": PermissionError' )
928
+
929
+ yield self .dir_file_count
930
+ end_time_total = time .time ()
931
+ logging .info (
932
+ f"[LIBRARY] Scanned directories in { (end_time_total - start_time_total ):.3f} seconds"
933
+ )
934
+ # Sorts the files by date modified, descending
921
935
if len (self .files_not_in_library ) <= 150000 :
922
936
try :
923
937
if platform .system () == "Windows" or platform .system () == "Darwin" :
924
938
self .files_not_in_library = sorted (
925
939
self .files_not_in_library ,
926
- key = lambda t : - (self . library_dir / t ).stat ().st_birthtime , # type: ignore[attr-defined]
940
+ key = lambda t : - (t ).stat ().st_birthtime , # type: ignore[attr-defined]
927
941
)
928
942
else :
929
943
self .files_not_in_library = sorted (
930
944
self .files_not_in_library ,
931
- key = lambda t : - (self . library_dir / t ).stat ().st_ctime ,
945
+ key = lambda t : - (t ).stat ().st_ctime ,
932
946
)
933
947
except (FileExistsError , FileNotFoundError ):
934
- print (
935
- "[LIBRARY] [ERROR] Couldn't sort files, some were moved during the scanning/sorting process."
948
+ logging . info (
949
+ "[LIBRARY][ERROR] Couldn't sort files, some were moved during the scanning/sorting process."
936
950
)
937
951
pass
938
952
else :
939
- print (
953
+ logging . info (
940
954
"[LIBRARY][INFO] Not bothering to sort files because there's OVER 150,000! Better sorting methods will be added in the future."
941
955
)
942
956
@@ -957,7 +971,7 @@ def remove_entry(self, entry_id: int) -> None:
957
971
# Step [1/2]:
958
972
# Remove this Entry from the Entries list.
959
973
entry = self .get_entry (entry_id )
960
- path = entry .path / entry .filename
974
+ path = self . library_dir / entry .path / entry .filename
961
975
# logging.info(f'Removing path: {path}')
962
976
963
977
del self .filename_to_entry_id_map [path ]
@@ -1087,8 +1101,8 @@ def refresh_dupe_files(self, results_filepath: str | Path):
1087
1101
)
1088
1102
)
1089
1103
for match in matches :
1090
- file_1 = files [match [0 ]]. relative_to ( self . library_dir )
1091
- file_2 = files [match [1 ]]. relative_to ( self . library_dir )
1104
+ file_1 = files [match [0 ]]
1105
+ file_2 = files [match [1 ]]
1092
1106
1093
1107
if (
1094
1108
file_1 in self .filename_to_entry_id_map .keys ()
@@ -1289,8 +1303,7 @@ def add_new_files_as_entries(self) -> list[int]:
1289
1303
"""Adds files from the `files_not_in_library` list to the Library as Entries. Returns list of added indices."""
1290
1304
new_ids : list [int ] = []
1291
1305
for file in self .files_not_in_library :
1292
- path = Path (file )
1293
- # print(os.path.split(file))
1306
+ path = Path (* file .parts [len (self .library_dir .parts ) :])
1294
1307
entry = Entry (
1295
1308
id = self ._next_entry_id , filename = path .name , path = path .parent , fields = []
1296
1309
)
@@ -1301,8 +1314,6 @@ def add_new_files_as_entries(self) -> list[int]:
1301
1314
self .files_not_in_library .clear ()
1302
1315
return new_ids
1303
1316
1304
- self .files_not_in_library .clear ()
1305
-
1306
1317
def get_entry (self , entry_id : int ) -> Entry :
1307
1318
"""Returns an Entry object given an Entry ID."""
1308
1319
return self .entries [self ._entry_id_to_index_map [int (entry_id )]]
@@ -1323,9 +1334,7 @@ def get_entry_id_from_filepath(self, filename: Path):
1323
1334
"""Returns an Entry ID given the full filepath it points to."""
1324
1335
try :
1325
1336
if self .entries :
1326
- return self .filename_to_entry_id_map [
1327
- Path (filename ).relative_to (self .library_dir )
1328
- ]
1337
+ return self .filename_to_entry_id_map [filename ]
1329
1338
except KeyError :
1330
1339
return - 1
1331
1340
0 commit comments