1#!/usr/bin/env python
2
3r"""
4PLDM functions.
5"""
6
7import re
8import var_funcs as vf
9import func_args as fa
10import bmc_ssh_utils as bsu
11
12
13def pldmtool(option_string, parse_results=1, **bsu_options):
14    r"""
15    Run pldmtool on the BMC with the caller's option string and return the result.
16
17    Example:
18
19    ${pldm_results}=  Pldmtool  base GetPLDMTypes
20    Rprint Vars  pldm_results
21
22    pldm_results:
23      [supported_types]:
24        [raw]:
25          [0]:                                        0
26          [1]:                                        2
27          [2]:                                        3
28        [text]:
29          [0]:                                        base
30          [1]:                                        platform
31          [2]:                                        bios
32
33    Description of argument(s):
34    option_string                   A string of options which are to be processed by the pldmtool command.
35    parse_results                   Parse the pldmtool results and return a dictionary rather than the raw
36                                    pldmtool output.
37    bsu_options                     Options to be passed directly to bmc_execute_command.  See its prolog for
38                                    details.
39    """
40
41    # This allows callers to specify arguments in python style (e.g. print_out=1 vs. print_out=${1}).
42    bsu_options = fa.args_to_objects(bsu_options)
43
44    stdout, stderr, rc = bsu.bmc_execute_command('pldmtool ' + option_string, **bsu_options)
45
46    if parse_results:
47        result = vf.key_value_outbuf_to_dict(stdout)
48        if 'supported_types' in result:
49            # 'supported types' begins like this:
50            # 0(base) 2(platform) 3(bios)
51            # Parsing it to look like it does in the example above.
52            supported_types = {'raw': [], 'text': []}
53            for entry in result['supported_types'].split(" "):
54                record = entry.split("(")
55                supported_types['raw'].append(record[0])
56                supported_types['text'].append(record[1].rstrip(")"))
57            result['supported_types'] = supported_types
58
59        elif 'supported_commands' in result:
60            commands = result['supported_commands'].split(":")[0].split(" ")
61            return commands
62
63        elif 'yyyy-mm-dd_hh' in result:
64            # Date & Time :
65            # YYYY-MM-DD HH:MM:SS - 2020-02-24 06:44:16
66            return result['yyyy-mm-dd_hh'].split(' - ')[1]
67        return result
68
69    return stdout
70