cornsnake.util_validate
Functions for checking and validating configuration settings in the config.py file.
1""" 2Functions for checking and validating configuration settings in the config.py file. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_validate.html) 5""" 6 7import os 8import re 9 10from . import config 11from . import util_date 12 13 14def _check_is_boolean(value, name): 15 """ 16 Check if a value is a boolean. 17 18 Args: 19 value (bool): The value to check. 20 name (str): The name of the value. 21 22 Returns: 23 str: An error message if value is not a boolean, None otherwise. 24 """ 25 if not isinstance(value, bool): 26 return f"{name} must be a boolean (True or False)" 27 28 return None 29 30 31def _check_is_string_or_empty(value, name): 32 """ 33 Check if a value is a string and not empty. 34 35 Args: 36 value (str): The value to check. 37 name (str): The name of the value. 38 39 Returns: 40 str: An error message if value is not a string or empty, None otherwise. 41 """ 42 if not isinstance(value, str): 43 return f"{name} must be a string" 44 if len(value.strip()) != len(value): 45 return f"{name} must not have leading or trailing whitespace" 46 47 return None 48 49 50def _check_is_string_not_empty(value, name): 51 """Check if a value is a string and not empty. 52 Args: 53 value (str): The value to check. 54 name (str): The name of the value. 55 Returns: 56 str: An error message if value is not a string or empty, None otherwise.""" 57 error = _check_is_string_or_empty(value, name) 58 if error: 59 return error 60 if len(value.strip()) == 0: 61 return f"{name} must not be empty" 62 return None 63 64 65def _check_is_list_of_strings_or_empty(value, name): 66 """Check if a value is a list of strings or empty. 67 Args: 68 value (list): The value to check. 69 name (str): The name of the value. 70 Returns: 71 str: An error message if value is not a list or contains non-string elements, None otherwise.""" 72 if not isinstance(value, list): 73 return f"{name} must be a list!" 74 for val in value: 75 error = _check_is_string_not_empty(val, f"Value in {name}") 76 if error: 77 return error 78 return None 79 80 81def _check_is_path_to_file_or_directory(value, name): 82 """Check if a value is a path to an existing file or directory. 83 Args: 84 value (str): The value to check. 85 name (str): The name of the value. 86 Returns: 87 str: An error message if value is not a valid path, None otherwise.""" 88 error = _check_is_string_not_empty(value, name) 89 if error: 90 return error 91 if not os.path.exists(value): 92 return f"The value of {name} must be a path to an existing file" 93 return None 94 95 96def _check_is_path_to_file(value, name): 97 """Check if a value is a path to an existing file. 98 Args: 99 value (str): The value to check. 100 name (str): The name of the value. 101 Returns: 102 str: An error message if value is not a valid file path, None otherwise.""" 103 error = _check_is_path_to_file_or_directory(value, name) 104 if error: 105 return error 106 if not os.path.isfile(value): 107 return f"The value of {name} must be a path to a file (not a directory)" 108 return None 109 110 111def _check_is_path_to_dir(value, name): 112 """Check if a value is a path to an existing directory. 113 Args: 114 value (str): The value to check. 115 name (str): The name of the value. 116 Returns: 117 str: An error message if value is not a valid directory path, None otherwise.""" 118 error = _check_is_path_to_file_or_directory(value, name) 119 if error: 120 return error 121 if not os.path.isdir(value): 122 return f"The value of {name} must be a path to a directory (not a file)" 123 return None 124 125 126def _check_is_path_to_dir_or_empty(value, name): 127 """Check if a value is a path to an existing directory or empty. 128 Args: 129 value (str): The value to check. 130 name (str): The name of the value. 131 Returns: 132 str: An error message if value is not a valid directory path or empty, None otherwise.""" 133 if value is None or len(value) == 0: 134 return None 135 return _check_is_path_to_dir(value, name) 136 137 138def _check_is_date_or_none(value, name): 139 """Check if a value is a date or empty. 140 Args: 141 value (str): The value to check. 142 name (str): The name of the value. 143 Returns: 144 str: An error message if value is not a valid date or empty, None otherwise.""" 145 if value is None or len(value) == 0: 146 return None 147 error_message = f"{name} must be a date in format 'yy-mm-dd'" 148 try: 149 date_value = util_date.parse_yyyy_mm_dd(value) 150 if not date_value: 151 return error_message 152 except Exception: 153 return error_message 154 return None 155 156 157def is_valid_blob_size(value): 158 """Check if a value is a valid blob size. 159 Args: 160 value (str): The value to check. 161 Returns: 162 bool: True if the value is a valid blob size, False otherwise.""" 163 if not value or len(value) == 0: 164 return False 165 pat = re.compile(r"[0-9]+[KMG]+") 166 return re.fullmatch(pat, value) 167 168 169def _check_is_blob_size(value, name): 170 """Check if a value is a string and a valid blob size. 171 Args: 172 value (str): The value to check. 173 name (str): The name of the value. 174 Returns: 175 str: An error message if value is not a valid blob size, None otherwise.""" 176 """Example of how to check configuration""" 177 error = _check_is_string_not_empty(value, name) 178 if error: 179 return error 180 if not is_valid_blob_size(value): 181 return f"{name} must be like 256K or 1M or 1G" 182 return None 183 184 185# Example of how to check configuration 186def _get_config_error(): 187 """Validate settings in config.py 188 Returns: 189 str: An error message if any configuration value is invalid, None otherwise.""" 190 """Validate settings in config.py""" 191 # required 192 error = _check_is_boolean(config.IS_VERBOSE, "IS_VERBOSE") 193 if error is not None: 194 return error 195 196 error = _check_is_path_to_file(config.PATH_TO_GIT, "PATH_TO_GIT") 197 if error is not None: 198 return error 199 200 # optional 201 error = _check_is_date_or_none(config.date_param, "date_param") 202 if error is not None: 203 return error 204 205 error = _check_is_path_to_dir_or_empty(config.path_to_repo_dir, "path_to_repo_dir") 206 if error is not None: 207 return error 208 209 error = _check_is_list_of_strings_or_empty(config.branches, "branches") 210 if error is not None: 211 return error 212 213 error = _check_is_string_or_empty(config.file_extensions, "file_extensions") 214 if error is not None: 215 return error 216 217 error = _check_is_blob_size(config.blob_size, "blob_size") 218 if error is not None: 219 return error 220 221 222# Validate settings in config.py 223def validate(): 224 """Validate settings in config.py 225 Raises: 226 SystemExit: If any configuration value is invalid, with an error message. 227 """ 228 """Validate settings in config.py""" 229 error = _get_config_error() 230 if error is not None: 231 raise SystemExit( 232 f"CONFIG ERROR: {error}. Please check the settings in config.py" 233 )
def
is_valid_blob_size(value):
158def is_valid_blob_size(value): 159 """Check if a value is a valid blob size. 160 Args: 161 value (str): The value to check. 162 Returns: 163 bool: True if the value is a valid blob size, False otherwise.""" 164 if not value or len(value) == 0: 165 return False 166 pat = re.compile(r"[0-9]+[KMG]+") 167 return re.fullmatch(pat, value)
Check if a value is a valid blob size. Args: value (str): The value to check. Returns: bool: True if the value is a valid blob size, False otherwise.
def
validate():
224def validate(): 225 """Validate settings in config.py 226 Raises: 227 SystemExit: If any configuration value is invalid, with an error message. 228 """ 229 """Validate settings in config.py""" 230 error = _get_config_error() 231 if error is not None: 232 raise SystemExit( 233 f"CONFIG ERROR: {error}. Please check the settings in config.py" 234 )
Validate settings in config.py Raises: SystemExit: If any configuration value is invalid, with an error message.