File: /var/www/virtual/spades/httpdocs/App_Access_Control/get_list.py
import sys
import os
from prettytable import PrettyTable
from texttable import Texttable
ALLOWED_IDS_DATA_FILE_NAME = "allowed_list.txt"
def Get_dict_of_Allowed_IDs():
Final_dict = dict()
# Try to Open Target Student Data File
try:
Allowed_IDs_File_Handle = open(ALLOWED_IDS_DATA_FILE_NAME, "r") # Open file
Allowed_IDs_Data = Allowed_IDs_File_Handle.read().split(",") # Get all name and ID data
Allowed_IDs_File_Handle.close() # Close File
except (FileNotFoundError):
#No File exists
print("\n[ERROR]: Student Data file '{}' doesnt exist!\n[INFO]: Making a new one..\n[INFO]: Exiting .. [Exit Reason: cannot continue with empty file]".format(ALLOWED_IDS_DATA_FILE_NAME))
#Create the file
NEW_Allowed_IDs_File_Handle = open(ALLOWED_IDS_DATA_FILE_NAME, "w")
#Close it immediately because we dont want to write any data to it
NEW_Allowed_IDs_File_Handle.close()
#Exit!
exit(1)
#EndTry
#For every name and ID
for index, Current_Allowed_ID in enumerate(Allowed_IDs_Data):
#Insert Name and ID as tuple in list as: ("Full_Name", "Full Name", ID)
Final_dict.update({index + 1 : Current_Allowed_ID})
#EndFor
#Return all student name and ID Tuple list
return Final_dict
#EndFunction
def Attempt_StudentsToCheck_Dataset_Generation():
Root_Dir = os.path.dirname(os.path.realpath(__file__))
Allowed_IDs_Dict = Get_dict_of_Allowed_IDs()
Allowed_IDs_Table = Texttable()
# Allowed_IDs_Table.field_names = ["Index", "ID"]
Allowed_IDs_Table.set_cols_align(["c", "c"])
Allowed_IDs_Table.add_row(["Index", "ID"])
for index, Current_Allowed_ID in Allowed_IDs_Dict.items():
Allowed_IDs_Table.add_row([index, Current_Allowed_ID])
#EndFor
print(Allowed_IDs_Table.draw())
return True
#EndFunction
def main():
if (Attempt_StudentsToCheck_Dataset_Generation()):
# print("\n[INFO]: Successfully Generated StudentsToCheck Dataset!\n[INFO]: Goodbye!")
pass
else:
pass
# print("\n[ERROR]: Could not Generate StudentsToCheck Dataset :(\n[INFO]: Exiting!")
#EndIf
#EndFunction
if __name__ == "__main__":
try:
main()
except (KeyboardInterrupt, SystemExit):
print("\n\n[ERROR]: Exiting\n[INFO]: GoodBye!")
exit()
#EndTry
#EndIf