|
8 | 8 | """ |
9 | 9 |
|
10 | 10 | import sys |
| 11 | +import os |
| 12 | + |
11 | 13 | from optparse import OptionParser |
12 | 14 |
|
13 | 15 |
|
@@ -45,18 +47,10 @@ def parse_options(p_def, short_options, types, help_strings, args=None): |
45 | 47 | # Process if help string exists |
46 | 48 | if key in help_strings: |
47 | 49 | # Set short option |
48 | | - if key in short_options: |
49 | | - short = short_options[key] |
50 | | - else: |
51 | | - # Default is no short option |
52 | | - short = "" |
| 50 | + short = short_options[key] if key in short_options else "" |
53 | 51 |
|
54 | 52 | # Set type |
55 | | - if key in types: |
56 | | - typ = types[key] |
57 | | - else: |
58 | | - # Default is str |
59 | | - typ = "string" |
| 53 | + typ = types[key] if key in types else "string" |
60 | 54 |
|
61 | 55 | # Special case: bool type |
62 | 56 | if typ == "bool": |
@@ -84,15 +78,15 @@ def parse_options(p_def, short_options, types, help_strings, args=None): |
84 | 78 | "--verbose", |
85 | 79 | dest="verbose", |
86 | 80 | action="store_true", |
87 | | - help=f"verbose output", |
| 81 | + help="verbose output", |
88 | 82 | ) |
89 | 83 |
|
90 | 84 | options_values, my_args = parser.parse_args(args) |
91 | 85 |
|
92 | 86 | # Transform parameter values to dict |
93 | | - options = {} |
94 | | - for key in vars(options_values): |
95 | | - options[key] = getattr(options_values, key) |
| 87 | + options = { |
| 88 | + key: getattr(options_values, key) for key in vars(options_values) |
| 89 | + } |
96 | 90 |
|
97 | 91 | # Add other default values which do not have argument option |
98 | 92 | for key in p_def: |
@@ -172,3 +166,51 @@ def my_string_split(string, num=-1, verbose=False, stop=False, sep=None): |
172 | 166 | ) |
173 | 167 |
|
174 | 168 | return res |
| 169 | + |
| 170 | + |
| 171 | +def read_param_script(params_in_path, params_def, verbose=False): |
| 172 | + """Read Params Scritpt. |
| 173 | +
|
| 174 | + Reads a python parameter script, or prompts user for input if file does not |
| 175 | + exist. |
| 176 | + Returns updated parameters. |
| 177 | +
|
| 178 | + Parameters |
| 179 | + ---------- |
| 180 | + params_in_path : str |
| 181 | + input file path |
| 182 | + params : dict |
| 183 | + default parameters |
| 184 | + verbose : bool, optional |
| 185 | + verbose output if ``True``; default is ``False`` |
| 186 | +
|
| 187 | + Returns |
| 188 | + ------- |
| 189 | + dict : |
| 190 | + updated parameters |
| 191 | +
|
| 192 | + """ |
| 193 | + params_in = {} |
| 194 | + params_out = {} |
| 195 | + for key in params_def: |
| 196 | + params_out[key] = params_def[key] |
| 197 | + |
| 198 | + if os.path.exists(params_in_path): |
| 199 | + if verbose: |
| 200 | + print(f"Reading parameter script {params_in_path}") |
| 201 | + with open(params_in_path) as f: |
| 202 | + exec(f.read()) |
| 203 | + # Set instance parameters, copy from above |
| 204 | + for key in params_in: |
| 205 | + params_out[key] = params_in[key] |
| 206 | + else: |
| 207 | + print( |
| 208 | + f"Configuration script {params_in_path} not found, asking for user input" |
| 209 | + ) |
| 210 | + for key in params_def: |
| 211 | + msg = f"{key}? [{params_def[key]}] " |
| 212 | + val_user = input(msg) |
| 213 | + if val_user != "": |
| 214 | + params_out[key] = val_user |
| 215 | + |
| 216 | + return params_out |
0 commit comments