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