-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.py
91 lines (78 loc) · 3.57 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
from yaml import safe_load as load
import sys
from time import strftime
import time
import stackprinter
stackprinter.set_excepthook(style='darkbg')
from loguru import logger
from src.backend import bootstrap_browser, bootstrap_login_page
from src.lib.exceptions import UserCausedHalt
from src.lib.logcreation import formatter, formatter_sensitive
def load_configuration(configuration_file_path='user/cfg.yml') -> dict:
"""
Loads a YAML configuration file and returns a dictionary of the configuration.
:param configuration_file_path: A string representing the path to the YAML configuration file to be loaded. Default is 'user/cfg.yml'.
:type configuration_file_path: str
:return: A dictionary representing the loaded YAML configuration file.
:rtype: dict
"""
with open(configuration_file_path, "r") as configuration_file:
config = load(configuration_file)
formatting = formatter
if config['sensitiveDebug'] == "True":
formatting = formatter_sensitive
if config['logCreation'] == "True":
logger.add("user/Logs/{0}.log".format(strftime("%d-%m-%Y-%H_%M_%S", time.localtime(time.time()))), colorize=False, backtrace=True, format=formatting)
logger.add(sys.stderr, format=formatting, colorize=True, backtrace=True)
logger.debug(f'Loaded configuration file located at {os.path.realpath(configuration_file.name)}')
return config
def userFacing(configuration: dict):
"""
Takes a configuration dictionary `configuration` as input and prompts the user to select a program mode and code type(s) they want to generate. Then, it checks whether the inputted program mode and code mode are valid or not. If the inputted modes are valid, it starts the simulated browser and runs it in an infinite loop.
:param configuration: A dictionary that contains the configuration for the function.
:type configuration: dict
:raises ValueError: If the inputted program mode or code mode is invalid.
:return: None
:rtype: None
"""
#To-do: move the below stuff to documentation.
# Ask the user what program mode they want to use
#programModePromptText = (
# f"Enter the program mode which you want to generate codes for."
# f"\n[{color(' Login ', 'green')}] - login"
# f"\n[{color('Password Reset', 'green')}] - reset\n:"
#)
# Ask the user what code type(s) they want to use
#codeModePromptText = (
# f"What kind(s) of codes would you like to generate?"
# f"\n[{color('normal','green')}] - {color('6','green')} Digit Normal Code"
# f"\n[{color('backup','green')}] - {color('8','green')} Digit Backup Code"
# f"\n[{color(' both','cyan') }] - Both {color('6','green')} Digit Normal Codes and {color('8','green')} Digit Backup Codes\n:"
#)
# Check whether the inputted program mode is valid
validProgramModes: set = {
'login',
'reset'
}
if configuration['programMode'].lower() not in validProgramModes: raise ValueError("Invalid program mode inputted!")
# Check whether the inputted code mode is valid
validCodeModes: set = {
'normal',
'backup',
'backup_let',
'both'
}
if configuration['codeMode'].lower() not in validCodeModes: raise ValueError('Invalid code-generation mode inputted!')
# Start the simulated browser, and run it in an infinite loop.
while True:
driver = bootstrap_browser(configuration)
bootstrap_login_page(driver, configuration)
if __name__ == '__main__':
try:
logger.remove()
userFacing(load_configuration())
except UserCausedHalt:
# Exit procedure taken from: https://stackoverflow.com/a/21144662
logger.info('User halted the program!')
sys.exit(130)