re-format terminal output information
This commit is contained in:
@@ -4,7 +4,6 @@ from py7zr import SevenZipFile, exceptions
|
|||||||
|
|
||||||
from utils.settings import BAD_DIR_NAME
|
from utils.settings import BAD_DIR_NAME
|
||||||
|
|
||||||
|
|
||||||
def mark_file_as_BAD(file: str, bad_exception: Exception) -> None:
|
def mark_file_as_BAD(file: str, bad_exception: Exception) -> None:
|
||||||
try:
|
try:
|
||||||
filename = os.path.basename(file)
|
filename = os.path.basename(file)
|
||||||
@@ -12,10 +11,9 @@ def mark_file_as_BAD(file: str, bad_exception: Exception) -> None:
|
|||||||
os.makedirs(bad_dir, exist_ok=True)
|
os.makedirs(bad_dir, exist_ok=True)
|
||||||
bad_file_path = os.path.join(bad_dir, filename)
|
bad_file_path = os.path.join(bad_dir, filename)
|
||||||
shutil.move(file, bad_file_path)
|
shutil.move(file, bad_file_path)
|
||||||
print(f'[Warning] Found BAD compressed file: {filename}\nMoved to: {bad_file_path}\nError message: {bad_exception}')
|
print(f'\n[Warning] Found BAD compressed file: {filename}\nMoved to: {bad_file_path}\nError message: {bad_exception}')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'[Error] {e}')
|
print(f'\n[Error] {e}')
|
||||||
|
|
||||||
|
|
||||||
def extract_zip(zip_file: str, target_dir: str) -> None | Exception:
|
def extract_zip(zip_file: str, target_dir: str) -> None | Exception:
|
||||||
try:
|
try:
|
||||||
@@ -26,10 +24,9 @@ def extract_zip(zip_file: str, target_dir: str) -> None | Exception:
|
|||||||
except zipfile.BadZipfile as e:
|
except zipfile.BadZipfile as e:
|
||||||
mark_file_as_BAD(zip_file, e)
|
mark_file_as_BAD(zip_file, e)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'[ERROR] Something went wrong while extracting the contents of a submitted zip file. Check the error message, get student id and download / organise manually\nError message: {e}')
|
print(f'\n[ERROR] Something went wrong while extracting the contents of a submitted zip file. Check the error message, get student id and download / organise manually\nError message: {e}')
|
||||||
return e
|
return e
|
||||||
|
|
||||||
|
|
||||||
def extract_rar(rar_file: str, target_dir: str) -> None:
|
def extract_rar(rar_file: str, target_dir: str) -> None:
|
||||||
try:
|
try:
|
||||||
with rarfile.RarFile(rar_file, 'r') as rar_ref:
|
with rarfile.RarFile(rar_file, 'r') as rar_ref:
|
||||||
@@ -48,10 +45,9 @@ def extract_rar(rar_file: str, target_dir: str) -> None:
|
|||||||
except rarfile.NotRarFile as e:
|
except rarfile.NotRarFile as e:
|
||||||
mark_file_as_BAD(rar_file, e)
|
mark_file_as_BAD(rar_file, e)
|
||||||
except rarfile.RarCannotExec as e:
|
except rarfile.RarCannotExec as e:
|
||||||
print('[Error] Missing unrar tool\nfor Windows: make sure file UnRAR.exe exists in directory \'utils\'\nfor Linux/Mac: need to install unrar (check README)')
|
print('\n[Error] Missing unrar tool\nfor Windows: make sure file UnRAR.exe exists in directory \'utils\'\nfor Linux/Mac: need to install unrar (check README)')
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
def extract_7z(seven_zip_file: str, target_dir: str) -> None:
|
def extract_7z(seven_zip_file: str, target_dir: str) -> None:
|
||||||
try: # extract the 7z file using py7zr
|
try: # extract the 7z file using py7zr
|
||||||
with open(seven_zip_file, 'rb') as f:
|
with open(seven_zip_file, 'rb') as f:
|
||||||
@@ -67,7 +63,6 @@ def extract_7z(seven_zip_file: str, target_dir: str) -> None:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
mark_file_as_BAD(seven_zip_file, e)
|
mark_file_as_BAD(seven_zip_file, e)
|
||||||
|
|
||||||
|
|
||||||
def extract_file_to_dir(file_path: str, student_dir: str) -> None | Exception:
|
def extract_file_to_dir(file_path: str, student_dir: str) -> None | Exception:
|
||||||
os.makedirs(student_dir, exist_ok=True) # create the subdirectory for student
|
os.makedirs(student_dir, exist_ok=True) # create the subdirectory for student
|
||||||
|
|
||||||
@@ -78,4 +73,4 @@ def extract_file_to_dir(file_path: str, student_dir: str) -> None | Exception:
|
|||||||
elif file_path.lower().endswith('.7z'):
|
elif file_path.lower().endswith('.7z'):
|
||||||
extract_7z(file_path, student_dir)
|
extract_7z(file_path, student_dir)
|
||||||
else:
|
else:
|
||||||
print(f"[Error] unknown file type: {file_path}")
|
print(f"\n[Error] unknown file type: {file_path}")
|
||||||
|
|||||||
Reference in New Issue
Block a user