Test Failed
Pull Request — master (#892)
by
unknown
04:03
created

scripts.savu_mod.savu_mod   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 9

5 Functions

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