1
|
|
|
# Copyright 2014 Diamond Light Source Ltd. |
2
|
|
|
# |
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
4
|
|
|
# you may not use this file except in compliance with the License. |
5
|
|
|
# You may obtain a copy of the License at |
6
|
|
|
# |
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
# |
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
# See the License for the specific language governing permissions and |
13
|
|
|
# limitations under the License. |
14
|
|
|
|
15
|
|
|
""" |
16
|
|
|
.. module:: arg_parsers |
17
|
|
|
:platform: Unix |
18
|
|
|
:synopsis: Contains all the argparse instances for configurator \ |
19
|
|
|
input commands. |
20
|
|
|
|
21
|
|
|
.. moduleauthor:: Nicola Wadeson <[email protected]> |
22
|
|
|
|
23
|
|
|
""" |
24
|
|
|
import argparse |
25
|
|
|
|
26
|
|
|
from . import savu_config as sc |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class MyException(Exception): |
30
|
|
|
def __init__(self, message, args=None): |
31
|
|
|
super(MyException, self).__init__(message) |
32
|
|
|
self.args = args |
33
|
|
|
|
34
|
|
|
def args(self): |
35
|
|
|
return self.args |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class ArgumentParser(argparse.ArgumentParser): |
39
|
|
|
|
40
|
|
|
def error(self, message): |
41
|
|
|
raise MyException(message) |
42
|
|
|
|
43
|
|
|
def exit(self, status=0, message=None): |
44
|
|
|
raise MyException(message) |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def __arg_parser(parser, args, command, doc): |
48
|
|
|
try: |
49
|
|
|
args = parser.parse_args(args=args) |
50
|
|
|
except MyException as e: |
51
|
|
|
args = e.args |
52
|
|
|
if args is not None: |
53
|
|
|
print(e) |
54
|
|
|
print(f"Please type '{command} -h' for help.") |
55
|
|
|
return parser if doc==True else args |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def _config_arg_parser(doc=True): |
59
|
|
|
""" Argument parser for command line arguments. """ |
60
|
|
|
desc = "Create process lists of plugins for Savu." |
61
|
|
|
parser = argparse.ArgumentParser(prog='savu_config.py', description=desc) |
62
|
|
|
disp_str = "Set the display level to show ALL parameters by default." |
63
|
|
|
parser.add_argument("-a", "--all", action="store_true", dest="disp_all", |
64
|
|
|
help=disp_str, default=False) |
65
|
|
|
input_str = "Open a Savu process list." |
66
|
|
|
parser.add_argument("-i", "--input", dest="file", help=input_str) |
67
|
|
|
parser.add_argument("-e", "--error", dest="error", help="Shows all errors that Savu encounters.", |
68
|
|
|
action='store_true', default=False) |
69
|
|
|
parser.add_argument("--examples", dest="examples", action='store_true', |
70
|
|
|
help="Add example plugins.", default=False) |
71
|
|
|
parser.add_argument("--tree", dest="tree", |
72
|
|
|
help="Display the tree view of a hdf/nxs file.", |
73
|
|
|
type=str, default=None, required=False) |
74
|
|
|
parser.add_argument("--check", dest="check", |
75
|
|
|
help="Check if a tomographic data file (hdf/nxs) contains conventional" |
76
|
|
|
" dataset-paths for NxTomoLoader.", |
77
|
|
|
type=str, default=None, required=False) |
78
|
|
|
parser.add_argument("--find", dest="find", nargs=2, action="append", |
79
|
|
|
help="Find paths to datasets in a hdf/nxs file matching a pattern", |
80
|
|
|
type=str, default=None, required=False) |
81
|
|
|
return parser if doc==True else parser.parse_args() |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def __verbosity_arguments(parser, q_str, v_str, vv_str): |
85
|
|
|
""" Add verbosity arguments to a parser. """ |
86
|
|
|
parser.add_argument("-q", "--quiet", action="store_true", dest="quiet", |
87
|
|
|
help="Quiet mode. "+q_str, default=False) |
88
|
|
|
parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", |
89
|
|
|
help="Verbose mode. "+v_str, default=False) |
90
|
|
|
parser.add_argument("-vv", "--vverbose", action="store_true", |
91
|
|
|
dest="vverbose", help="Verbose verbose mode. "+vv_str, |
92
|
|
|
default=False) |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def _open_arg_parser(args=None, doc=True): |
96
|
|
|
""" Argument parser for open command. """ |
97
|
|
|
desc = sc.get_description()['open'] |
98
|
|
|
parser = ArgumentParser(prog='open', description=desc) |
99
|
|
|
file_str = "The path to the process list to open." |
100
|
|
|
parser.add_argument('file', help=file_str) |
101
|
|
|
parser.add_argument('-s', '--skip', help='Skip broken plugins.', |
102
|
|
|
action='store_true', default=False) |
103
|
|
|
return __arg_parser(parser, args, 'open', doc) |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
def _disp_arg_parser(args=None, doc=True): |
107
|
|
|
""" Argument parser for disp command. """ |
108
|
|
|
desc = sc.get_description()['disp'] |
109
|
|
|
parser = ArgumentParser(prog='disp', description=desc) |
110
|
|
|
q_str = "Display plugin names only." |
111
|
|
|
v_str = "Display plugin names, synopsis and parameter details." |
112
|
|
|
vv_str = \ |
113
|
|
|
"Display plugin names, full synopsis, parameter details and warnings." |
114
|
|
|
__verbosity_arguments(parser, q_str, v_str, vv_str) |
115
|
|
|
|
116
|
|
|
all_str = "Display ALL parameters (user parameters only by default)." |
117
|
|
|
parser.add_argument("-a", "--all", action='store_true', help=all_str, |
118
|
|
|
default=False) |
119
|
|
|
l_str = "Display current parameter visibility level." |
120
|
|
|
parser.add_argument("-l", "--level", action='store_true', help=l_str, |
121
|
|
|
default=False) |
122
|
|
|
dataset_str = "Display in_datasets and out_datasets." |
123
|
|
|
parser.add_argument("-d", "--datasets", action='store_true', help=dataset_str, |
124
|
|
|
default=False) |
125
|
|
|
|
126
|
|
|
parser.add_argument("start", nargs='?', help="Display this list entry.") |
127
|
|
|
stop_str = "Display entries from start to stop." |
128
|
|
|
parser.add_argument("stop", nargs='?', help=stop_str) |
129
|
|
|
return __arg_parser(parser, args, 'disp', doc) |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
def _list_arg_parser(args=None, doc=True): |
133
|
|
|
""" Argument parser for list command. """ |
134
|
|
|
desc = sc.get_description()['list'] |
135
|
|
|
parser = ArgumentParser(prog='list', description=desc) |
136
|
|
|
q_str = "List plugin names only." |
137
|
|
|
v_str = "List plugin names and full synopsis." |
138
|
|
|
vv_str = "List all information." |
139
|
|
|
__verbosity_arguments(parser, q_str, v_str, vv_str) |
140
|
|
|
type_str = "List plugins or collections containing this string." |
141
|
|
|
parser.add_argument("string", nargs='?', help=type_str, default="") |
142
|
|
|
return __arg_parser(parser, args, 'list', doc) |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
def _save_arg_parser(args=None, doc=True): |
146
|
|
|
""" Argument parser for save command. """ |
147
|
|
|
desc = sc.get_description()['save'] |
148
|
|
|
parser = ArgumentParser(prog='save', description=desc) |
149
|
|
|
parser.add_argument("filepath", nargs='?', help="The output file path.") |
150
|
|
|
parser.add_argument("-i", "--input", action="store_true", default=False, |
151
|
|
|
help="Save to the input file.") |
152
|
|
|
parser.add_argument("-t", "--template", action="store_true", default=False, |
153
|
|
|
help="Create a Savu template file.") |
154
|
|
|
return __arg_parser(parser, args, 'save', doc) |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
def _mod_arg_parser(args=None, doc=True): |
158
|
|
|
""" Argument parser for mod command. """ |
159
|
|
|
# required to allow negative values in a list or tuple |
160
|
|
|
if args: |
161
|
|
|
args.insert(1, '--') |
162
|
|
|
desc = sc.get_description()['mod'] |
163
|
|
|
parser = ArgumentParser(prog='mod', description=desc) |
164
|
|
|
param_str = ("The plugin parameter to modify. Either " |
165
|
|
|
"'plugin_pos.param_name' or ' plugin_pos.param_no'") |
166
|
|
|
parser.add_argument("param", help=param_str) |
167
|
|
|
val_str = "The plugin parameter value." |
168
|
|
|
parser.add_argument("value", nargs='*', help=val_str) |
169
|
|
|
parser.add_argument("-d", "--default", action="store_true", default=False, |
170
|
|
|
help="Revert to default value.") |
171
|
|
|
return __arg_parser(parser, args, 'mod', doc) |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
def _set_arg_parser(args=None, doc=True): |
175
|
|
|
""" Argument parser for set command. """ |
176
|
|
|
desc = sc.get_description()['set'] |
177
|
|
|
parser = ArgumentParser(prog='set', description=desc) |
178
|
|
|
parser.add_argument('plugin_pos', type=str, nargs='+', |
179
|
|
|
help="Plugin position(s).") |
180
|
|
|
parser.add_argument("status", type=str, choices=['on', 'ON', 'off', 'OFF'], |
181
|
|
|
help="Plugin status.") |
182
|
|
|
return __arg_parser(parser, args, 'set', doc) |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
def _add_arg_parser(args=None, doc=True): |
186
|
|
|
""" Argument parser for add command. """ |
187
|
|
|
desc = sc.get_description()['add'] |
188
|
|
|
parser = ArgumentParser(prog='add', description=desc) |
189
|
|
|
parser.add_argument("name", help="The plugin name.") |
190
|
|
|
pos_str = "Plugin list position (defaults to end)." |
191
|
|
|
parser.add_argument('pos', nargs='?', help=pos_str) |
192
|
|
|
return __arg_parser(parser, args, 'add', doc) |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
def _dupl_arg_parser(args=None, doc=True): |
196
|
|
|
""" Argument parser for dupl command. """ |
197
|
|
|
desc = sc.get_description()['dupl'] |
198
|
|
|
parser = ArgumentParser(prog='dupl', description=desc) |
199
|
|
|
orig_pos_str = "The position of the plugin to be duplicated." |
200
|
|
|
parser.add_argument("orig_pos", help=orig_pos_str) |
201
|
|
|
pos_str = "Position for the new plugin (defaults to end)." |
202
|
|
|
parser.add_argument('new_pos', nargs='?', help=pos_str) |
203
|
|
|
return __arg_parser(parser, args, 'dupl', doc) |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
def _ref_arg_parser(args=None, doc=True): |
207
|
|
|
""" Argument parser for ref command. """ |
208
|
|
|
desc = sc.get_description()['ref'] |
209
|
|
|
parser = ArgumentParser(prog='ref', description=desc) |
210
|
|
|
plugin_str = "Plugin position to refresh or '*' for the whole list" |
211
|
|
|
parser.add_argument("pos", nargs='+', help=plugin_str) |
212
|
|
|
defaults_str = "Populate parameters with default values." |
213
|
|
|
parser.add_argument("-d", "--defaults", "--default", action="store_true", |
214
|
|
|
dest="defaults", help=defaults_str, default=False) |
215
|
|
|
parser.add_argument("-n", "--nodisp", action="store_true", dest="nodisp", |
216
|
|
|
help=argparse.SUPPRESS, default=False) |
217
|
|
|
return __arg_parser(parser, args, 'ref', doc) |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
def _rem_arg_parser(args=None, doc=True): |
221
|
|
|
""" Argument parser for rem command. """ |
222
|
|
|
desc = sc.get_description()['rem'] |
223
|
|
|
parser = ArgumentParser(prog='rem', description=desc) |
224
|
|
|
parser.add_argument('pos', type=str, nargs='+', |
225
|
|
|
help="Plugin position(s).") |
226
|
|
|
return __arg_parser(parser, args, 'rem', doc) |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
def _cite_arg_parser(args=None, doc=True): |
230
|
|
|
""" Argument parser for cite command. """ |
231
|
|
|
desc = sc.get_description()['cite'] |
232
|
|
|
parser = ArgumentParser(prog='cite', description=desc) |
233
|
|
|
parser.add_argument("start", nargs='?', help="Display this plugin citation.") |
234
|
|
|
stop_str = "Display plugins from start to stop." |
235
|
|
|
parser.add_argument("stop", nargs='?', help=stop_str) |
236
|
|
|
return __arg_parser(parser, args, 'cite', doc) |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
def _move_arg_parser(args=None, doc=True): |
240
|
|
|
""" Argument parser for move command. """ |
241
|
|
|
desc = sc.get_description()['move'] |
242
|
|
|
parser = ArgumentParser(prog='move', description=desc) |
243
|
|
|
parser.add_argument("orig_pos", help="Original position.") |
244
|
|
|
parser.add_argument('new_pos', help="New position.") |
245
|
|
|
return __arg_parser(parser, args, 'move', doc) |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
def _replace_arg_parser(args=None, doc=True): |
249
|
|
|
""" Argument parser for replace command. """ |
250
|
|
|
desc = sc.get_description()['replace'] |
251
|
|
|
parser = ArgumentParser(prog='replace', description=desc) |
252
|
|
|
parser.add_argument("old", help="Old plugin position.") |
253
|
|
|
parser.add_argument('new_plugin', help="New plugin name.") |
254
|
|
|
return __arg_parser(parser, args, 'replace', doc) |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
def _coll_arg_parser(args=None, doc=True): |
258
|
|
|
""" Argument parser for coll command. """ |
259
|
|
|
desc = sc.get_description()['coll'] |
260
|
|
|
parser = ArgumentParser(prog='coll', description=desc) |
261
|
|
|
return __arg_parser(parser, args, 'coll', doc) |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
def _level_arg_parser(args=None, doc=True): |
265
|
|
|
""" Argument parser for level command. """ |
266
|
|
|
desc = sc.get_description()['level'] |
267
|
|
|
parser = ArgumentParser(prog='level', description=desc) |
268
|
|
|
level_str = "The visibility level. Display the current visibility level" \ |
269
|
|
|
" by using 'level' without an argument." |
270
|
|
|
parser.add_argument("level", nargs='?', help=level_str |
271
|
|
|
, choices=['basic', 'intermediate', 'advanced']) |
272
|
|
|
return __arg_parser(parser, args, 'level', doc) |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
def _expand_arg_parser(args=None, doc=True): |
276
|
|
|
""" Argument parser for expand command. """ |
277
|
|
|
desc = sc.get_description()["expand"] |
278
|
|
|
parser = ArgumentParser(prog="expand", description=desc) |
279
|
|
|
plugin_pos_str = "Expand this plugin preview parameter" |
280
|
|
|
parser.add_argument("plugin_pos", nargs="?", help=plugin_pos_str) |
281
|
|
|
dim_str = "Data dimensions. If this value is the same as the " \ |
282
|
|
|
"current dimension value, then the dimension of the " \ |
283
|
|
|
"preview parameter will be unchanged. If it is greater " \ |
284
|
|
|
"or smaller than the current dimension value, then the " \ |
285
|
|
|
"dimension of the preview value will be altered." |
286
|
|
|
parser.add_argument("dim", nargs="?", type=int, help=dim_str) |
287
|
|
|
# Hidden argument to use when argument can be string and one |
288
|
|
|
# dimension is displayed |
289
|
|
|
parser.add_argument('dim_view', nargs="?", default=False, |
290
|
|
|
help=argparse.SUPPRESS) |
291
|
|
|
expand_off_str = "Turn off the expand view" |
292
|
|
|
parser.add_argument("--off", action="store_true", |
293
|
|
|
dest="off", help=expand_off_str, default=False) |
294
|
|
|
return __arg_parser(parser, args, "expand", doc) |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
def _clear_arg_parser(args=None, doc=True): |
298
|
|
|
""" Argument parser for clear command. """ |
299
|
|
|
desc = sc.get_description()['clear'] |
300
|
|
|
parser = ArgumentParser(prog='clear', description=desc) |
301
|
|
|
return __arg_parser(parser, args, 'clear', doc) |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
def _history_arg_parser(args=None, doc=True): |
305
|
|
|
""" Argument parser for history command. """ |
306
|
|
|
desc = sc.get_description()['history'] |
307
|
|
|
parser = ArgumentParser(prog='history', description=desc) |
308
|
|
|
return __arg_parser(parser, args, 'history', doc) |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
def _exit_arg_parser(args=None, doc=True): |
312
|
|
|
""" Argument parser for exit command """ |
313
|
|
|
desc = sc.get_description()['exit'] |
314
|
|
|
parser = ArgumentParser(prog='exit', description=desc) |
315
|
|
|
return __arg_parser(parser, args, 'exit', doc) |
316
|
|
|
|
317
|
|
|
|
318
|
|
|
def _help_arg_parser(args=None, doc=True): |
319
|
|
|
""" Argument parser for help command """ |
320
|
|
|
desc = sc.get_description()['help'] |
321
|
|
|
parser = ArgumentParser(prog='help', description=desc) |
322
|
|
|
return __arg_parser(parser, args, 'help', doc) |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
def _get_verbosity(args): |
326
|
|
|
if args.vverbose: |
327
|
|
|
return '-vv' |
328
|
|
|
elif args.verbose: |
329
|
|
|
return '-v' |
330
|
|
|
elif args.quiet: |
331
|
|
|
return '-q' |
332
|
|
|
else: |
333
|
|
|
return None |
334
|
|
|
|