I am trying to run a program but that program needs to read a config file. This config file has to have a base directory from which it will read the files. The config file has several sections for different sub directories from the base file.
Ultimately I want the program to break the script and return an error message if the base directory is not inside the config file.
This requires an If Statement. However if statements typically don't break scripts. How can I write an if statemtent that will break the function in which this config file is read from which will also break the script? This function will be used inside another function
def process_dirconfig_file(config_file_from_sysarg):
config = ConfigParser()
config.read(config_file_from_sysarg)
dirconfig_file_Pobj = Path(config_file_from_sysarg)
if Path.is_file(dirconfig_file_Pobj):
parseddict = {}
for sect in config.sections():
for k, v in config.items(sect):
# print('{} = {}'.format(k, v))
parseddict[k] = v
print(parseddict)
if ("base_dir" not in parseddict) or (parseddict["base_dir"] == ""):
print(f"{Fore.RED} Error: Your config file is missing 'base directory' for file processing")
elif("archive_dir" not in parseddict) or (parseddict["archive_dir"] == ""):
print(f"{Fore.RED} Error: Your config file is missing 'archive directory' for file processing")
elif ("error_dir" not in parseddict) or (parseddict["error_dir"] == ""):
print(f"{Fore.RED} Error: Your config file is missing 'error directory' for file processing")
elif ("empty_dir" not in parseddict) or (parseddict["empty_dir"] == ""):
print(f"{Fore.RED} Error: Your config file is missing 'empty directory' for file processing")
else:
print(f"{Fore.RED} Error: No directory config file. Please create a config file of directories to be used in processing")
This function is being used inside this function:
def odf_history_from_csv_to_dbtable(csvfile_path_list, db_instance):
odfsdict = db_instance['odfs_tester_history']
#table_row = {}
totalresult_list = []
process_dirconfig_file(dirconfig_file)
for csv in csvfile_path_list: # is there a faster way to compare the list of files in archive and history?
if csv not in archivefiles_path_set:
csvhistoryfilelist_to_dbtable(csv, db_instance)
odfscsv_df = pd.read_csv(csv, header=None, names=['ODFS_LOG_FILENAME', 'ODFS_FILE_CREATE_DATETIME', 'LOT', 'TESTER', 'WAFER_SCRIBE'])
odfscsv_df['CSV_FILENAME'] = csv.name #add csvfilename column to existing df
result = odfscsv_df.to_sql('odfs_tester_history', con=odfsdict['engine'], if_exists='append', index=False)
totalresult_list.append(result)
else:
print(csv.name + " is in archive folder already")
#print (totalresult_list)
return totalresult_list
db_instance = dbhandler()
odfs_tabletest_dict = db_instance['odfs_tester_history_files']
return.