Coverage for src/drive_sync_directory.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-03 03:08 +0000

1"""Drive sync directory orchestration. 

2 

3This module provides the main sync directory coordination functionality, 

4orchestrating folder processing, file collection, and parallel downloads per SRP. 

5""" 

6 

7__author__ = "Mandar Patil (mandarons@pm.me)" 

8 

9import unicodedata 

10from typing import Any 

11 

12from src import config_parser, configure_icloudpy_logging, get_logger, recycle_bin, sync_drive 

13from src.drive_cleanup import remove_obsolete 

14from src.drive_filtering import wanted_parent_folder 

15from src.drive_folder_processing import process_folder 

16from src.drive_parallel_download import collect_file_for_download, execute_parallel_downloads 

17 

18# Configure icloudpy logging immediately after import 

19configure_icloudpy_logging() 

20 

21LOGGER = get_logger() 

22 

23 

24def sync_directory( 

25 drive: Any, 

26 destination_path: str, 

27 items: Any, 

28 root: str, 

29 top: bool = True, 

30 filters: dict[str, list[str]] | None = None, 

31 ignore: list[str] | None = None, 

32 remove: bool = False, 

33 config: Any | None = None, 

34) -> set[str]: 

35 """Synchronize a directory from iCloud Drive to local filesystem. 

36 

37 This function orchestrates the entire sync process by: 

38 1. Processing folders and recursively syncing subdirectories 

39 2. Collecting files for parallel download 

40 3. Executing parallel downloads 

41 4. Cleaning up obsolete files if requested 

42 

43 Args: 

44 drive: iCloud drive service instance 

45 destination_path: Local destination directory 

46 items: iCloud items to process 

47 root: Root directory for relative path calculations 

48 top: Whether this is the top-level sync call 

49 filters: Dictionary of filters (folders, file_extensions) 

50 ignore: List of ignore patterns 

51 remove: Whether to remove obsolete local files 

52 config: Configuration object 

53 

54 Returns: 

55 Set of all processed file paths 

56 """ 

57 files = set() 

58 download_tasks = [] 

59 

60 if not (drive and destination_path and items and root): 

61 return files 

62 

63 # First pass: process folders and collect download tasks 

64 for i in items: 

65 item = drive[i] 

66 

67 if item.type in ("folder", "app_library"): 

68 _process_folder_item( 

69 item, 

70 destination_path, 

71 filters, 

72 ignore, 

73 root, 

74 files, 

75 config, 

76 ) 

77 elif item.type == "file": 

78 _process_file_item( 

79 item, 

80 destination_path, 

81 filters, 

82 ignore, 

83 root, 

84 files, 

85 download_tasks, 

86 config, 

87 ) 

88 

89 # Second pass: execute downloads in parallel 

90 if download_tasks: 

91 _execute_downloads(download_tasks, config) 

92 

93 # Final cleanup if this is the top-level call 

94 if top and remove: 

95 remove_obsolete( 

96 destination_path=destination_path, 

97 files=files, 

98 recycle_bin=recycle_bin.from_config(config, "drive"), 

99 exclude_filenames={config_parser.get_mount_marker_filename(config or {})}, 

100 ) 

101 

102 return files 

103 

104 

105def _process_folder_item( 

106 item: Any, 

107 destination_path: str, 

108 filters: dict[str, list[str]] | None, 

109 ignore: list[str] | None, 

110 root: str, 

111 files: set[str], 

112 config: Any | None, 

113) -> None: 

114 """Process a single folder item. 

115 

116 Args: 

117 item: iCloud folder item 

118 destination_path: Local destination directory 

119 filters: Dictionary of filters 

120 ignore: List of ignore patterns 

121 root: Root directory 

122 files: Set to update with processed files 

123 config: Configuration object 

124 """ 

125 new_folder = process_folder( 

126 item=item, 

127 destination_path=destination_path, 

128 filters=filters["folders"] if filters and "folders" in filters else None, 

129 ignore=ignore, 

130 root=root, 

131 ) 

132 if not new_folder: 

133 return 

134 

135 try: 

136 files.add(unicodedata.normalize("NFC", new_folder)) 

137 # Recursively sync subdirectory 

138 subdirectory_files = sync_directory( 

139 drive=item, 

140 destination_path=new_folder, 

141 items=item.dir(), 

142 root=root, 

143 top=False, 

144 filters=filters, 

145 ignore=ignore, 

146 config=config, 

147 ) 

148 files.update(subdirectory_files) 

149 except Exception: 

150 # Continue execution to next item, without crashing the app 

151 pass 

152 

153 

154def _process_file_item( 

155 item: Any, 

156 destination_path: str, 

157 filters: dict[str, list[str]] | None, 

158 ignore: list[str] | None, 

159 root: str, 

160 files: set[str], 

161 download_tasks: list[dict[str, Any]], 

162 config: Any | None = None, 

163) -> None: 

164 """Process a single file item. 

165 

166 Args: 

167 item: iCloud file item 

168 destination_path: Local destination directory 

169 filters: Dictionary of filters 

170 ignore: List of ignore patterns 

171 root: Root directory 

172 files: Set to update with processed files 

173 download_tasks: List to append download tasks to 

174 config: Configuration object 

175 """ 

176 if not wanted_parent_folder( 

177 filters=filters["folders"] if filters and "folders" in filters else None, 

178 ignore=ignore, 

179 root=root, 

180 folder_path=destination_path, 

181 ): 

182 return 

183 

184 try: 

185 download_info = collect_file_for_download( 

186 item=item, 

187 destination_path=destination_path, 

188 filters=filters["file_extensions"] if filters and "file_extensions" in filters else None, 

189 ignore=ignore, 

190 files=files, 

191 config=config, 

192 ) 

193 if download_info: 

194 download_tasks.append(download_info) 

195 except Exception: 

196 # Continue execution to next item, without crashing the app 

197 pass 

198 

199 

200def _execute_downloads(download_tasks: list[dict[str, Any]], config: Any) -> None: 

201 """Execute parallel downloads. 

202 

203 Args: 

204 download_tasks: List of download task dictionaries 

205 config: Configuration object 

206 """ 

207 max_threads = sync_drive.get_max_threads(config) 

208 execute_parallel_downloads(download_tasks, max_threads)