Mercurial > repos > siwaa > erecord_file
comparison main_file__OriginalNOK.py @ 4:4c45f7b92955 draft
planemo upload for repository https://forgemia.inra.fr/nathalie.rousse/erecord-deploy/-/tree/main/galaxy-tools/erecord_file commit 882e24ac5dbef67295dd4466908b7eea02e34697-dirty
author | siwaa |
---|---|
date | Thu, 15 Feb 2024 17:12:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:aa5c088856c8 | 4:4c45f7b92955 |
---|---|
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 (jsonoutput_filepath) | |
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 from erecord.api.cmn.utils.errors import build_error_content | |
46 | |
47 import os | |
48 import shutil | |
49 | |
50 PM = "[main_file.py]" | |
51 | |
52 r = dict() | |
53 | |
54 import sys | |
55 if MAIN_DEBUG: r["sys.path"] = sys.path # debug | |
56 if MAIN_DEBUG: r["sys.argv"] = sys.argv # debug | |
57 | |
58 data = dict() | |
59 json_response = dict() | |
60 | |
61 # default configuration + maybe some modifications | |
62 config = erecord_api.conf() # default (vle-2.0.0 version) | |
63 config.DB_add_vpz(vpz_id=1312, vpzname="wwdm.vpz", pkgname="wwdm") | |
64 | |
65 try : | |
66 parser = argparse.ArgumentParser() | |
67 | |
68 # -action | |
69 available_action_list = ActionViewMixin.get_action_list() | |
70 htext = "action value among : " + str(available_action_list) | |
71 parser.add_argument("-action", default='help', help=htext, | |
72 choices=available_action_list) | |
73 | |
74 # -data | |
75 parser.add_argument("-data", type=str, help="data json file path") | |
76 | |
77 # -datafolder | |
78 htext = "datafolder file path, available for action : " | |
79 htext +="post_vpz_output, post_vpz_inout," | |
80 htext +="post_vpz_experiment, post_vpz_report" | |
81 parser.add_argument("-datafolder", type=str, help=htext) | |
82 | |
83 # -experimentfile | |
84 htext = "experimentfile path, available for action : post_vpz_experiment" | |
85 parser.add_argument("-experimentfile", type=str, help=htext) | |
86 | |
87 args = parser.parse_args() | |
88 | |
89 if (args.action is None) or (args.action == 'None') : | |
90 warn = "NO action given => enforced to 'help'" | |
91 r["warning"] = warn | |
92 action = 'help' # enforced (default) | |
93 else : | |
94 action = args.action | |
95 | |
96 if (args.data is None) or (args.data == 'None') : | |
97 data = dict() # NO data (JSON format) given as argument | |
98 else : | |
99 jsoninput_filename = args.data | |
100 inputfile = open(jsoninput_filename, 'r') | |
101 data = json.load(inputfile) # get data | |
102 | |
103 if (args.datafolder is None) or (args.datafolder == 'None') : | |
104 pass # NO datafolder given | |
105 else : | |
106 data['datafolder'] = args.datafolder | |
107 | |
108 if (args.experimentfile is None) or (args.experimentfile == 'None') : | |
109 pass # NO experimentfile given | |
110 else : | |
111 data['experimentfile'] = args.experimentfile | |
112 | |
113 data['action'] = action | |
114 | |
115 r["data"] = data | |
116 | |
117 # configuration | |
118 if MAIN_DEBUG: r["config"] = config.get_as_dict() # debug | |
119 | |
120 r_init = erecord_api.init(config) | |
121 if MAIN_DEBUG: r["init"] = r_init # debug | |
122 | |
123 response = erecord_api.action(config, data) | |
124 json_response = json.loads(response) | |
125 | |
126 # Restitution : file (experiment.xls, conditions.xls, report.zip) | |
127 # Note : | |
128 # tool.xml command "touch" the file (in case of failed to be copied here) | |
129 # for it to exist (even if empty) => able to be found | |
130 if action in ['get_vpz_experiment', 'post_vpz_experiment', | |
131 'get_vpz_report_conditions', 'post_vpz_report_conditions', | |
132 'get_vpz_report', 'post_vpz_report']: | |
133 try: | |
134 filename = json_response["outputfile_name"] | |
135 outputfile_path = json_response["outputfile_path"] | |
136 shutil.copyfile(src=outputfile_path, | |
137 dst=os.path.join(config.out_path,filename)) | |
138 # tool.xml uses them | |
139 | |
140 if not MAIN_DEBUG: # some response information not kept | |
141 for k in ('outputfile_name', 'outputfile_path', 'zip_file_path'): | |
142 if k in json_response.keys(): del(json_response[k]) | |
143 | |
144 except Exception as e : | |
145 | |
146 msg = PM+"[second file " | |
147 if "outputfile_name" in json_response.keys() : | |
148 msg = "('"+ json_response["outputfile_name"] + "')" | |
149 msg = " restitution]" | |
150 cnt = build_error_content(exception=e, errormsg=msg) | |
151 json_response["error PLUS"] = str(cnt) | |
152 | |
153 except Exception as e : | |
154 | |
155 response = erecord_api.error_case(data=data, msg=PM, e=e) | |
156 json_response = json.loads(response) | |
157 | |
158 r["response"] = json_response | |
159 | |
160 try : | |
161 # Restitution : json file | |
162 jsonoutput_filepath = os.path.join(config.out_path, | |
163 config.jsonoutput_filename) | |
164 with open(jsonoutput_filepath, "w") as outfile: | |
165 json.dump(r, outfile) | |
166 except : | |
167 print(PM, " -- error when trying to open output json file --") | |
168 |