Mercurial > repos > siwaa > erecord_text
changeset 0:5edb952b394e draft default tip
planemo upload for repository https://forgemia.inra.fr/nathalie.rousse/erecord-deploy/-/tree/main/galaxy-tools/erecord_text commit c9272a57d834895bf21c987be7d7416532cfa23f-dirty
author | siwaa |
---|---|
date | Tue, 27 Feb 2024 07:58:55 +0000 |
parents | |
children | |
files | erecord_text.xml main_text.py |
diffstat | 2 files changed, 232 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/erecord_text.xml Tue Feb 27 07:58:55 2024 +0000 @@ -0,0 +1,101 @@ +<tool id="erecord_text" name="erecord_text" version="1.0.0"> + + <description>erecord returning .json</description> + + <requirements> + + <!-- singularity --> + <container type="singularity">oras://registry.forgemia.inra.fr/nathalie.rousse/erecord-deploy/rep_recordb:9d6ea1defd50dd63fe036a7545a9ea527572192e</container> + + <!-- image locale --> + <!-- container type="singularity">/home/nrousse/workspace_git/SIWAA_regroup/erecord-deploy/containers/_fab_SIMG/rep_recordb.simg</container--> + + </requirements> + + <command detect_errors="aggressive"> + <![CDATA[ + . /PYVENV/bin/activate; + /bin/bash -c "python3 $__tool_directory__/main_text.py -data '${input_text}' -datafolder ${datafolder}"; + cp WS/OUT/output.json ${output_json}; + ]]> + </command> + + <inputs> + + <param name="input_text" optional="true" type="text" area="true" + label="Input json as text"/> + + <param name="datafolder" optional="true" type="data" format="zip" + label="Add a datafolder zip file (available and used only in 'post_vpz_output' or 'post_vpz_inout' cases)" + help="Precisions sur forme du dossier..."/> + + </inputs> + + <outputs> + <data format="json" name="output_json" label="jsonoutput"/> + </outputs> + + <tests> + </tests> + + <help><![CDATA[ + +erecord_text.xml + +================ + +Actions: + +------- + +- Available : + + - 'get_vpz_input' 'post_vpz_input' + - 'get_vpz_output' 'post_vpz_output' + - 'get_vpz_inout' 'post_vpz_inout' + - 'get_pkg_list' 'get_pkg_vpz_list' 'get_pkg_data_list' + - 'help' + +- Unavailable (actions with 2 output files) : + + - 'get_vpz_experiment' 'post_vpz_experiment' + - 'get_vpz_report_conditions' 'post_vpz_report_conditions' + - 'get_vpz_report' 'post_vpz_report' + +Code: + +---- + +- main_text.py + +Inputs: + +------- + +- data json as text (optional) (text, param type="text") + + - This data contains the action value + - Examples : + + - {"action":"pkg_list"} + - {"action": "post_vpz_output", "pkgname": "wwdm", "vpzname": "wwdm.vpz", "style": "compactlist", "plan":"linear", "restype" : "matrix", "cond_wwdm.A": [0.0063, 0.0065, 0.0067], "cond_wwdm.B": 0.00201, "duration":6.0, "outselect": "view"} + +- datafolder (optional) (only in 'post_vpz_output', 'post_vpz_inout' cases) + +Outputs: + +-------- + +- data json file (no other file returned) + +Credits: + +-------- + +- Author: Nathalie Rousse nathalie.rousse@inrae.fr +- Copyright: INRAE + + ]]> + </help> + +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main_text.py Tue Feb 27 07:58:55 2024 +0000 @@ -0,0 +1,131 @@ +# -*- coding: UTF-8 -*- +""" + main_text + + Requests having only 1 json data as OUTput + + Accepts actions : + 'get_vpz_input' 'post_vpz_input' + 'get_vpz_output' 'post_vpz_output' + 'get_vpz_inout' 'post_vpz_inout' + 'get_pkg_list' 'get_pkg_vpz_list' 'get_pkg_data_list' + 'help' + + Refused actions : + 'get_vpz_experiment' 'post_vpz_experiment' + 'get_vpz_report_conditions' 'post_vpz_report_conditions' + 'get_vpz_report' 'post_vpz_report' + + Parameters (Inputs) : + - '-data' parameter : data (string format) (optional) + A required action value must be included into data. + - '-datafolder' parameter : datafolder file path (optional, + available only in some cases) + + Outputs : + - output.json data file (jsonoutput_filepath) +""" + +MAIN_DEBUG=False # True for more information returned + +import argparse +import json +import erecord.api.erecord as erecord_api + +import os + +PM = "[main_text.py]" + +UNACCEPTED_ACTIONS_LIST = ['get_vpz_experiment', 'post_vpz_experiment', + 'get_vpz_report_conditions', 'post_vpz_report_conditions', + 'get_vpz_report', 'post_vpz_report'] + +r = dict() + +import sys +if MAIN_DEBUG: r["sys.path"] = sys.path # debug +if MAIN_DEBUG: r["sys.argv"] = sys.argv # debug + +data = dict() +json_response = dict() + +# default configuration + maybe some modifications +config = erecord_api.conf() # default (vle-2.0.0 version) +config.DB_add_vpz(vpz_id=1312, vpzname="wwdm.vpz", pkgname="wwdm") + +try : + parser = argparse.ArgumentParser() + parser.add_argument("-data", type=str, help="data in string format") + parser.add_argument("-datafolder", type=str, help="datafolder file path") + + args = parser.parse_args() + + if (args.data is None) or (args.data == 'None') : + warn = "NO data (string format) given => action enforced to 'help'" + r["warning"] = warn + data = {'action':'help'} # enforced (default) + else: + #jsoninput_filename = args.data + #inputfile = open(jsoninput_filename, 'r') + #data = json.load(inputfile) # get data + + # examples + # {"action":"pkg_list"} + # {"action": "post_vpz_output", "pkgname": "wwdm", "vpzname": "wwdm.vpz", "style": "compactlist", "plan":"linear", "restype" : "matrix", "cond_wwdm.A": [0.0063, 0.0065, 0.0067], "cond_wwdm.B": 0.00201, "duration":6.0, "outselect": "view"} + + jsoninput_text = args.data + try : + + # restore sanitized text + MAPPING = {'>': '__gt__', '<': '__lt__', "'": '__sq__', '"': '__dq__', '[': '__ob__', ']': '__cb__', '{': '__oc__', '}': '__cc__', '@': '__at__', '\n': '__cn__', '\r': '__cr__', '\t': '__tc__', '#': '__pd__'} + for key, value in MAPPING.items(): + jsoninput_text = jsoninput_text.replace(value, key) + + data = json.loads(jsoninput_text) # get data + except: + warn = "Failed to get json data from string '" +jsoninput_text+ "'" + warn = warn + " => action enforced to 'help'" + r["warning"] = warn + data = {'action':'help'} # enforced (default) + + if (args.datafolder is None) or (args.datafolder == 'None') : + pass # NO datafolder given + else : + data['datafolder'] = args.datafolder + + # Pre-filtering : Unaccepted actions => enforced to 'help' action + if "action" in data.keys() : + a = data["action"] + if a in UNACCEPTED_ACTIONS_LIST : + warn = "Unaccepted action '"+a+"' => action enforced to 'help'" + r["warning"] = warn + data["action"] = 'help' + + r["data"] = data + + # configuration + if MAIN_DEBUG: r["config"] = config.get_as_dict() # debug + + r_init = erecord_api.init(config) + if MAIN_DEBUG: r["init"] = r_init # debug + + response = erecord_api.action(config, data) + json_response = json.loads(response) + +except Exception as e : + + response = erecord_api.error_case(data=data, msg=PM, e=e) + json_response = json.loads(response) + +r["response"] = json_response + +try : + # Restitution : json file + jsonoutput_filepath = os.path.join(config.out_path, + config.jsonoutput_filename) + with open(jsonoutput_filepath, "w") as outfile: + json.dump(r, outfile) + +except : + print(PM, " -- error when trying to open output json file --") +