Skip to content

Commit

Permalink
handling error exit_with_error_exception
Browse files Browse the repository at this point in the history
  • Loading branch information
choglass committed Dec 31, 2024
1 parent 2c899a9 commit b124483
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
19 changes: 11 additions & 8 deletions cell2mol/final_c2m_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from cell2mol.refcell import process_refcell
from cell2mol.unitcell import process_unitcell
from cell2mol.xyz_molecule import get_molecule
from cell2mol.read_write import prefilter_cif, exit_with_error
from cell2mol.read_write import prefilter_cif, exit_with_error_input, exit_with_error_exception

def main():
input, system_type, cell_para, debug_mode = parsing_arguments()
Expand All @@ -15,32 +15,35 @@ def main():

print(input, input_path, system_type, cell_para, debug_mode, name, extension)
if not os.path.exists(input_path):
exit_with_error(f"Input file not found: {input_path}")
exit_with_error_input(f"Input file not found: {input_path}")
if extension == ".cif":
if prefilter_cif(input_path):
handle_cif_file(input_path, system_type, name, current_dir, debug_mode)
else:
exit_with_error("CIF file is not suitable for processing")
exit_with_error_input("CIF file is not suitable for processing")
elif extension == ".xyz":
handle_xyz_file(input_path, system_type, name, cell_para, current_dir, debug_mode)
else:
exit_with_error("Invalid file extension")
exit_with_error_input("Invalid file extension")

def handle_cif_file(input_path, system_type, name, current_dir, debug_mode):
if system_type == "reference":
print("Processing reference (Wyckoff sites) from .cif file")
process_refcell(input_path, name, current_dir, debug_mode)
try:
process_refcell(input_path, name, current_dir, debug_mode)
except Exception as e:
exit_with_error_exception(e)
elif system_type == "unitcell":
print("Processing unit cell from .cif file")
process_unitcell(input_path, name, current_dir, debug_mode)
else:
exit_with_error("Invalid system type for .cif file", {"system_type": system_type})
exit_with_error_input("Invalid system type for .cif file", {"system_type": system_type})


def handle_xyz_file(input_path, system_type, name, cell_para, current_dir, debug_mode):
if system_type == "unitcell":
if cell_para is None:
exit_with_error("Cell parameters must be provided for .xyz file of a unit cell")
exit_with_error_input("Cell parameters must be provided for .xyz file of a unit cell")
else:
print("Processing unit cell from .xyz file")
# users should provide chemical formula (Fe-O2-H3) of refmoleculist
Expand All @@ -49,7 +52,7 @@ def handle_xyz_file(input_path, system_type, name, cell_para, current_dir, debug
print("Processing molecule from .xyz file")
get_molecule(input_path, name, current_dir, debug_mode)
else:
exit_with_error("Invalid system type for .xyz file", {"system_type": system_type})
exit_with_error_input("Invalid system type for .xyz file", {"system_type": system_type})


if __name__ == "__main__" or __name__ == "cell2mol.final_c2m_driver":
Expand Down
27 changes: 22 additions & 5 deletions cell2mol/read_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
from collections import defaultdict
import os
import traceback

#######################
def get_wyckoff_positions(file_path):
Expand Down Expand Up @@ -54,32 +55,48 @@ def get_wyckoff_positions(file_path):
return ref_labels, ref_fracs

#######################
def exit_with_error(message):
def exit_with_error_input(message):
"""Logs the error message to a file and exits the program."""
error_log_path = os.path.join(os.getcwd(), "error_input.out")
with open(error_log_path, "w") as error_log:
error_log.write(f"Error: {message}\n")
sys.exit(message)

#######################
def exit_with_error_exception(e):
"""Logs the error details to a file and exits the program."""
error_details = traceback.format_exc()
error_log_path = os.path.join(os.getcwd(), f"error_{type(e).__name__}.out")

# Write the full error details to the log file
with open(error_log_path, "w") as error_log:
error_log.write(f"Error message: {type(e).__name__} - {str(e)}\n")
error_log.write(f"Error details:\n{error_details}")

# Print the error details to the console
print(f"An error occurred. Details have been logged to {error_log_path}")
print(f"Error details:\n{error_details}")

sys.exit(e)
#######################
def prefilter_cif(input_path):

with open(input_path, 'r') as ciffile:
file_content = ciffile.read()
if 'radical' in file_content:
exit_with_error("Radical found in cif file. STOPPING")
exit_with_error_input("Radical found in cif file. STOPPING")
return False
elif '_atom_site_fract_x' not in file_content:
exit_with_error("No fractional coordinates found in cif file. STOPPING")
exit_with_error_input("No fractional coordinates found in cif file. STOPPING")
return False
elif '?' in file_content:
if "_diffrn_ambient_temperature ?" not in file_content and "_chemical_melting_point ?" not in file_content:
exit_with_error("Disorder found in cif file. STOPPING")
exit_with_error_input("Disorder found in cif file. STOPPING")
return False
else:
num_greps = file_content.count('?')
if num_greps > 1:
exit_with_error("Disorder found in cif file. STOPPING")
exit_with_error_input("Disorder found in cif file. STOPPING")
return False
else:
return True
Expand Down

0 comments on commit b124483

Please sign in to comment.