Coverage for pyguymer3/return_link_list.py: 5%

22 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def return_link_list( 

5 path, 

6 /, 

7 *, 

8 allowHidden = False, 

9 debug = __debug__, 

10 ensureNFC = True, 

11 follow_symlinks = True, 

12): 

13 """Return a recursive list of link names in a directory. 

14 

15 This function returns a sorted list of link names recursively in a directory. 

16 

17 Parameters 

18 ---------- 

19 path : str 

20 the directory to search 

21 allowHidden : bool, optional 

22 allow hidden files 

23 debug : bool, optional 

24 print debug messages 

25 ensureNFC : bool, optional 

26 ensure that the Unicode encoding is NFC 

27 follow_symlinks : bool, optional 

28 follow symbolic links 

29 

30 Returns 

31 ------- 

32 ans : list of str 

33 the sorted list of link names 

34 

35 Notes 

36 ----- 

37 Copyright 2017 Thomas Guymer [1]_ 

38 

39 References 

40 ---------- 

41 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

42 """ 

43 

44 # Import standard modules ... 

45 import os 

46 

47 # Import sub-functions ... 

48 from .make_path_safe import make_path_safe 

49 

50 # Create empty list ... 

51 contents = [] 

52 

53 # Check it exists ... 

54 if os.path.exists(path): 

55 # Open folder ... 

56 with os.scandir(path) as scanObj: 

57 # Loop over folder contents ... 

58 for entry in scanObj: 

59 # Check if the user wants to perform debugging ... 

60 if debug: 

61 # Test if this part is hidden and print the full path for 

62 # identification ... 

63 if not allowHidden and entry.name.startswith("."): 

64 print(f"WARNING: \"{entry.path}\" is hidden") 

65 

66 # Test if this part is illegal and print the full path for 

67 # identification ... 

68 if not entry.name.startswith(".") and entry.name != make_path_safe(entry.name, allowHidden = allowHidden, ensureNFC = ensureNFC): 

69 print(f"WARNING: \"{entry.path}\" is illegal") 

70 

71 # Check if it might need following ... 

72 if entry.is_dir(follow_symlinks = follow_symlinks): 

73 # Check that the directory is list-able ... 

74 # NOTE: On 20/Aug/2022 this was (incorrectly, in my opinion) 

75 # returning False on regular folders on FreeBSD (but 

76 # not MacOS) when passed "follow_symlinks = False". 

77 if os.access(entry, os.X_OK): 

78 # Recursively run this function again and add to the 

79 # list ... 

80 contents += return_link_list( 

81 entry.path, 

82 allowHidden = allowHidden, 

83 debug = debug, 

84 ensureNFC = ensureNFC, 

85 follow_symlinks = follow_symlinks, 

86 ) 

87 elif debug: 

88 print(f"WARNING: \"{entry.path}\" cannot be listed") 

89 

90 # Check if it should be added to the list ... 

91 if entry.is_symlink(): 

92 # Add to the list ... 

93 contents.append(entry.path) 

94 elif debug: 

95 print(f"WARNING: \"{path}\" does not exist") 

96 

97 # Return sorted list ... 

98 return sorted(contents)