Mercurial > repos > siwaa > erecord_file
view main_file.py @ 0:27e7e4d38e44 draft
planemo upload for repository https://forgemia.inra.fr/nathalie.rousse/erecord-deploy/-/tree/main/galaxy-tools/erecord_file commit 88beb443a737b4c4d42bb0011a26a0acf535c366
author | siwaa |
---|---|
date | Tue, 12 Dec 2023 17:33:05 +0000 |
parents | |
children | 9e8f5f220917 |
line wrap: on
line source
# -*- coding: UTF-8 -*- """ main_file Requests having as OUTput : 1 json data + maybe 1 more file (xls or zip) Accepts actions (all) : 'help' 'get_pkg_list' 'get_pkg_vpz_list' 'get_pkg_data_list' 'get_vpz_input' 'post_vpz_input' 'get_vpz_output' 'post_vpz_output' 'get_vpz_inout' 'post_vpz_inout' 'get_vpz_experiment' 'post_vpz_experiment' 'get_vpz_report_conditions' 'post_vpz_report_conditions' 'get_vpz_report' 'post_vpz_report' Parameters (Inputs) : - '-action' parameter : action value (mandatory) - '-data' parameter : data (JSON format) (optional) - '-datafolder' parameter : datafolder file path (optional, available only in some cases) - '-experimentfile' parameter : experimentfile path (optional, available only in some cases) : If there is an action value into data, then this one is ignored. Outputs : - output.json data file - 1 other file in following cases : - experiment.xls file if 'action' == get_vpz_experiment, post_vpz_experiment - conditions.xls file if 'action' == get_vpz_report_conditions, post_vpz_report_conditions - report.zip file if 'action' == get_vpz_report, post_vpz_report """ MAIN_DEBUG=False # True for more information returned import argparse import json import erecord.api.erecord as erecord_api from erecord.api.cmn.views_mixins import ActionViewMixin import os import shutil from erecord.api.cmn.utils.dir_and_file import create_dir_if_absent from erecord.api.cmn.configs.config import PROJECT_HOME from erecord.api.cmn.configs.config import out_path PM = "[main_file.py]" jsonoutput_filepath = os.path.join(out_path, "output.json") # tool.xml uses it r = dict() import sys if MAIN_DEBUG: r["sys.path"] = sys.path # debug if MAIN_DEBUG: r["sys.argv"] = sys.argv # debug if MAIN_DEBUG: r["out_path"] = out_path # debug data = dict() json_response = dict() create_dir_if_absent(PROJECT_HOME) # required try : parser = argparse.ArgumentParser() # -action available_action_list = ActionViewMixin.get_action_list() htext = "action value among : " + str(available_action_list) parser.add_argument("-action", default='help', help=htext, choices=available_action_list) # -data parser.add_argument("-data", type=str, help="data json file path") # -datafolder htext = "datafolder file path, available for action : " htext +="post_vpz_output, post_vpz_inout," htext +="post_vpz_experiment, post_vpz_report" parser.add_argument("-datafolder", type=str, help=htext) # -experimentfile htext = "experimentfile path, available for action : post_vpz_experiment" parser.add_argument("-experimentfile", type=str, help=htext) args = parser.parse_args() if (args.action is None) or (args.action == 'None') : warn = "NO action given => enforced to 'help'" r["warning"] = warn action = 'help' # enforced (default) else : action = args.action if (args.data is None) or (args.data == 'None') : data = dict() # NO data (JSON format) given as argument else : jsoninput_filename = args.data inputfile = open(jsoninput_filename, 'r') data = json.load(inputfile) # get data if (args.datafolder is None) or (args.datafolder == 'None') : pass # NO datafolder given else : data['datafolder'] = args.datafolder if (args.experimentfile is None) or (args.experimentfile == 'None') : pass # NO experimentfile given else : data['experimentfile'] = args.experimentfile data['action'] = action r["data"] = data r_init = erecord_api.init() if MAIN_DEBUG: r["init"] = r_init # debug response = erecord_api.action(data) json_response = json.loads(response) # Restitution : file (experiment.xls, conditions.xls, report.zip) if action in ['get_vpz_experiment', 'post_vpz_experiment', 'get_vpz_report_conditions', 'post_vpz_report_conditions', 'get_vpz_report', 'post_vpz_report']: filename = json_response["outputfile_name"] outputfile_path = json_response["outputfile_path"] shutil.copyfile(src=outputfile_path, dst=os.path.join(out_path,filename)) # tool.xml uses them if not MAIN_DEBUG: # some response information not kept for k in ('outputfile_name', 'outputfile_path', 'zip_file_path'): if k in json_response.keys(): del(json_response[k]) except Exception as e : response = erecord_api.error_case(data=data, msg=PM, e=e) json_response = json.loads(response) r["response"] = json_response # Restitution : json file with open(jsonoutput_filepath, "w") as outfile: json.dump(r, outfile)