importcsvimportioimportjsonimportloggingimportosfromcollectionsimportdefaultdictfrom..utils.exceptionsimportOasisExceptionfrom..utils.logimportoasis_logfrom.filesimportGENERAL_SETTINGS_FILE,GUL_SUMMARIES_FILE,IL_SUMMARIES_FILE,MODEL_SETTINGS_FILEdef_get_summaries(summary_file):""" Get a list representation of a summary file. """summaries_dict=defaultdict(lambda:{'leccalc':{}})withio.open(summary_file,'r',encoding='utf-8')ascsvfile:reader=csv.reader(csvfile)forrowinreader:id=int(row[0])ifrow[1].startswith('leccalc'):summaries_dict[id]['leccalc'][row[1]]=row[2].lower()=='true'else:summaries_dict[id][row[1]]=row[2].lower()=='true'summaries=list()foridinsorted(summaries_dict):summaries_dict[id]['id']=idsummaries.append(summaries_dict[id])returnsummaries@oasis_log
[docs]defcreate_analysis_settings_json(directory):""" Generate an analysis settings JSON from a set of CSV files in a specified directory. Args: ``directory`` (string): the directory containing the CSV files. Returns: The analysis settings JSON. """ifnotos.path.exists(directory):error_message="Directory does not exist: {}".format(directory)logging.getLogger(__name__).error(error_message)raiseOasisException(error_message)general_settings_file=os.path.join(directory,GENERAL_SETTINGS_FILE)model_settings_file=os.path.join(directory,MODEL_SETTINGS_FILE)gul_summaries_file=os.path.join(directory,GUL_SUMMARIES_FILE)il_summaries_file=os.path.join(directory,IL_SUMMARIES_FILE)forfilein[general_settings_file,model_settings_file,gul_summaries_file,il_summaries_file]:ifnotos.path.exists(file):error_message="File does not exist: {}".format(directory)logging.getLogger(__name__).error(error_message)raiseOasisException(error_message)general_settings=dict()withio.open(general_settings_file,'r',encoding='utf-8')ascsvfile:reader=csv.reader(csvfile)forrowinreader:general_settings[row[0]]=eval("{}('{}')".format(row[2],row[1]))model_settings=dict()withio.open(model_settings_file,'r',encoding='utf-8')ascsvfile:reader=csv.reader(csvfile)forrowinreader:model_settings[row[0]]=eval("{}('{}')".format(row[2],row[1]))gul_summaries=_get_summaries(gul_summaries_file)il_summaries=_get_summaries(il_summaries_file)analysis_settings=general_settingsanalysis_settings['model_settings']=model_settingsanalysis_settings['gul_summaries']=gul_summariesanalysis_settings['il_summaries']=il_summariesoutput_json=json.dumps(analysis_settings)logging.getLogger(__name__).info("Analysis settings json: {}".format(output_json))returnoutput_json