Mercurial > repos > siwaa > erecord_json
comparison main_json.py @ 0:8a0659f23df7 draft
planemo upload for repository https://forgemia.inra.fr/nathalie.rousse/erecord-deploy/-/tree/main/galaxy-tools/erecord_json commit 88beb443a737b4c4d42bb0011a26a0acf535c366
author | siwaa |
---|---|
date | Tue, 12 Dec 2023 17:37:35 +0000 |
parents | |
children | afc88ac8de69 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8a0659f23df7 |
---|---|
1 # -*- coding: UTF-8 -*- | |
2 """ | |
3 main_json | |
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 (JSON 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 | |
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 import os | |
35 from erecord.api.cmn.utils.dir_and_file import create_dir_if_absent | |
36 from erecord.api.cmn.configs.config import PROJECT_HOME | |
37 from erecord.api.cmn.configs.config import out_path | |
38 | |
39 PM = "[main_json.py]" | |
40 | |
41 UNACCEPTED_ACTIONS_LIST = ['get_vpz_experiment', 'post_vpz_experiment', | |
42 'get_vpz_report_conditions', 'post_vpz_report_conditions', | |
43 'get_vpz_report', 'post_vpz_report'] | |
44 | |
45 jsonoutput_filepath = os.path.join(out_path, "output.json") # tool.xml uses it | |
46 | |
47 r = dict() | |
48 | |
49 import sys | |
50 if MAIN_DEBUG: r["sys.path"] = sys.path # debug | |
51 if MAIN_DEBUG: r["sys.argv"] = sys.argv # debug | |
52 if MAIN_DEBUG: r["out_path"] = out_path # debug | |
53 | |
54 data = dict() | |
55 json_response = dict() | |
56 | |
57 create_dir_if_absent(PROJECT_HOME) # required | |
58 | |
59 try : | |
60 parser = argparse.ArgumentParser() | |
61 parser.add_argument("-data", type=str, help="data json file path") | |
62 parser.add_argument("-datafolder", type=str, help="datafolder file path") | |
63 | |
64 args = parser.parse_args() | |
65 | |
66 if (args.data is None) or (args.data == 'None') : | |
67 warn = "NO data (JSON format) given => action enforced to 'help'" | |
68 r["warning"] = warn | |
69 data = {'action':'help'} # enforced (default) | |
70 else: | |
71 jsoninput_filename = args.data | |
72 inputfile = open(jsoninput_filename, 'r') | |
73 data = json.load(inputfile) # get data | |
74 | |
75 if (args.datafolder is None) or (args.datafolder == 'None') : | |
76 pass # NO datafolder given | |
77 else : | |
78 data['datafolder'] = args.datafolder | |
79 | |
80 # Pre-filtering : Unaccepted actions => enforced to 'help' action | |
81 if "action" in data.keys() : | |
82 a = data["action"] | |
83 if a in UNACCEPTED_ACTIONS_LIST : | |
84 warn = "Unaccepted action '"+a+"' => action enforced to 'help'" | |
85 r["warning"] = warn | |
86 data["action"] = 'help' | |
87 | |
88 r["data"] = data | |
89 r_init = erecord_api.init() | |
90 if MAIN_DEBUG: r["init"] = r_init # debug | |
91 response = erecord_api.action(data) | |
92 json_response = json.loads(response) | |
93 | |
94 except Exception as e : | |
95 | |
96 response = erecord_api.error_case(data=data, msg=PM, e=e) | |
97 json_response = json.loads(response) | |
98 | |
99 r["response"] = json_response | |
100 | |
101 # Restitution : json file | |
102 with open(jsonoutput_filepath, "w") as outfile: | |
103 json.dump(r, outfile) | |
104 |