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:: savu_mod |
17
|
|
|
:platform: Unix |
18
|
|
|
:synopsis: A command line tool for editing process lists |
19
|
|
|
|
20
|
|
|
.. moduleauthor:: Jessica Verschoyle <[email protected]> |
21
|
|
|
|
22
|
|
|
""" |
23
|
|
|
|
24
|
|
|
import os |
25
|
|
|
import argparse |
26
|
|
|
from datetime import datetime |
27
|
|
|
|
28
|
|
|
from scripts.config_generator.config_utils import error_catcher_savu |
29
|
|
|
|
30
|
|
|
import warnings |
31
|
|
|
with warnings.catch_warnings(): |
32
|
|
|
warnings.simplefilter("ignore") |
33
|
|
|
import scripts.config_generator.savu_config as sc |
34
|
|
|
from scripts.config_generator.content import Content |
35
|
|
|
from scripts.config_generator.display_formatter import DispDisplay |
36
|
|
|
|
37
|
|
|
def arg_parser(doc=True): |
38
|
|
|
""" Arg parser for command line arguments. |
39
|
|
|
""" |
40
|
|
|
desc = "Modify one parameter value inside one process list." |
41
|
|
|
parser = argparse.ArgumentParser(prog='savu_mod', description=desc) |
42
|
|
|
parser.add_argument('plugin', |
43
|
|
|
help='Plugin name or number', |
44
|
|
|
type=str) |
45
|
|
|
parser.add_argument('plugin_index', |
46
|
|
|
help='Associated plugin index (for when identical plugins are present in the process list)', |
47
|
|
|
nargs="?", type=int, default=1) |
48
|
|
|
parser.add_argument('param', |
49
|
|
|
help='Parameter name or number from the list of plugin parameters', |
50
|
|
|
type=str) |
51
|
|
|
parser.add_argument("value", help="New parameter value") |
52
|
|
|
parser.add_argument('process_list', |
53
|
|
|
help='Process list file path', |
54
|
|
|
type=str) |
55
|
|
|
save_str = "Save the modified process list without a confirmation." |
56
|
|
|
parser.add_argument("-q", "--quick", action="store_true", |
57
|
|
|
dest="save", help=save_str, default=False) |
58
|
|
|
copy_str = "Save the modified process list to the same location, " \ |
59
|
|
|
"with a timestamp appended to the name." |
60
|
|
|
parser.add_argument("-c", "--copy", action="store_true", |
61
|
|
|
dest="copy", help=copy_str, default=False) |
62
|
|
|
return parser if doc is True else parser.parse_args() |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def load_process_list(process_list): |
66
|
|
|
""" Load only plugins inside the process list to the configurator""" |
67
|
|
|
content = Content(level='basic') |
68
|
|
|
# Open the process list file |
69
|
|
|
content.fopen(process_list, update=True) |
70
|
|
|
# Refresh the plugins |
71
|
|
|
sc._ref(content, '* -n') |
72
|
|
|
return content |
73
|
|
|
|
74
|
|
|
def timestamp_file(filename): |
75
|
|
|
""" Return a filename with a timestamp |
76
|
|
|
|
77
|
|
|
:param filename: filename input |
78
|
|
|
:return: t_filename, the filename with a timestamp |
79
|
|
|
""" |
80
|
|
|
cur_time = datetime.now().strftime("%Y_%m_%d-%Hh_%Mm_%Ss_%f") |
81
|
|
|
filelist = filename.split(".") |
82
|
|
|
t_filename = f"{filelist[0]}_{cur_time}.{filelist[1]}" |
83
|
|
|
return t_filename |
84
|
|
|
|
85
|
|
|
def modify_content(args): |
86
|
|
|
"""Load the process list and modify the parameter value. |
87
|
|
|
(The savu_config function isn't accessed directly as the |
88
|
|
|
arg parser takes different arguments) |
89
|
|
|
""" |
90
|
|
|
if not os.path.isfile(args.process_list): |
91
|
|
|
raise ValueError("Please enter a valid directory.") |
92
|
|
|
|
93
|
|
|
content = load_process_list(args.process_list) |
94
|
|
|
# Modify the plugin and parameter value |
95
|
|
|
plugin = content.plugin_to_num(args.plugin, args.plugin_index) |
96
|
|
|
content_modified = content.modify(plugin, args.param, args.value) |
97
|
|
|
|
98
|
|
|
return content, content_modified |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
def save_content(content, args): |
102
|
|
|
"""Save the plugin list with the modified parameter value """ |
103
|
|
|
content.check_file(args.process_list) |
104
|
|
|
DispDisplay(content.plugin_list)._notices() |
105
|
|
|
filename = timestamp_file(args.process_list) \ |
106
|
|
|
if args.copy else args.process_list |
107
|
|
|
save_file = "y" if args.save else input("Are you sure you want to save " |
108
|
|
|
"the modified process list to %s [y/N]" % (filename)) |
109
|
|
|
content.save(filename, check=save_file) |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
@error_catcher_savu |
113
|
|
|
def command_line_modify(): |
114
|
|
|
""" Allows modification of one parameter from one plugin. |
115
|
|
|
Aims to allow easier modification when performing batch processing |
116
|
|
|
and applying the same process lists to multiple data sets, with |
117
|
|
|
minor adjustments |
118
|
|
|
""" |
119
|
|
|
args = arg_parser(doc=False) |
120
|
|
|
|
121
|
|
|
content, content_modified = modify_content(args) |
122
|
|
|
if content_modified: |
123
|
|
|
print(f"Parameter {args.param} for the plugin {args.plugin} was " |
124
|
|
|
f"modified to {args.value}.") |
125
|
|
|
save_content(content, args) |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
if __name__ == '__main__': |
129
|
|
|
command_line_modify() |
130
|
|
|
|
131
|
|
|
|