cfbd_json_py.utls
1# Creation Date: 08/30/2023 01:13 EDT 2# Last Updated Date: 08/28/2024 02:10 PM EDT 3# Author: Joseph Armstrong (armstrongjoseph08@gmail.com) 4# File Name: utls.py 5# Purpose: Houses utility functions for this python package. 6############################################################################### 7import json 8import logging 9import os 10import secrets 11 12import keyring 13 14 15def reverse_cipher_encrypt(plain_text_str: str): 16 """ 17 NOT INTENDED TO BE CALLED BY THE USER! 18 19 Implements a reverse cipher encryption to a plain text string. 20 21 Parameters 22 ---------- 23 `plain_text_str` (mandatory, str): 24 The string you want to encrypt through reverse cipher encryption. 25 26 Returns 27 ---------- 28 A string encrypted through reverse cipher encryption. 29 """ 30 translated_text = "" 31 str_len = len(plain_text_str) - 1 32 while str_len >= 0: 33 translated_text = translated_text + plain_text_str[str_len] 34 str_len = str_len - 1 35 36 del plain_text_str 37 38 return translated_text 39 40 41def reverse_cipher_decrypt(encrypted_text_str: str): 42 """ 43 NOT INTENDED TO BE CALLED BY THE USER! 44 45 Decrypts a string that was presumed to 46 be encrypted by a reverse cipher encryption. 47 48 Parameters 49 ---------- 50 `encrypted_text_str` (mandatory, str): 51 The string you presume that is encrypted 52 through reverse cipher encryption, 53 that you want decrypted. 54 55 Returns 56 ---------- 57 A decrypted string. 58 59 """ 60 translated_text = "" 61 str_len = len(encrypted_text_str) - 1 62 63 while str_len >= 0: 64 translated_text = translated_text + encrypted_text_str[str_len] 65 str_len = str_len - 1 66 67 del encrypted_text_str 68 69 return translated_text 70 71 72def get_cfbd_api_token(api_key_dir: str = None): 73 """ 74 NOT INTENDED TO BE CALLED BY THE USER! 75 76 If you've already set the API key using 77 `cfbd_json_py.utls.set_cfbd_api_token()`, 78 you don't need to use this function. 79 80 If the CFBD API key exists in the environment, 81 or is in a file, this function Retrieves the CFBD API key, 82 and returns it as a string. 83 84 If this package is being used in a GitHub Actions action, 85 set the key in the environment by 86 creating a repository secret named `CFBD_API_KEY`. 87 88 Parameters 89 ---------- 90 `api_key_dir` (str, optional): 91 Optional argument. If `api_key_dir` is set to a non-null string, 92 `set_cfbd_api_token()` will attempt 93 to see if the API key is in that directory. 94 95 Returns 96 ---------- 97 A CFBD API key that exists within this python environment, 98 or within this computer. 99 100 """ 101 try: 102 key = keyring.get_password("cfbd_json_py", str(os.getlogin())) 103 if key is None or len(key) == 0: 104 raise KeyError( 105 "Could not locate a valid CFBD key." 106 ) 107 return key 108 except Exception: 109 logging.warning( 110 "Could not locate a CFBD key on this device normally, " + 111 "checking if the key exists within the environment. " + 112 "If you have previously set a key using `set_cfbd_api_token()`, " + 113 "you have nothing to worry about, because this function will " + 114 "automatically fix this issue." 115 ) 116 key = deprecated_get_cfbd_api_token(api_key_dir) 117 set_cfbd_api_token(key) 118 return key 119 120 121def deprecated_get_cfbd_api_token(api_key_dir: str = None): 122 """ 123 DEPRECATED! DO NOT USE!! 124 125 NOT INTENDED TO BE CALLED BY THE USER! 126 127 If you've already set the API key using 128 `cfbd_json_py.utls.set_cfbd_api_token()`, 129 you don't need to use this function. 130 131 If the CFBD API key exists in the environment, 132 or is in a file, this function Retrieves the CFBD API key, 133 and returns it as a string. 134 135 If this package is being used in a GitHub Actions action, 136 set the key in the environment by 137 creating a repository secret named `CFBD_API_KEY`. 138 139 Parameters 140 ---------- 141 `api_key_dir` (str, optional): 142 Optional argument. If `api_key_dir` is set to a non-null string, 143 `set_cfbd_api_token()` will attempt 144 to save the key file in that directory, 145 instead of this user's home directory. 146 147 Returns 148 ---------- 149 A CFBD API key that exists within this python environment, 150 or within this computer. 151 """ 152 # raise NotImplementedError('it ain\'t ready') 153 154 try: 155 key = os.environ["CFBD_API_KEY"] 156 return key 157 except Exception as e: 158 logging.info( 159 "CFBD key not found in this python environment.\n" + 160 f"Attempting to load the API key from a file.\nFull Exception: {e}" 161 ) 162 163 if api_key_dir is not None: 164 with open(f"{api_key_dir}/.cfbd/cfbd.json", "r") as f: 165 json_str = f.read() 166 167 json_data = json.loads(json_str) 168 169 return_key = json_data["cfbd_api_token"] 170 return_key = reverse_cipher_decrypt(return_key) 171 return_key = return_key[10:] 172 return_key = return_key[:-10] 173 174 del api_key_dir, json_str, json_data 175 176 return return_key 177 else: 178 home_dir = os.path.expanduser("~") 179 180 with open(f"{home_dir}/.cfbd/cfbd.json", "r") as f: 181 json_str = f.read() 182 183 json_data = json.loads(json_str) 184 185 return_key = json_data["cfbd_api_token"] 186 return_key = reverse_cipher_decrypt(return_key) 187 return_key = return_key[10:] 188 return_key = return_key[:-10] 189 190 del api_key_dir, json_str, json_data 191 192 return return_key 193 194 195def set_cfbd_api_token(api_key: str): 196 """ 197 Sets the CFBD API key for use by this python package. 198 199 Parameters 200 ---------- 201 `api_key` (str, mandatory): 202 The CFBD API key you have. 203 DO NOT input `Bearer {your CFBD API key}`, 204 this package will take care of that for you. 205 206 Returns 207 ---------- 208 Nothing. 209 This function only sets up the API 210 that this package can reference later. 211 212 """ 213 keyring.set_password("cfbd_json_py", str(os.getlogin()), api_key) 214 215 216def _set_cfbd_api_token(api_key: str, api_key_dir: str = None): 217 """ 218 Sets the CFBD API key into a file that exists 219 either in `{home_dir}/.cfbd/cfbd_key.json`, or in a custom directory. 220 221 Parameters 222 ---------- 223 `api_key` (str, mandatory): 224 The CFBD API key you have. 225 DO NOT input `Bearer {your CFBD API key}`, 226 this package will take care of that for you. 227 228 `api_key_dir` (str, optional): 229 Optional argument. If `api_key_dir` is set to a non-null string, 230 `set_cfbd_api_token()` will attempt 231 to save the key file in that directory, 232 instead of this user's home directory. 233 234 Returns 235 ---------- 236 Nothing. 237 This function only sets up the API key file 238 that this package can reference later. 239 """ 240 241 alph_letters = [ 242 "a", 243 "b", 244 "c", 245 "d", 246 "e", 247 "f", 248 "g", 249 "h", 250 "i", 251 "j", 252 "k", 253 "l", 254 "m", 255 "n", 256 "o", 257 "p", 258 "q", 259 "r", 260 "s", 261 "t", 262 "u", 263 "v", 264 "w", 265 "x", 266 "y", 267 "z", 268 "0", 269 "1", 270 "2", 271 "3", 272 "4", 273 "5", 274 "6", 275 "7", 276 "8", 277 "9", 278 "A", 279 "B", 280 "C", 281 "D", 282 "E", 283 "F", 284 "G", 285 "H", 286 "I", 287 "J", 288 "K", 289 "L", 290 "M", 291 "N", 292 "O", 293 "P", 294 "Q", 295 "R", 296 "S", 297 "T", 298 "U", 299 "V", 300 "W", 301 "X", 302 "Y", 303 "Z", 304 ] 305 306 front_hash = "" 307 back_hash = "" 308 309 for i in range(0, 10): 310 r_str = secrets.choice(alph_letters) 311 front_hash += r_str 312 del r_str 313 314 for i in range(0, 10): 315 r_str = secrets.choice(alph_letters) 316 back_hash += r_str 317 del r_str 318 319 encrypted_key = reverse_cipher_encrypt(api_key) 320 321 json_str = \ 322 f'{{\n\t"cfbd_api_token":"{front_hash}{encrypted_key}{back_hash}"\n}}' 323 del encrypted_key 324 # print(json_str) 325 326 if api_key_dir is not None: 327 try: 328 os.mkdir(f"{api_key_dir}/.cfbd") 329 except Exception as e: 330 logging.info( 331 f"./cfbd directory created/already exists. Full exception: {e}" 332 ) 333 334 with open(f"{api_key_dir}/.cfbd/cfbd.json", "w+") as f: 335 f.write(json_str) 336 else: 337 home_dir = os.path.expanduser("~") 338 339 try: 340 os.mkdir(f"{home_dir}/.cfbd") 341 except Exception as e: 342 logging.info( 343 f"./cfbd directory created/already exists. Full exception: {e}" 344 ) 345 346 with open(f"{home_dir}/.cfbd/cfbd.json", "w+") as f: 347 f.write(json_str) 348 349 del json_str 350 351 352# if __name__ == "__main__": 353# text = "Hello World" 354# e_text = reverse_cipher_encrypt(text) 355# ue_text = reverse_cipher_decrypt(e_text) 356 357# print(f"Original Text:\t{text}") 358# print(f"Encrypted Text:\t{e_text}") 359# print(f"Decrypted Text:\t{ue_text}") 360 361# print(f'remove first 2 characters from string: {text[2:]}') 362# print(f'remove last 2 characters from string: {text[:-2]}') 363 364# key = "hello world" 365# set_cfbd_api_token("text") 366# print(key) 367 368# if __name__ == "__main__": 369 370# return_key = get_cfbd_api_token() 371# print(return_key)
16def reverse_cipher_encrypt(plain_text_str: str): 17 """ 18 NOT INTENDED TO BE CALLED BY THE USER! 19 20 Implements a reverse cipher encryption to a plain text string. 21 22 Parameters 23 ---------- 24 `plain_text_str` (mandatory, str): 25 The string you want to encrypt through reverse cipher encryption. 26 27 Returns 28 ---------- 29 A string encrypted through reverse cipher encryption. 30 """ 31 translated_text = "" 32 str_len = len(plain_text_str) - 1 33 while str_len >= 0: 34 translated_text = translated_text + plain_text_str[str_len] 35 str_len = str_len - 1 36 37 del plain_text_str 38 39 return translated_text
NOT INTENDED TO BE CALLED BY THE USER!
Implements a reverse cipher encryption to a plain text string.
Parameters
plain_text_str
(mandatory, str):
The string you want to encrypt through reverse cipher encryption.
Returns
A string encrypted through reverse cipher encryption.
42def reverse_cipher_decrypt(encrypted_text_str: str): 43 """ 44 NOT INTENDED TO BE CALLED BY THE USER! 45 46 Decrypts a string that was presumed to 47 be encrypted by a reverse cipher encryption. 48 49 Parameters 50 ---------- 51 `encrypted_text_str` (mandatory, str): 52 The string you presume that is encrypted 53 through reverse cipher encryption, 54 that you want decrypted. 55 56 Returns 57 ---------- 58 A decrypted string. 59 60 """ 61 translated_text = "" 62 str_len = len(encrypted_text_str) - 1 63 64 while str_len >= 0: 65 translated_text = translated_text + encrypted_text_str[str_len] 66 str_len = str_len - 1 67 68 del encrypted_text_str 69 70 return translated_text
NOT INTENDED TO BE CALLED BY THE USER!
Decrypts a string that was presumed to be encrypted by a reverse cipher encryption.
Parameters
encrypted_text_str
(mandatory, str):
The string you presume that is encrypted
through reverse cipher encryption,
that you want decrypted.
Returns
A decrypted string.
73def get_cfbd_api_token(api_key_dir: str = None): 74 """ 75 NOT INTENDED TO BE CALLED BY THE USER! 76 77 If you've already set the API key using 78 `cfbd_json_py.utls.set_cfbd_api_token()`, 79 you don't need to use this function. 80 81 If the CFBD API key exists in the environment, 82 or is in a file, this function Retrieves the CFBD API key, 83 and returns it as a string. 84 85 If this package is being used in a GitHub Actions action, 86 set the key in the environment by 87 creating a repository secret named `CFBD_API_KEY`. 88 89 Parameters 90 ---------- 91 `api_key_dir` (str, optional): 92 Optional argument. If `api_key_dir` is set to a non-null string, 93 `set_cfbd_api_token()` will attempt 94 to see if the API key is in that directory. 95 96 Returns 97 ---------- 98 A CFBD API key that exists within this python environment, 99 or within this computer. 100 101 """ 102 try: 103 key = keyring.get_password("cfbd_json_py", str(os.getlogin())) 104 if key is None or len(key) == 0: 105 raise KeyError( 106 "Could not locate a valid CFBD key." 107 ) 108 return key 109 except Exception: 110 logging.warning( 111 "Could not locate a CFBD key on this device normally, " + 112 "checking if the key exists within the environment. " + 113 "If you have previously set a key using `set_cfbd_api_token()`, " + 114 "you have nothing to worry about, because this function will " + 115 "automatically fix this issue." 116 ) 117 key = deprecated_get_cfbd_api_token(api_key_dir) 118 set_cfbd_api_token(key) 119 return key
NOT INTENDED TO BE CALLED BY THE USER!
If you've already set the API key using
cfbd_json_py.utls.set_cfbd_api_token()
,
you don't need to use this function.
If the CFBD API key exists in the environment, or is in a file, this function Retrieves the CFBD API key, and returns it as a string.
If this package is being used in a GitHub Actions action,
set the key in the environment by
creating a repository secret named CFBD_API_KEY
.
Parameters
api_key_dir
(str, optional):
Optional argument. If api_key_dir
is set to a non-null string,
set_cfbd_api_token()
will attempt
to see if the API key is in that directory.
Returns
A CFBD API key that exists within this python environment, or within this computer.
122def deprecated_get_cfbd_api_token(api_key_dir: str = None): 123 """ 124 DEPRECATED! DO NOT USE!! 125 126 NOT INTENDED TO BE CALLED BY THE USER! 127 128 If you've already set the API key using 129 `cfbd_json_py.utls.set_cfbd_api_token()`, 130 you don't need to use this function. 131 132 If the CFBD API key exists in the environment, 133 or is in a file, this function Retrieves the CFBD API key, 134 and returns it as a string. 135 136 If this package is being used in a GitHub Actions action, 137 set the key in the environment by 138 creating a repository secret named `CFBD_API_KEY`. 139 140 Parameters 141 ---------- 142 `api_key_dir` (str, optional): 143 Optional argument. If `api_key_dir` is set to a non-null string, 144 `set_cfbd_api_token()` will attempt 145 to save the key file in that directory, 146 instead of this user's home directory. 147 148 Returns 149 ---------- 150 A CFBD API key that exists within this python environment, 151 or within this computer. 152 """ 153 # raise NotImplementedError('it ain\'t ready') 154 155 try: 156 key = os.environ["CFBD_API_KEY"] 157 return key 158 except Exception as e: 159 logging.info( 160 "CFBD key not found in this python environment.\n" + 161 f"Attempting to load the API key from a file.\nFull Exception: {e}" 162 ) 163 164 if api_key_dir is not None: 165 with open(f"{api_key_dir}/.cfbd/cfbd.json", "r") as f: 166 json_str = f.read() 167 168 json_data = json.loads(json_str) 169 170 return_key = json_data["cfbd_api_token"] 171 return_key = reverse_cipher_decrypt(return_key) 172 return_key = return_key[10:] 173 return_key = return_key[:-10] 174 175 del api_key_dir, json_str, json_data 176 177 return return_key 178 else: 179 home_dir = os.path.expanduser("~") 180 181 with open(f"{home_dir}/.cfbd/cfbd.json", "r") as f: 182 json_str = f.read() 183 184 json_data = json.loads(json_str) 185 186 return_key = json_data["cfbd_api_token"] 187 return_key = reverse_cipher_decrypt(return_key) 188 return_key = return_key[10:] 189 return_key = return_key[:-10] 190 191 del api_key_dir, json_str, json_data 192 193 return return_key
DEPRECATED! DO NOT USE!!
NOT INTENDED TO BE CALLED BY THE USER!
If you've already set the API key using
cfbd_json_py.utls.set_cfbd_api_token()
,
you don't need to use this function.
If the CFBD API key exists in the environment, or is in a file, this function Retrieves the CFBD API key, and returns it as a string.
If this package is being used in a GitHub Actions action,
set the key in the environment by
creating a repository secret named CFBD_API_KEY
.
Parameters
api_key_dir
(str, optional):
Optional argument. If api_key_dir
is set to a non-null string,
set_cfbd_api_token()
will attempt
to save the key file in that directory,
instead of this user's home directory.
Returns
A CFBD API key that exists within this python environment, or within this computer.
196def set_cfbd_api_token(api_key: str): 197 """ 198 Sets the CFBD API key for use by this python package. 199 200 Parameters 201 ---------- 202 `api_key` (str, mandatory): 203 The CFBD API key you have. 204 DO NOT input `Bearer {your CFBD API key}`, 205 this package will take care of that for you. 206 207 Returns 208 ---------- 209 Nothing. 210 This function only sets up the API 211 that this package can reference later. 212 213 """ 214 keyring.set_password("cfbd_json_py", str(os.getlogin()), api_key)
Sets the CFBD API key for use by this python package.
Parameters
api_key
(str, mandatory):
The CFBD API key you have.
DO NOT input Bearer {your CFBD API key}
,
this package will take care of that for you.
Returns
Nothing. This function only sets up the API that this package can reference later.