1
- import time
1
+ from time import time
2
2
from collections .abc import Iterator
3
3
from dataclasses import dataclass , field
4
4
from pathlib import Path
5
5
6
+ import structlog
7
+
6
8
from src .core .constants import TS_FOLDER_NAME
7
9
from src .core .library import Library , Entry
8
10
11
+ logger = structlog .get_logger (__name__ )
12
+
9
13
10
14
@dataclass
11
15
class RefreshDirTracker :
12
16
library : Library
13
- dir_file_count : int = 0
14
17
files_not_in_library : list [Path ] = field (default_factory = list )
15
18
16
19
@property
@@ -36,38 +39,40 @@ def save_new_files(self) -> Iterator[int]:
36
39
37
40
self .files_not_in_library = []
38
41
39
- def refresh_dir (self ) -> Iterator [int ]:
42
+ def refresh_dir (self , lib_path : Path ) -> Iterator [int ]:
40
43
"""Scan a directory for files, and add those relative filenames to internal variables."""
41
- if self .library .folder is None :
42
- raise ValueError ("No folder set." )
44
+ if self .library .library_dir is None :
45
+ raise ValueError ("No library directory set." )
43
46
44
- start_time = time .time ()
45
- self .files_not_in_library = []
46
- self .dir_file_count = 0
47
+ start_time_total = time ()
48
+ start_time_loop = time ()
47
49
48
- lib_path = self .library .folder .path
50
+ self .files_not_in_library = []
51
+ dir_file_count = 0
49
52
50
53
for path in lib_path .glob ("**/*" ):
51
54
str_path = str (path )
52
- if (
53
- path .is_dir ()
54
- or "$RECYCLE.BIN" in str_path
55
- or TS_FOLDER_NAME in str_path
56
- or "tagstudio_thumbs" in str_path
57
- ):
55
+ if path .is_dir ():
58
56
continue
59
57
60
- suffix = path .suffix .lower ().lstrip ("." )
61
- if suffix in self .library .ignored_extensions :
58
+ if "$RECYCLE.BIN" in str_path or TS_FOLDER_NAME in str_path :
62
59
continue
63
60
64
- self . dir_file_count += 1
61
+ dir_file_count += 1
65
62
relative_path = path .relative_to (lib_path )
66
63
# TODO - load these in batch somehow
67
64
if not self .library .has_path_entry (relative_path ):
68
65
self .files_not_in_library .append (relative_path )
69
66
70
- end_time = time .time ()
71
67
# Yield output every 1/30 of a second
72
- if (end_time - start_time ) > 0.034 :
73
- yield self .dir_file_count
68
+ if (time () - start_time_loop ) > 0.034 :
69
+ yield dir_file_count
70
+ start_time_loop = time ()
71
+
72
+ end_time_total = time ()
73
+ logger .info (
74
+ "Directory scan time" ,
75
+ path = lib_path ,
76
+ duration = (end_time_total - start_time_total ),
77
+ new_files_count = dir_file_count ,
78
+ )
0 commit comments