|
| 1 | +"""ARGS. |
| 2 | +
|
| 3 | +:Description: Handling of command line arguments. |
| 4 | +
|
| 5 | +:Author: Martin Kilbinger <martin.kilbinger@cea.fr> |
| 6 | +
|
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | +import os |
| 12 | + |
| 13 | +from optparse import OptionParser |
| 14 | + |
| 15 | + |
| 16 | +def parse_options(p_def, short_options, types, help_strings, args=None): |
| 17 | + """Parse command line options. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + p_def : dict |
| 22 | + default parameter values |
| 23 | + short_options : dict |
| 24 | + command line options short (one character) versions |
| 25 | + types : dict |
| 26 | + command line options types |
| 27 | + help_strings : dict |
| 28 | + command line options help strings |
| 29 | + args : list, optional |
| 30 | + list of arguments, default is None, in which case sys.args[1:] |
| 31 | + is used |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + options: tuple |
| 36 | + Command line options |
| 37 | +
|
| 38 | + """ |
| 39 | + if args is None: |
| 40 | + args = sys.argv[1:] |
| 41 | + |
| 42 | + usage = "%prog [OPTIONS]" |
| 43 | + parser = OptionParser(usage=usage) |
| 44 | + |
| 45 | + # Loop over default parameter values |
| 46 | + for key in p_def: |
| 47 | + # Process if help string exists |
| 48 | + if key in help_strings: |
| 49 | + # Set short option |
| 50 | + short = short_options[key] if key in short_options else "" |
| 51 | + |
| 52 | + # Set type |
| 53 | + typ = types[key] if key in types else "string" |
| 54 | + |
| 55 | + # Special case: bool type |
| 56 | + if typ == "bool": |
| 57 | + parser.add_option( |
| 58 | + f"{short}", |
| 59 | + f"--{key}", |
| 60 | + dest=key, |
| 61 | + default=False, |
| 62 | + action="store_true", |
| 63 | + help=help_strings[key].format(p_def[key]), |
| 64 | + ) |
| 65 | + else: |
| 66 | + parser.add_option( |
| 67 | + short, |
| 68 | + f"--{key}", |
| 69 | + dest=key, |
| 70 | + type=typ, |
| 71 | + default=p_def[key], |
| 72 | + help=help_strings[key].format(p_def[key]), |
| 73 | + ) |
| 74 | + |
| 75 | + # Add verbose option |
| 76 | + parser.add_option( |
| 77 | + "-v", |
| 78 | + "--verbose", |
| 79 | + dest="verbose", |
| 80 | + action="store_true", |
| 81 | + help="verbose output", |
| 82 | + ) |
| 83 | + |
| 84 | + options_values, my_args = parser.parse_args(args) |
| 85 | + |
| 86 | + # Transform parameter values to dict |
| 87 | + options = { |
| 88 | + key: getattr(options_values, key) for key in vars(options_values) |
| 89 | + } |
| 90 | + |
| 91 | + # Add other default values which do not have argument option |
| 92 | + for key in p_def: |
| 93 | + if key not in options: |
| 94 | + options[key] = p_def[key] |
| 95 | + |
| 96 | + return options |
| 97 | + |
| 98 | + |
| 99 | +def my_string_split(string, num=-1, verbose=False, stop=False, sep=None): |
| 100 | + """My String Split. |
| 101 | +
|
| 102 | + Split a *string* into a list of strings. Choose as separator |
| 103 | + the first in the list [space, underscore] that occurs in the string. |
| 104 | + (Thus, if both occur, use space.) |
| 105 | +
|
| 106 | + Parameters |
| 107 | + ---------- |
| 108 | + string : str |
| 109 | + Input string |
| 110 | + num : int |
| 111 | + Required length of output list of strings, -1 if no requirement. |
| 112 | + verbose : bool |
| 113 | + Verbose output |
| 114 | + stop : bool |
| 115 | + Stop programs with error if True, return None and continues otherwise |
| 116 | + sep : bool |
| 117 | + Separator, try ' ', '_', and '.' if None (default) |
| 118 | +
|
| 119 | + Raises |
| 120 | + ------ |
| 121 | + ValueError |
| 122 | + If number of elements in string and num are different, for stop=True |
| 123 | + If no separator found in string |
| 124 | +
|
| 125 | + Returns |
| 126 | + ------- |
| 127 | + list |
| 128 | + List of string on success, and None if failed |
| 129 | +
|
| 130 | + """ |
| 131 | + if string is None: |
| 132 | + return None |
| 133 | + |
| 134 | + if sep is None: |
| 135 | + has_space = string.find(" ") |
| 136 | + has_underscore = string.find("_") |
| 137 | + has_dot = string.find(".") |
| 138 | + |
| 139 | + if has_space != -1: |
| 140 | + my_sep = " " |
| 141 | + elif has_underscore != -1: |
| 142 | + my_sep = "_" |
| 143 | + elif has_dot != -1: |
| 144 | + my_sep = "." |
| 145 | + else: |
| 146 | + # no separator found, does string consist of only one element? |
| 147 | + if num == -1 or num == 1: |
| 148 | + my_sep = None |
| 149 | + else: |
| 150 | + raise ValueError( |
| 151 | + "No separator (' ', '_', or '.') found in string" |
| 152 | + + f" '{string}', cannot split" |
| 153 | + ) |
| 154 | + else: |
| 155 | + if not string.find(sep): |
| 156 | + raise ValueError( |
| 157 | + f"No separator '{sep}' found in string '{string}' cannot split" |
| 158 | + ) |
| 159 | + my_sep = sep |
| 160 | + |
| 161 | + res = string.split(my_sep) |
| 162 | + |
| 163 | + if num != -1 and num != len(res) and stop: |
| 164 | + raise ValueError( |
| 165 | + f"String '{len(res)}' has length {num}, required is {num}" |
| 166 | + ) |
| 167 | + |
| 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