Mercurial > repos > siwaa > erecord_text
comparison main_text.py @ 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 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:5edb952b394e |
|---|---|
| 1 # -*- coding: UTF-8 -*- | |
| 2 """ | |
| 3 main_text | |
| 4 | |
| 5 Requests having only 1 json data as OUTput | |
| 6 | |
| 7 Accepts actions : | |
| 8 'get_vpz_input' 'post_vpz_input' | |
| 9 'get_vpz_output' 'post_vpz_output' | |
| 10 'get_vpz_inout' 'post_vpz_inout' | |
| 11 'get_pkg_list' 'get_pkg_vpz_list' 'get_pkg_data_list' | |
| 12 'help' | |
| 13 | |
| 14 Refused actions : | |
| 15 'get_vpz_experiment' 'post_vpz_experiment' | |
| 16 'get_vpz_report_conditions' 'post_vpz_report_conditions' | |
| 17 'get_vpz_report' 'post_vpz_report' | |
| 18 | |
| 19 Parameters (Inputs) : | |
| 20 - '-data' parameter : data (string format) (optional) | |
| 21 A required action value must be included into data. | |
| 22 - '-datafolder' parameter : datafolder file path (optional, | |
| 23 available only in some cases) | |
| 24 | |
| 25 Outputs : | |
| 26 - output.json data file (jsonoutput_filepath) | |
| 27 """ | |
| 28 | |
| 29 MAIN_DEBUG=False # True for more information returned | |
| 30 | |
| 31 import argparse | |
| 32 import json | |
| 33 import erecord.api.erecord as erecord_api | |
| 34 | |
| 35 import os | |
| 36 | |
| 37 PM = "[main_text.py]" | |
| 38 | |
| 39 UNACCEPTED_ACTIONS_LIST = ['get_vpz_experiment', 'post_vpz_experiment', | |
| 40 'get_vpz_report_conditions', 'post_vpz_report_conditions', | |
| 41 'get_vpz_report', 'post_vpz_report'] | |
| 42 | |
| 43 r = dict() | |
| 44 | |
| 45 import sys | |
| 46 if MAIN_DEBUG: r["sys.path"] = sys.path # debug | |
| 47 if MAIN_DEBUG: r["sys.argv"] = sys.argv # debug | |
| 48 | |
| 49 data = dict() | |
| 50 json_response = dict() | |
| 51 | |
| 52 # default configuration + maybe some modifications | |
| 53 config = erecord_api.conf() # default (vle-2.0.0 version) | |
| 54 config.DB_add_vpz(vpz_id=1312, vpzname="wwdm.vpz", pkgname="wwdm") | |
| 55 | |
| 56 try : | |
| 57 parser = argparse.ArgumentParser() | |
| 58 parser.add_argument("-data", type=str, help="data in string format") | |
| 59 parser.add_argument("-datafolder", type=str, help="datafolder file path") | |
| 60 | |
| 61 args = parser.parse_args() | |
| 62 | |
| 63 if (args.data is None) or (args.data == 'None') : | |
| 64 warn = "NO data (string format) given => action enforced to 'help'" | |
| 65 r["warning"] = warn | |
| 66 data = {'action':'help'} # enforced (default) | |
| 67 else: | |
| 68 #jsoninput_filename = args.data | |
| 69 #inputfile = open(jsoninput_filename, 'r') | |
| 70 #data = json.load(inputfile) # get data | |
| 71 | |
| 72 # examples | |
| 73 # {"action":"pkg_list"} | |
| 74 # {"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"} | |
| 75 | |
| 76 jsoninput_text = args.data | |
| 77 try : | |
| 78 | |
| 79 # restore sanitized text | |
| 80 MAPPING = {'>': '__gt__', '<': '__lt__', "'": '__sq__', '"': '__dq__', '[': '__ob__', ']': '__cb__', '{': '__oc__', '}': '__cc__', '@': '__at__', '\n': '__cn__', '\r': '__cr__', '\t': '__tc__', '#': '__pd__'} | |
| 81 for key, value in MAPPING.items(): | |
| 82 jsoninput_text = jsoninput_text.replace(value, key) | |
| 83 | |
| 84 data = json.loads(jsoninput_text) # get data | |
| 85 except: | |
| 86 warn = "Failed to get json data from string '" +jsoninput_text+ "'" | |
| 87 warn = warn + " => action enforced to 'help'" | |
| 88 r["warning"] = warn | |
| 89 data = {'action':'help'} # enforced (default) | |
| 90 | |
| 91 if (args.datafolder is None) or (args.datafolder == 'None') : | |
| 92 pass # NO datafolder given | |
| 93 else : | |
| 94 data['datafolder'] = args.datafolder | |
| 95 | |
| 96 # Pre-filtering : Unaccepted actions => enforced to 'help' action | |
| 97 if "action" in data.keys() : | |
| 98 a = data["action"] | |
| 99 if a in UNACCEPTED_ACTIONS_LIST : | |
| 100 warn = "Unaccepted action '"+a+"' => action enforced to 'help'" | |
| 101 r["warning"] = warn | |
| 102 data["action"] = 'help' | |
| 103 | |
| 104 r["data"] = data | |
| 105 | |
| 106 # configuration | |
| 107 if MAIN_DEBUG: r["config"] = config.get_as_dict() # debug | |
| 108 | |
| 109 r_init = erecord_api.init(config) | |
| 110 if MAIN_DEBUG: r["init"] = r_init # debug | |
| 111 | |
| 112 response = erecord_api.action(config, data) | |
| 113 json_response = json.loads(response) | |
| 114 | |
| 115 except Exception as e : | |
| 116 | |
| 117 response = erecord_api.error_case(data=data, msg=PM, e=e) | |
| 118 json_response = json.loads(response) | |
| 119 | |
| 120 r["response"] = json_response | |
| 121 | |
| 122 try : | |
| 123 # Restitution : json file | |
| 124 jsonoutput_filepath = os.path.join(config.out_path, | |
| 125 config.jsonoutput_filename) | |
| 126 with open(jsonoutput_filepath, "w") as outfile: | |
| 127 json.dump(r, outfile) | |
| 128 | |
| 129 except : | |
| 130 print(PM, " -- error when trying to open output json file --") | |
| 131 |
