Mercurial > repos > siwaa > erecord_file
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:27e7e4d38e44 |
---|---|
1 # -*- coding: UTF-8 -*- | |
2 """ | |
3 main_file | |
4 | |
5 Requests having as OUTput : 1 json data + maybe 1 more file (xls or zip) | |
6 | |
7 Accepts actions (all) : | |
8 'help' | |
9 'get_pkg_list' 'get_pkg_vpz_list' 'get_pkg_data_list' | |
10 'get_vpz_input' 'post_vpz_input' | |
11 'get_vpz_output' 'post_vpz_output' | |
12 'get_vpz_inout' 'post_vpz_inout' | |
13 'get_vpz_experiment' 'post_vpz_experiment' | |
14 'get_vpz_report_conditions' 'post_vpz_report_conditions' | |
15 'get_vpz_report' 'post_vpz_report' | |
16 | |
17 Parameters (Inputs) : | |
18 - '-action' parameter : action value (mandatory) | |
19 - '-data' parameter : data (JSON format) (optional) | |
20 - '-datafolder' parameter : datafolder file path (optional, | |
21 available only in some cases) | |
22 - '-experimentfile' parameter : experimentfile path (optional, | |
23 available only in some cases) : | |
24 | |
25 If there is an action value into data, then this one is ignored. | |
26 | |
27 Outputs : | |
28 - output.json data file | |
29 - 1 other file in following cases : | |
30 - experiment.xls file | |
31 if 'action' == get_vpz_experiment, post_vpz_experiment | |
32 - conditions.xls file | |
33 if 'action' == get_vpz_report_conditions, | |
34 post_vpz_report_conditions | |
35 - report.zip file | |
36 if 'action' == get_vpz_report, post_vpz_report | |
37 """ | |
38 | |
39 MAIN_DEBUG=False # True for more information returned | |
40 | |
41 import argparse | |
42 import json | |
43 import erecord.api.erecord as erecord_api | |
44 from erecord.api.cmn.views_mixins import ActionViewMixin | |
45 import os | |
46 import shutil | |
47 from erecord.api.cmn.utils.dir_and_file import create_dir_if_absent | |
48 from erecord.api.cmn.configs.config import PROJECT_HOME | |
49 from erecord.api.cmn.configs.config import out_path | |
50 | |
51 PM = "[main_file.py]" | |
52 | |
53 jsonoutput_filepath = os.path.join(out_path, "output.json") # tool.xml uses it | |
54 | |
55 r = dict() | |
56 | |
57 import sys | |
58 if MAIN_DEBUG: r["sys.path"] = sys.path # debug | |
59 if MAIN_DEBUG: r["sys.argv"] = sys.argv # debug | |
60 if MAIN_DEBUG: r["out_path"] = out_path # debug | |
61 | |
62 data = dict() | |
63 json_response = dict() | |
64 | |
65 create_dir_if_absent(PROJECT_HOME) # required | |
66 | |
67 try : | |
68 parser = argparse.ArgumentParser() | |
69 | |
70 # -action | |
71 available_action_list = ActionViewMixin.get_action_list() | |
72 htext = "action value among : " + str(available_action_list) | |
73 parser.add_argument("-action", default='help', help=htext, | |
74 choices=available_action_list) | |
75 | |
76 # -data | |
77 parser.add_argument("-data", type=str, help="data json file path") | |
78 | |
79 # -datafolder | |
80 htext = "datafolder file path, available for action : " | |
81 htext +="post_vpz_output, post_vpz_inout," | |
82 htext +="post_vpz_experiment, post_vpz_report" | |
83 parser.add_argument("-datafolder", type=str, help=htext) | |
84 | |
85 # -experimentfile | |
86 htext = "experimentfile path, available for action : post_vpz_experiment" | |
87 parser.add_argument("-experimentfile", type=str, help=htext) | |
88 | |
89 args = parser.parse_args() | |
90 | |
91 if (args.action is None) or (args.action == 'None') : | |
92 warn = "NO action given => enforced to 'help'" | |
93 r["warning"] = warn | |
94 action = 'help' # enforced (default) | |
95 else : | |
96 action = args.action | |
97 | |
98 if (args.data is None) or (args.data == 'None') : | |
99 data = dict() # NO data (JSON format) given as argument | |
100 else : | |
101 jsoninput_filename = args.data | |
102 inputfile = open(jsoninput_filename, 'r') | |
103 data = json.load(inputfile) # get data | |
104 | |
105 if (args.datafolder is None) or (args.datafolder == 'None') : | |
106 pass # NO datafolder given | |
107 else : | |
108 data['datafolder'] = args.datafolder | |
109 | |
110 if (args.experimentfile is None) or (args.experimentfile == 'None') : | |
111 pass # NO experimentfile given | |
112 else : | |
113 data['experimentfile'] = args.experimentfile | |
114 | |
115 data['action'] = action | |
116 | |
117 r["data"] = data | |
118 r_init = erecord_api.init() | |
119 if MAIN_DEBUG: r["init"] = r_init # debug | |
120 response = erecord_api.action(data) | |
121 json_response = json.loads(response) | |
122 | |
123 # Restitution : file (experiment.xls, conditions.xls, report.zip) | |
124 if action in ['get_vpz_experiment', 'post_vpz_experiment', | |
125 'get_vpz_report_conditions', 'post_vpz_report_conditions', | |
126 'get_vpz_report', 'post_vpz_report']: | |
127 | |
128 filename = json_response["outputfile_name"] | |
129 outputfile_path = json_response["outputfile_path"] | |
130 shutil.copyfile(src=outputfile_path, | |
131 dst=os.path.join(out_path,filename)) # tool.xml uses them | |
132 | |
133 if not MAIN_DEBUG: # some response information not kept | |
134 for k in ('outputfile_name', 'outputfile_path', 'zip_file_path'): | |
135 if k in json_response.keys(): | |
136 del(json_response[k]) | |
137 | |
138 except Exception as e : | |
139 | |
140 response = erecord_api.error_case(data=data, msg=PM, e=e) | |
141 json_response = json.loads(response) | |
142 | |
143 r["response"] = json_response | |
144 | |
145 # Restitution : json file | |
146 with open(jsonoutput_filepath, "w") as outfile: | |
147 json.dump(r, outfile) | |
148 |