Coverage for src/bob/bio/vein/script/validate.py: 0%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-12 23:27 +0200

1#!/usr/bin/env python 

2# vim: set fileencoding=utf-8 : 

3 

4 

5"""Utilities for command-line option validation""" 

6 

7 

8import glob 

9import logging 

10import os 

11 

12import schema 

13 

14logger = logging.getLogger(__name__) 

15 

16 

17def setup_logger(name, level): 

18 """Sets up and checks a verbosity level respects min and max boundaries 

19 

20 

21 Parameters: 

22 

23 name (str): The name of the logger to setup 

24 

25 v (int): A value indicating the verbosity that must be set 

26 

27 

28 Returns: 

29 

30 logging.Logger: A standard Python logger that can be used to log messages 

31 

32 

33 Raises: 

34 

35 schema.SchemaError: If the verbosity level exceeds the maximum allowed of 4 

36 

37 """ 

38 

39 import clapper.logging 

40 

41 logger = clapper.logging.setup(name) 

42 

43 if not (0 <= level < 4): 

44 raise schema.SchemaError( 

45 "there can be only up to 3 -v's in a command-line" 

46 ) 

47 

48 # Sets-up logging 

49 clapper.logging.set_verbosity_level(logger, level) 

50 

51 return logger 

52 

53 

54def make_dir(p): 

55 """Checks if a path exists, if it doesn't, creates it 

56 

57 

58 Parameters: 

59 

60 p (str): The path to check 

61 

62 

63 Returns 

64 

65 bool: ``True``, always 

66 

67 """ 

68 

69 if not os.path.exists(p): 

70 logger.info("Creating directory `%s'...", p) 

71 os.makedirs(p) 

72 

73 return True 

74 

75 

76def check_path_does_not_exist(p): 

77 """Checks if a path exists, if it does, raises an exception 

78 

79 

80 Parameters: 

81 

82 p (str): The path to check 

83 

84 

85 Returns: 

86 

87 bool: ``True``, always 

88 

89 

90 Raises: 

91 

92 schema.SchemaError: if the path exists 

93 

94 """ 

95 

96 if os.path.exists(p): 

97 raise schema.SchemaError("path to {} exists".format(p)) 

98 

99 return True 

100 

101 

102def check_path_exists(p): 

103 """Checks if a path exists, if it doesn't, raises an exception 

104 

105 

106 Parameters: 

107 

108 p (str): The path to check 

109 

110 

111 Returns: 

112 

113 bool: ``True``, always 

114 

115 

116 Raises: 

117 

118 schema.SchemaError: if the path doesn't exist 

119 

120 """ 

121 

122 if not os.path.exists(p): 

123 raise schema.SchemaError("path to {} does not exist".format(p)) 

124 

125 return True 

126 

127 

128def check_model_does_not_exist(p): 

129 """Checks if the path to any potential model file does not exist 

130 

131 

132 Parameters: 

133 

134 p (str): The path to check 

135 

136 

137 Returns: 

138 

139 bool: ``True``, always 

140 

141 

142 Raises: 

143 

144 schema.SchemaError: if the path exists 

145 

146 """ 

147 

148 files = glob.glob(p + ".*") 

149 if files: 

150 raise schema.SchemaError("{} already exists".format(files)) 

151 

152 return True 

153 

154 

155def open_multipage_pdf_file(s): 

156 """Returns an opened matplotlib multi-page file 

157 

158 

159 Parameters: 

160 

161 p (str): The path to the file to open 

162 

163 

164 Returns: 

165 

166 matplotlib.backends.backend_pdf.PdfPages: with the handle to the multipage 

167 PDF file 

168 

169 

170 Raises: 

171 

172 schema.SchemaError: if the path exists 

173 

174 """ 

175 from matplotlib.backends.backend_pdf import PdfPages 

176 

177 return PdfPages(s)