generated from BrenoFariasdaSilva/Template-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
155 lines (116 loc) · 6.75 KB
/
utils.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os # For running a command in the terminal
import platform # For getting the operating system name
import sys # For exiting the program
from colorama import Style # For coloring the terminal
from dotenv import load_dotenv # For loading .env files
# Macros:
class BackgroundColors: # Colors for the terminal
CYAN = "\033[96m" # Cyan
GREEN = "\033[92m" # Green
YELLOW = "\033[93m" # Yellow
RED = "\033[91m" # Red
BOLD = "\033[1m" # Bold
UNDERLINE = "\033[4m" # Underline
CLEAR_TERMINAL = "\033[H\033[J" # Clear the terminal
# Execution Constants:
VERBOSE = False # Verbose mode. If set to True, it will output messages at the start/call of each function (Note: It will output a lot of messages).
# File Path Constants:
START_PATH = os.getcwd() # The starting path
# Input/Output Directory Constants:
OUTPUT_DIRECTORY = f"{START_PATH}/Outputs/" # The path to the output directory
# Sound Constants:
SOUND_COMMANDS = {"Darwin": "afplay", "Linux": "aplay", "Windows": "start"} # The commands to play a sound for each operating system
SOUND_FILE = "./.assets/Sounds/NotificationSound.wav" # The path to the sound file
def verbose_output(true_string="", false_string=""):
"""
Outputs a message if the VERBOSE constant is set to True.
:param true_string: The string to be outputted if VERBOSE is True.
:param false_string: The string to be outputted if VERBOSE is False.
:return: None
"""
if VERBOSE and true_string != "": # If VERBOSE is True and the true_string is not empty
print(true_string) # Output the true_string
elif false_string != "": # If the false_string is not empty
print(false_string) # Output the false_string
def verify_filepath_exists(filepath):
"""
Verify if a file or folder exists at the specified path.
:param filepath: Path to the file or folder
:return: True if the file or folder exists, False otherwise
"""
verbose_output(f"{BackgroundColors.YELLOW}Verifying if the file or folder exists at the path: {BackgroundColors.CYAN}{filepath}{Style.RESET_ALL}") # Output the verbose message
return os.path.exists(filepath) # Return True if the file or folder exists, False otherwise
def verify_env_file(env_path=None, key=None):
"""
Verify if the .env file exists and if the desired key is present.
:param env_path: Path to the .env file.
:param key: The key to get in the .env file.
:return: The value of the key if it exists.
"""
verbose_output(true_string=f"{BackgroundColors.GREEN}Verifying the .env file...{Style.RESET_ALL}") # Output the verification message
if env_path is None: # If the env_path parameter is not set
print(f"{BackgroundColors.RED}The {BackgroundColors.CYAN}env_path{BackgroundColors.RED} parameter is not set.{Style.RESET_ALL}")
sys.exit(1) # Exit the program
if key is None: # If the key parameter is not set
print(f"{BackgroundColors.RED}The {BackgroundColors.CYAN}key{BackgroundColors.RED} parameter is not set.{Style.RESET_ALL}")
sys.exit(1) # Exit the program
if not verify_filepath_exists(env_path): # If the .env file does not exist
print(f"{BackgroundColors.RED}.env file not found at {BackgroundColors.CYAN}{env_path}{Style.RESET_ALL}") # Output the error message
sys.exit(1) # Exit the program
load_dotenv(env_path) # Load the .env file
api_key = os.getenv(key) # Get the value of the key
if not api_key: # If the key does not exist
print(f"{BackgroundColors.RED}Key {BackgroundColors.CYAN}{key}{BackgroundColors.RED} not found in .env file.{Style.RESET_ALL}") # Output the error message
sys.exit(1) # Exit the program
return api_key # Return the API key
def create_directory(full_directory_name, relative_directory_name=""):
"""
Creates a directory.
:param full_directory_name: Name of the directory to be created.
:param relative_directory_name: Relative name of the directory to be created that will be shown in the terminal.
:return: None
"""
verbose_output(true_string=f"{BackgroundColors.GREEN}Creating the {BackgroundColors.CYAN}{relative_directory_name}{BackgroundColors.GREEN} directory...{Style.RESET_ALL}")
relative_directory_name = full_directory_name.replace(START_PATH, "") if relative_directory_name == "" else relative_directory_name # Get the relative directory name if it is not provided
if os.path.isdir(full_directory_name): # Verify if the directory already exists
verbose_output(f"{BackgroundColors.YELLOW}The directory already exists at the path: {BackgroundColors.CYAN}{relative_directory_name}{Style.RESET_ALL}") # Output the verbose message
return # Return if the directory already exists
try: # Try to create the directory
os.makedirs(full_directory_name) # Create the directory
except OSError: # If the directory cannot be created
print(f"{BackgroundColors.GREEN}The creation of the {BackgroundColors.CYAN}{relative_directory_name}{BackgroundColors.GREEN} directory failed.{Style.RESET_ALL}")
def write_output_to_file(output, file_path):
"""
Writes the chat output to a file.
:param output: The output text.
:param file_path: Path to the file.
:return: None
"""
verbose_output(true_string=f"{BackgroundColors.GREEN}Writing the output to the file...{Style.RESET_ALL}") # Output the writing message
with open(file_path, "w") as file: # Open the file
file.write(output) # Write the output
def play_sound():
"""
Plays a sound when the program finishes.
:return: None
"""
if verify_filepath_exists(SOUND_FILE): # If the sound file exists
if platform.system() in SOUND_COMMANDS: # If the platform.system() is in the SOUND_COMMANDS dictionary
os.system(f"{SOUND_COMMANDS[platform.system()]} {SOUND_FILE}") # Play the sound
else: # If the platform.system() is not in the SOUND_COMMANDS dictionary
print(f"{BackgroundColors.RED}The {BackgroundColors.CYAN}platform.system(){BackgroundColors.RED} is not in the {BackgroundColors.CYAN}SOUND_COMMANDS dictionary{BackgroundColors.RED}. Please add it!{Style.RESET_ALL}")
else: # If the sound file does not exist
print(f"{BackgroundColors.RED}Sound file {BackgroundColors.CYAN}{SOUND_FILE}{BackgroundColors.RED} not found. Make sure the file exists.{Style.RESET_ALL}")
def main():
"""
Main function.
:return: None
"""
print(f"{BackgroundColors.CLEAR_TERMINAL}{BackgroundColors.BOLD}{BackgroundColors.GREEN}Welcome to the {BackgroundColors.CYAN}Utils{BackgroundColors.GREEN} program!{Style.RESET_ALL}") # Output the welcome message
print(f"{BackgroundColors.BOLD}{BackgroundColors.GREEN}Program finished.{Style.RESET_ALL}") # Output the end of the program message
if __name__ == "__main__":
"""
This is the standard boilerplate that calls the main() function.
:return: None
"""
main() # Call the main function