Coverage for src/drive_folder_processing.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-11-12 17:18 +0000

1"""Folder processing utilities. 

2 

3This module provides folder creation and processing functionality for 

4iCloud Drive sync operations, separating folder logic from sync operations per SRP. 

5""" 

6 

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

8 

9import os 

10import unicodedata 

11from typing import Any 

12from urllib.parse import unquote 

13 

14from src import configure_icloudpy_logging, get_logger 

15from src.drive_filtering import wanted_folder 

16 

17# Configure icloudpy logging immediately after import 

18configure_icloudpy_logging() 

19 

20LOGGER = get_logger() 

21 

22 

23def process_folder( 

24 item: Any, 

25 destination_path: str, 

26 filters: list[str] | None, 

27 ignore: list[str] | None, 

28 root: str, 

29) -> str | None: 

30 """Process a folder item by creating the local directory if wanted. 

31 

32 Args: 

33 item: iCloud folder item 

34 destination_path: Local destination directory 

35 filters: Folder filters to apply 

36 ignore: Ignore patterns 

37 root: Root directory for relative path calculations 

38 

39 Returns: 

40 Path to the created directory, or None if folder should be skipped 

41 """ 

42 if not (item and destination_path and root): 

43 return None 

44 

45 # Decode URL-encoded folder name from iCloud API 

46 # This handles special characters like %CC%88 (combining diacritical marks) 

47 decoded_name = unquote(item.name) 

48 new_directory = os.path.join(destination_path, decoded_name) 

49 new_directory_norm = unicodedata.normalize("NFC", new_directory) 

50 

51 if not wanted_folder(filters=filters, ignore=ignore, folder_path=new_directory_norm, root=root): 

52 LOGGER.debug(f"Skipping the unwanted folder {new_directory} ...") 

53 return None 

54 

55 os.makedirs(new_directory_norm, exist_ok=True) 

56 return new_directory