Coverage for NeuralTSNE/NeuralTSNE/Utils/Validators/FileTypeValidators/filetype_validators.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-18 16:32 +0000

1import argparse 

2 

3 

4class FileTypeWithExtensionCheck(argparse.FileType): 

5 """ 

6 Custom `argparse.FileType` with additional extension validation. 

7 

8 Parameters 

9 ---------- 

10 `mode` : `str`, optional 

11 File mode. Defaults to `r`. 

12 `valid_extensions` : `str` | `Tuple[str, ...]`, optional 

13 Valid file extensions. 

14 `**kwargs` 

15 Additional keyword arguments. 

16 

17 Note 

18 ---- 

19 This class extends `argparse.FileType` to include validation of file extensions. 

20 """ 

21 

22 def __init__(self, mode="r", valid_extensions=None, **kwargs): 

23 super().__init__(mode, **kwargs) 

24 self.valid_extensions = valid_extensions 

25 

26 def __call__(self, string): 

27 """ 

28 Validate the file extension before calling the parent `__call__` method. 

29 

30 Parameters 

31 ---------- 

32 `string` : `str` 

33 Input string representing the filename. 

34 

35 Returns 

36 ------- 

37 `file` 

38 File object. 

39 

40 Note 

41 ---- 

42 This method performs additional validation on the file extension before calling 

43 the parent `__call__` method from `argparse.FileType`. 

44 """ 

45 if self.valid_extensions: 

46 if not string.endswith(self.valid_extensions): 

47 raise argparse.ArgumentTypeError("Not a valid filename extension!") 

48 return super().__call__(string) 

49 

50 

51class FileTypeWithExtensionCheckWithPredefinedDatasets(FileTypeWithExtensionCheck): 

52 """ 

53 Custom `argparse.FileType` with additional extension and predefined dataset validation. 

54 

55 Parameters 

56 ---------- 

57 `mode` : `str`, optional 

58 File mode. Defaults to `r`. 

59 `valid_extensions` : `str` | `Tuple[str, ...]`, optional 

60 Valid file extensions. 

61 `available_datasets` : `List[str]`, optional 

62 List of available datasets. 

63 `**kwargs` 

64 Additional keyword arguments. 

65 

66 Note 

67 ---- 

68 This class extends `FileTypeWithExtensionCheck` to include validation of predefined datasets. 

69 """ 

70 

71 def __init__( 

72 self, mode="r", valid_extensions=None, available_datasets=None, **kwargs 

73 ): 

74 super().__init__(mode, valid_extensions, **kwargs) 

75 self.available_datasets = available_datasets or [] 

76 

77 def __call__(self, string): 

78 """ 

79 Validate the file extension and predefined dataset before calling the parent `__call__` method. 

80 

81 Parameters 

82 ---------- 

83 `string` : `str` 

84 Input string representing the filename. 

85 

86 Returns 

87 ------- 

88 `file` | `str` 

89 File object or predefined dataset name. 

90 

91 Note 

92 ---- 

93 This method performs additional validation on the file extension and predefined dataset before calling 

94 the parent `__call__` method from `FileTypeWithExtensionCheck`. 

95 """ 

96 if len(self.available_datasets) > 0 and string in self.available_datasets: 

97 return string 

98 return super().__call__(string)