Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | Define variable manipulation functions. |
| 5 | """ |
| 6 | |
| 7 | import os |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 8 | import re |
Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 9 | |
| 10 | try: |
| 11 | from robot.utils import DotDict |
| 12 | except ImportError: |
| 13 | pass |
| 14 | |
| 15 | import collections |
| 16 | |
| 17 | import gen_print as gp |
| 18 | import gen_misc as gm |
| 19 | |
| 20 | |
| 21 | def create_var_dict(*args): |
Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 22 | r""" |
| 23 | Create a dictionary whose keys/values are the arg names/arg values passed |
| 24 | to it and return it to the caller. |
| 25 | |
| 26 | Note: The resulting dictionary will be ordered. |
| 27 | |
| 28 | Description of argument(s): |
| 29 | *args An unlimited number of arguments to be processed. |
| 30 | |
| 31 | Example use: |
| 32 | |
| 33 | first_name = 'Steve' |
| 34 | last_name = 'Smith' |
| 35 | var_dict = create_var_dict(first_name, last_name) |
| 36 | |
| 37 | gp.print_var(var_dict) |
| 38 | |
| 39 | The print-out of the resulting var dictionary is: |
| 40 | var_dict: |
| 41 | var_dict[first_name]: Steve |
| 42 | var_dict[last_name]: Smith |
| 43 | """ |
| 44 | |
| 45 | try: |
| 46 | result_dict = collections.OrderedDict() |
| 47 | except AttributeError: |
| 48 | result_dict = DotDict() |
| 49 | |
| 50 | arg_num = 1 |
| 51 | for arg in args: |
| 52 | arg_name = gp.get_arg_name(None, arg_num, stack_frame_ix=2) |
| 53 | result_dict[arg_name] = arg |
| 54 | arg_num += 1 |
| 55 | |
| 56 | return result_dict |
| 57 | |
| 58 | |
| 59 | default_record_delim = ':' |
| 60 | default_key_val_delim = '.' |
| 61 | |
| 62 | |
| 63 | def join_dict(dict, |
| 64 | record_delim=default_record_delim, |
| 65 | key_val_delim=default_key_val_delim): |
Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 66 | r""" |
| 67 | Join a dictionary's keys and values into a string and return the string. |
| 68 | |
| 69 | Description of argument(s): |
| 70 | dict The dictionary whose keys and values are |
| 71 | to be joined. |
| 72 | record_delim The delimiter to be used to separate |
| 73 | dictionary pairs in the resulting string. |
| 74 | key_val_delim The delimiter to be used to separate keys |
| 75 | from values in the resulting string. |
| 76 | |
| 77 | Example use: |
| 78 | |
| 79 | gp.print_var(var_dict) |
| 80 | str1 = join_dict(var_dict) |
| 81 | gp.pvar(str1) |
| 82 | |
| 83 | Program output. |
| 84 | var_dict: |
| 85 | var_dict[first_name]: Steve |
| 86 | var_dict[last_name]: Smith |
| 87 | str1: |
| 88 | first_name.Steve:last_name.Smith |
| 89 | """ |
| 90 | |
| 91 | format_str = '%s' + key_val_delim + '%s' |
| 92 | return record_delim.join([format_str % (key, value) for (key, value) in |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 93 | dict.items()]) |
Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def split_to_dict(string, |
| 97 | record_delim=default_record_delim, |
| 98 | key_val_delim=default_key_val_delim): |
Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 99 | r""" |
| 100 | Split a string into a dictionary and return it. |
| 101 | |
| 102 | This function is the complement to join_dict. |
| 103 | |
| 104 | Description of argument(s): |
| 105 | string The string to be split into a dictionary. |
| 106 | The string must have the proper delimiters |
| 107 | in it. A string created by join_dict |
| 108 | would qualify. |
| 109 | record_delim The delimiter to be used to separate |
| 110 | dictionary pairs in the input string. |
| 111 | key_val_delim The delimiter to be used to separate |
| 112 | keys/values in the input string. |
| 113 | |
| 114 | Example use: |
| 115 | |
| 116 | gp.print_var(str1) |
| 117 | new_dict = split_to_dict(str1) |
| 118 | gp.print_var(new_dict) |
| 119 | |
| 120 | |
| 121 | Program output. |
| 122 | str1: |
| 123 | first_name.Steve:last_name.Smith |
| 124 | new_dict: |
| 125 | new_dict[first_name]: Steve |
| 126 | new_dict[last_name]: Smith |
| 127 | """ |
| 128 | |
| 129 | try: |
| 130 | result_dict = collections.OrderedDict() |
| 131 | except AttributeError: |
| 132 | result_dict = DotDict() |
| 133 | |
| 134 | raw_keys_values = string.split(record_delim) |
| 135 | for key_value in raw_keys_values: |
| 136 | key_value_list = key_value.split(key_val_delim) |
| 137 | try: |
| 138 | result_dict[key_value_list[0]] = key_value_list[1] |
| 139 | except IndexError: |
| 140 | result_dict[key_value_list[0]] = "" |
| 141 | |
| 142 | return result_dict |
| 143 | |
| 144 | |
| 145 | def create_file_path(file_name_dict, |
| 146 | dir_path="/tmp/", |
| 147 | file_suffix=""): |
Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 148 | r""" |
| 149 | Create a file path using the given parameters and return it. |
| 150 | |
| 151 | Description of argument(s): |
| 152 | file_name_dict A dictionary with keys/values which are to |
| 153 | appear as part of the file name. |
| 154 | dir_path The dir_path that is to appear as part of |
| 155 | the file name. |
| 156 | file_suffix A suffix to be included as part of the |
| 157 | file name. |
| 158 | """ |
| 159 | |
| 160 | dir_path = gm.add_trailing_slash(dir_path) |
| 161 | return dir_path + join_dict(file_name_dict) + file_suffix |
| 162 | |
| 163 | |
| 164 | def parse_file_path(file_path): |
Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 165 | r""" |
| 166 | Parse a file path created by create_file_path and return the result as a |
| 167 | dictionary. |
| 168 | |
| 169 | This function is the complement to create_file_path. |
| 170 | |
| 171 | Description of argument(s): |
| 172 | file_path The file_path. |
| 173 | |
| 174 | Example use: |
| 175 | gp.pvar(boot_results_file_path) |
| 176 | file_path_data = parse_file_path(boot_results_file_path) |
| 177 | gp.pvar(file_path_data) |
| 178 | |
| 179 | Program output. |
| 180 | |
| 181 | boot_results_file_path: |
| 182 | /tmp/pgm_name.obmc_boot_test:openbmc_nickname.beye6:master_pid.2039:boot_re |
| 183 | sults |
| 184 | file_path_data: |
| 185 | file_path_data[dir_path]: /tmp/ |
| 186 | file_path_data[pgm_name]: obmc_boot_test |
| 187 | file_path_data[openbmc_nickname]: beye6 |
| 188 | file_path_data[master_pid]: 2039 |
| 189 | file_path_data[boot_results]: |
| 190 | """ |
| 191 | |
| 192 | try: |
| 193 | result_dict = collections.OrderedDict() |
| 194 | except AttributeError: |
| 195 | result_dict = DotDict() |
| 196 | |
| 197 | dir_path = os.path.dirname(file_path) + os.sep |
| 198 | file_path = os.path.basename(file_path) |
| 199 | |
| 200 | result_dict['dir_path'] = dir_path |
| 201 | |
| 202 | result_dict.update(split_to_dict(file_path)) |
| 203 | |
| 204 | return result_dict |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def parse_key_value(string, |
| 208 | delim=":", |
| 209 | strip=" ", |
| 210 | to_lower=1, |
| 211 | underscores=1): |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 212 | r""" |
| 213 | Parse a key/value string and return as a key/value tuple. |
| 214 | |
| 215 | This function is useful for parsing a line of program output or data that |
| 216 | is in the following form: |
| 217 | <key or variable name><delimiter><value> |
| 218 | |
| 219 | An example of a key/value string would be as follows: |
| 220 | |
| 221 | Current Limit State: No Active Power Limit |
| 222 | |
| 223 | In the example shown, the delimiter is ":". The resulting key would be as |
| 224 | follows: |
| 225 | Current Limit State |
| 226 | |
| 227 | Note: If one were to take the default values of to_lower=1 and |
| 228 | underscores=1, the resulting key would be as follows: |
| 229 | current_limit_state |
| 230 | |
| 231 | The to_lower and underscores arguments are provided for those who wish to |
| 232 | have their key names have the look and feel of python variable names. |
| 233 | |
| 234 | The resulting value for the example above would be as follows: |
| 235 | No Active Power Limit |
| 236 | |
| 237 | Another example: |
| 238 | name=Mike |
| 239 | |
| 240 | In this case, the delim would be "=", the key is "name" and the value is |
| 241 | "Mike". |
| 242 | |
| 243 | Description of argument(s): |
| 244 | string The string to be parsed. |
| 245 | delim The delimiter which separates the key from |
| 246 | the value. |
| 247 | strip The characters (if any) to strip from the |
| 248 | beginning and end of both the key and the |
| 249 | value. |
| 250 | to_lower Change the key name to lower case. |
| 251 | underscores Change any blanks found in the key name to |
| 252 | underscores. |
| 253 | """ |
| 254 | |
| 255 | pair = string.split(delim) |
| 256 | |
| 257 | key = pair[0].strip(strip) |
| 258 | if len(pair) == 0: |
| 259 | value = "" |
| 260 | else: |
MICHAEL J. WALSH | 9509a0f | 2018-02-08 11:08:52 -0600 | [diff] [blame] | 261 | value = delim.join(pair[1:]).strip(strip) |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 262 | |
| 263 | if to_lower: |
| 264 | key = key.lower() |
| 265 | if underscores: |
| 266 | key = re.sub(r" ", "_", key) |
| 267 | |
| 268 | return key, value |
| 269 | |
| 270 | |
| 271 | def key_value_list_to_dict(list, |
Michael Walsh | cad0713 | 2018-02-19 17:28:01 -0600 | [diff] [blame] | 272 | process_indent=0, |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 273 | **args): |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 274 | r""" |
| 275 | Convert a list containing key/value strings to a dictionary and return it. |
| 276 | |
| 277 | See docstring of parse_key_value function for details on key/value strings. |
| 278 | |
| 279 | Example usage: |
| 280 | |
| 281 | For the following value of list: |
| 282 | |
| 283 | list: |
| 284 | list[0]: Current Limit State: No Active Power Limit |
| 285 | list[1]: Exception actions: Hard Power Off & Log Event to SEL |
| 286 | list[2]: Power Limit: 0 Watts |
| 287 | list[3]: Correction time: 0 milliseconds |
| 288 | list[4]: Sampling period: 0 seconds |
| 289 | |
| 290 | And the following call in python: |
| 291 | |
| 292 | power_limit = key_value_outbuf_to_dict(list) |
| 293 | |
| 294 | The resulting power_limit directory would look like this: |
| 295 | |
| 296 | power_limit: |
| 297 | [current_limit_state]: No Active Power Limit |
| 298 | [exception_actions]: Hard Power Off & Log Event to SEL |
| 299 | [power_limit]: 0 Watts |
| 300 | [correction_time]: 0 milliseconds |
| 301 | [sampling_period]: 0 seconds |
| 302 | |
Michael Walsh | cad0713 | 2018-02-19 17:28:01 -0600 | [diff] [blame] | 303 | Another example containing a sub-list (see process_indent description |
| 304 | below): |
| 305 | |
| 306 | Provides Device SDRs : yes |
| 307 | Additional Device Support : |
| 308 | Sensor Device |
| 309 | SEL Device |
| 310 | FRU Inventory Device |
| 311 | Chassis Device |
| 312 | |
| 313 | Note that the 2 qualifications for containing a sub-list are met: 1) |
| 314 | 'Additional Device Support' has no value and 2) The entries below it are |
| 315 | indented. In this case those entries contain no delimiters (":") so they |
| 316 | will be processed as a list rather than as a dictionary. The result would |
| 317 | be as follows: |
| 318 | |
| 319 | mc_info: |
| 320 | mc_info[provides_device_sdrs]: yes |
| 321 | mc_info[additional_device_support]: |
| 322 | mc_info[additional_device_support][0]: Sensor Device |
| 323 | mc_info[additional_device_support][1]: SEL Device |
| 324 | mc_info[additional_device_support][2]: FRU Inventory Device |
| 325 | mc_info[additional_device_support][3]: Chassis Device |
| 326 | |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 327 | Description of argument(s): |
| 328 | list A list of key/value strings. (See |
| 329 | docstring of parse_key_value function for |
| 330 | details). |
Michael Walsh | cad0713 | 2018-02-19 17:28:01 -0600 | [diff] [blame] | 331 | process_indent This indicates that indented |
| 332 | sub-dictionaries and sub-lists are to be |
| 333 | processed as such. An entry may have a |
| 334 | sub-dict or sub-list if 1) It has no value |
| 335 | other than blank 2) There are entries |
| 336 | below it that are indented. |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 337 | **args Arguments to be interpreted by |
| 338 | parse_key_value. (See docstring of |
| 339 | parse_key_value function for details). |
| 340 | """ |
| 341 | |
| 342 | try: |
| 343 | result_dict = collections.OrderedDict() |
| 344 | except AttributeError: |
| 345 | result_dict = DotDict() |
| 346 | |
Michael Walsh | cad0713 | 2018-02-19 17:28:01 -0600 | [diff] [blame] | 347 | if not process_indent: |
| 348 | for entry in list: |
| 349 | key, value = parse_key_value(entry, **args) |
| 350 | result_dict[key] = value |
| 351 | return result_dict |
| 352 | |
| 353 | # Process list while paying heed to indentation. |
| 354 | delim = args.get("delim", ":") |
| 355 | # Initialize "parent_" indentation level variables. |
| 356 | parent_indent = len(list[0]) - len(list[0].lstrip()) |
| 357 | sub_list = [] |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 358 | for entry in list: |
Michael Walsh | c1dfc78 | 2017-09-26 16:08:51 -0500 | [diff] [blame] | 359 | key, value = parse_key_value(entry, **args) |
Michael Walsh | cad0713 | 2018-02-19 17:28:01 -0600 | [diff] [blame] | 360 | |
| 361 | indent = len(entry) - len(entry.lstrip()) |
| 362 | |
| 363 | if indent > parent_indent and parent_value == "": |
| 364 | # This line is indented compared to the parent entry and the |
| 365 | # parent entry has no value. |
| 366 | # Append the entry to sub_list for later processing. |
| 367 | sub_list.append(str(entry)) |
| 368 | continue |
| 369 | |
| 370 | # Process any outstanding sub_list and add it to |
| 371 | # result_dict[parent_key]. |
| 372 | if len(sub_list) > 0: |
| 373 | if any(delim in word for word in sub_list): |
| 374 | # If delim is found anywhere in the sub_list, we'll process |
| 375 | # as a sub-dictionary. |
| 376 | result_dict[parent_key] = key_value_list_to_dict(sub_list, |
| 377 | **args) |
| 378 | else: |
| 379 | result_dict[parent_key] = map(str.strip, sub_list) |
| 380 | del sub_list[:] |
| 381 | |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 382 | result_dict[key] = value |
| 383 | |
Michael Walsh | cad0713 | 2018-02-19 17:28:01 -0600 | [diff] [blame] | 384 | parent_key = key |
| 385 | parent_value = value |
| 386 | parent_indent = indent |
| 387 | |
| 388 | # Any outstanding sub_list to be processed? |
| 389 | if len(sub_list) > 0: |
| 390 | if any(delim in word for word in sub_list): |
| 391 | # If delim is found anywhere in the sub_list, we'll process as a |
| 392 | # sub-dictionary. |
| 393 | result_dict[parent_key] = key_value_list_to_dict(sub_list, **args) |
| 394 | else: |
| 395 | result_dict[parent_key] = map(str.strip, sub_list) |
| 396 | |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 397 | return result_dict |
| 398 | |
| 399 | |
| 400 | def key_value_outbuf_to_dict(out_buf, |
| 401 | **args): |
Michael Walsh | 05c68d9 | 2017-09-20 16:36:37 -0500 | [diff] [blame] | 402 | r""" |
| 403 | Convert a buffer with a key/value string on each line to a dictionary and |
| 404 | return it. |
| 405 | |
| 406 | Each line in the out_buf should end with a \n. |
| 407 | |
| 408 | See docstring of parse_key_value function for details on key/value strings. |
| 409 | |
| 410 | Example usage: |
| 411 | |
| 412 | For the following value of out_buf: |
| 413 | |
| 414 | Current Limit State: No Active Power Limit |
| 415 | Exception actions: Hard Power Off & Log Event to SEL |
| 416 | Power Limit: 0 Watts |
| 417 | Correction time: 0 milliseconds |
| 418 | Sampling period: 0 seconds |
| 419 | |
| 420 | And the following call in python: |
| 421 | |
| 422 | power_limit = key_value_outbuf_to_dict(out_buf) |
| 423 | |
| 424 | The resulting power_limit directory would look like this: |
| 425 | |
| 426 | power_limit: |
| 427 | [current_limit_state]: No Active Power Limit |
| 428 | [exception_actions]: Hard Power Off & Log Event to SEL |
| 429 | [power_limit]: 0 Watts |
| 430 | [correction_time]: 0 milliseconds |
| 431 | [sampling_period]: 0 seconds |
| 432 | |
| 433 | Description of argument(s): |
| 434 | out_buf A buffer with a key/value string on each |
| 435 | line. (See docstring of parse_key_value |
| 436 | function for details). |
| 437 | **args Arguments to be interpreted by |
| 438 | parse_key_value. (See docstring of |
| 439 | parse_key_value function for details). |
| 440 | """ |
| 441 | |
| 442 | # Create key_var_list and remove null entries. |
| 443 | key_var_list = list(filter(None, out_buf.split("\n"))) |
Michael Walsh | c1dfc78 | 2017-09-26 16:08:51 -0500 | [diff] [blame] | 444 | return key_value_list_to_dict(key_var_list, **args) |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 445 | |
| 446 | |
Michael Walsh | dc97882 | 2018-07-12 15:34:13 -0500 | [diff] [blame] | 447 | def create_field_desc_regex(line): |
| 448 | |
| 449 | r""" |
| 450 | Create a field descriptor regular expression based on the input line and |
| 451 | return it. |
| 452 | |
| 453 | This function is designed for use by the list_to_report function (defined |
| 454 | below). |
| 455 | |
| 456 | Example: |
| 457 | |
| 458 | Given the following input line: |
| 459 | |
| 460 | -------- ------------ ------------------ ------------------------ |
| 461 | |
| 462 | This function will return this regular expression: |
| 463 | |
| 464 | (.{8}) (.{12}) (.{18}) (.{24}) |
| 465 | |
| 466 | This means that other report lines interpreted using the regular |
| 467 | expression are expected to have: |
| 468 | - An 8 character field |
| 469 | - 3 spaces |
| 470 | - A 12 character field |
| 471 | - One space |
| 472 | - An 18 character field |
| 473 | - One space |
| 474 | - A 24 character field |
| 475 | |
| 476 | Description of argument(s): |
| 477 | line A line consisting of dashes to represent |
| 478 | fields and spaces to delimit fields. |
| 479 | """ |
| 480 | |
| 481 | # Split the line into a descriptors list. Example: |
| 482 | # descriptors: |
| 483 | # descriptors[0]: -------- |
| 484 | # descriptors[1]: |
| 485 | # descriptors[2]: |
| 486 | # descriptors[3]: ------------ |
| 487 | # descriptors[4]: ------------------ |
| 488 | # descriptors[5]: ------------------------ |
| 489 | descriptors = line.split(" ") |
| 490 | |
| 491 | # Create regexes list. Example: |
| 492 | # regexes: |
| 493 | # regexes[0]: (.{8}) |
| 494 | # regexes[1]: |
| 495 | # regexes[2]: |
| 496 | # regexes[3]: (.{12}) |
| 497 | # regexes[4]: (.{18}) |
| 498 | # regexes[5]: (.{24}) |
| 499 | regexes = [] |
| 500 | for descriptor in descriptors: |
| 501 | if descriptor == "": |
| 502 | regexes.append("") |
| 503 | else: |
| 504 | regexes.append("(.{" + str(len(descriptor)) + "})") |
| 505 | |
| 506 | # Join the regexes list into a regex string. |
| 507 | field_desc_regex = ' '.join(regexes) |
| 508 | |
| 509 | return field_desc_regex |
| 510 | |
| 511 | |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 512 | def list_to_report(report_list, |
| 513 | to_lower=1): |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 514 | r""" |
| 515 | Convert a list containing report text lines to a report "object" and |
| 516 | return it. |
| 517 | |
| 518 | The first entry in report_list must be a header line consisting of column |
| 519 | names delimited by white space. No column name may contain white space. |
| 520 | The remaining report_list entries should contain tabular data which |
| 521 | corresponds to the column names. |
| 522 | |
| 523 | A report object is a list where each entry is a dictionary whose keys are |
| 524 | the field names from the first entry in report_list. |
| 525 | |
| 526 | Example: |
| 527 | Given the following report_list as input: |
| 528 | |
| 529 | rl: |
| 530 | rl[0]: Filesystem 1K-blocks Used Available Use% Mounted on |
| 531 | rl[1]: dev 247120 0 247120 0% /dev |
| 532 | rl[2]: tmpfs 248408 79792 168616 32% /run |
| 533 | |
| 534 | This function will return a list of dictionaries as shown below: |
| 535 | |
| 536 | df_report: |
| 537 | df_report[0]: |
| 538 | [filesystem]: dev |
| 539 | [1k-blocks]: 247120 |
| 540 | [used]: 0 |
| 541 | [available]: 247120 |
| 542 | [use%]: 0% |
| 543 | [mounted]: /dev |
| 544 | df_report[1]: |
| 545 | [filesystem]: dev |
| 546 | [1k-blocks]: 247120 |
| 547 | [used]: 0 |
| 548 | [available]: 247120 |
| 549 | [use%]: 0% |
| 550 | [mounted]: /dev |
| 551 | |
| 552 | Notice that because "Mounted on" contains a space, "on" would be |
| 553 | considered the 7th field. In this case, there is never any data in field |
| 554 | 7 so things work out nicely. A caller could do some pre-processing if |
| 555 | desired (e.g. change "Mounted on" to "Mounted_on"). |
| 556 | |
Michael Walsh | dc97882 | 2018-07-12 15:34:13 -0500 | [diff] [blame] | 557 | Example 2: |
| 558 | |
| 559 | If the 2nd line of report data is a series of dashes and spaces as in the |
| 560 | following example, that line will serve to delineate columns. |
| 561 | |
| 562 | The 2nd line of data is like this: |
| 563 | ID status size |
| 564 | tool,clientid,userid |
| 565 | -------- ------------ ------------------ ------------------------ |
| 566 | 20000001 in progress 0x7D0 ,, |
| 567 | |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 568 | Description of argument(s): |
| 569 | report_list A list where each entry is one line of |
| 570 | output from a report. The first entry |
| 571 | must be a header line which contains |
| 572 | column names. Column names may not |
| 573 | contain spaces. |
| 574 | to_lower Change the resulting key names to lower |
| 575 | case. |
| 576 | """ |
| 577 | |
Michael Walsh | dc97882 | 2018-07-12 15:34:13 -0500 | [diff] [blame] | 578 | if len(report_list) <= 1: |
| 579 | # If we don't have at least a descriptor line and one line of data, |
| 580 | # return an empty array. |
| 581 | return [] |
| 582 | |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 583 | header_line = report_list[0] |
| 584 | if to_lower: |
| 585 | header_line = header_line.lower() |
Michael Walsh | dc97882 | 2018-07-12 15:34:13 -0500 | [diff] [blame] | 586 | |
| 587 | field_desc_regex = "" |
| 588 | if re.match(r"^-[ -]*$", report_list[1]): |
| 589 | # We have a field descriptor line (as shown in example 2 above). |
| 590 | field_desc_regex = create_field_desc_regex(report_list[1]) |
| 591 | field_desc_len = len(report_list[1]) |
| 592 | pad_format_string = "%-" + str(field_desc_len) + "s" |
| 593 | # The field descriptor line has served its purpose. Deleting it. |
| 594 | del report_list[1] |
| 595 | |
| 596 | # Process the header line by creating a list of column names. |
| 597 | if field_desc_regex == "": |
| 598 | columns = header_line.split() |
| 599 | else: |
| 600 | # Pad the line with spaces on the right to facilitate processing with |
| 601 | # field_desc_regex. |
| 602 | header_line = pad_format_string % header_line |
| 603 | columns = map(str.strip, re.findall(field_desc_regex, header_line)[0]) |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 604 | |
| 605 | report_obj = [] |
| 606 | for report_line in report_list[1:]: |
Michael Walsh | dc97882 | 2018-07-12 15:34:13 -0500 | [diff] [blame] | 607 | if field_desc_regex == "": |
| 608 | line = report_line.split() |
| 609 | else: |
| 610 | # Pad the line with spaces on the right to facilitate processing |
| 611 | # with field_desc_regex. |
| 612 | report_line = pad_format_string % report_line |
| 613 | line = map(str.strip, re.findall(field_desc_regex, report_line)[0]) |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 614 | try: |
| 615 | line_dict = collections.OrderedDict(zip(columns, line)) |
| 616 | except AttributeError: |
| 617 | line_dict = DotDict(zip(columns, line)) |
| 618 | report_obj.append(line_dict) |
| 619 | |
| 620 | return report_obj |
| 621 | |
| 622 | |
| 623 | def outbuf_to_report(out_buf, |
| 624 | **args): |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 625 | r""" |
| 626 | Convert a text buffer containing report lines to a report "object" and |
| 627 | return it. |
| 628 | |
| 629 | Refer to list_to_report (above) for more details. |
| 630 | |
| 631 | Example: |
| 632 | |
| 633 | Given the following out_buf: |
| 634 | |
Michael Walsh | dc97882 | 2018-07-12 15:34:13 -0500 | [diff] [blame] | 635 | Filesystem 1K-blocks Used Available Use% Mounted |
| 636 | on |
| 637 | dev 247120 0 247120 0% /dev |
| 638 | tmpfs 248408 79792 168616 32% /run |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 639 | |
| 640 | This function will return a list of dictionaries as shown below: |
| 641 | |
| 642 | df_report: |
| 643 | df_report[0]: |
| 644 | [filesystem]: dev |
| 645 | [1k-blocks]: 247120 |
| 646 | [used]: 0 |
| 647 | [available]: 247120 |
| 648 | [use%]: 0% |
| 649 | [mounted]: /dev |
| 650 | df_report[1]: |
| 651 | [filesystem]: dev |
| 652 | [1k-blocks]: 247120 |
| 653 | [used]: 0 |
| 654 | [available]: 247120 |
| 655 | [use%]: 0% |
| 656 | [mounted]: /dev |
| 657 | |
| 658 | Other possible uses: |
| 659 | - Process the output of a ps command. |
| 660 | - Process the output of an ls command (the caller would need to supply |
Michael Walsh | dc97882 | 2018-07-12 15:34:13 -0500 | [diff] [blame] | 661 | column names) |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 662 | |
| 663 | Description of argument(s): |
Michael Walsh | dc97882 | 2018-07-12 15:34:13 -0500 | [diff] [blame] | 664 | out_buf A text report. The first line must be a |
Michael Walsh | db560d4 | 2017-11-20 16:42:49 -0600 | [diff] [blame] | 665 | header line which contains column names. |
| 666 | Column names may not contain spaces. |
| 667 | **args Arguments to be interpreted by |
| 668 | list_to_report. (See docstring of |
| 669 | list_to_report function for details). |
| 670 | """ |
| 671 | |
| 672 | report_list = filter(None, out_buf.split("\n")) |
| 673 | return list_to_report(report_list, **args) |