summaryrefslogtreecommitdiffstats
path: root/scripts/entity_gen.py
blob: 057821b28431f8323d2758ef81cd0048b0e7ce10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python

import os
import sys
import yaml
import argparse
from mako.template import Template


def generate_cpp(entity_yaml, output_dir):
    with open(os.path.join(script_dir, entity_yaml), 'r') as f:
        ifile = yaml.safe_load(f)
        if not isinstance(ifile, dict):
            ifile = {}

        # Render the mako template

        t = Template(filename=os.path.join(
                     script_dir,
                     "writeentity.mako.cpp"))

        output_cpp = os.path.join(output_dir, "entity-gen.cpp")
        with open(output_cpp, 'w') as fd:
            fd.write(t.render(entityDict=ifile))


def main():

    valid_commands = {
        'generate-cpp': generate_cpp
    }
    parser = argparse.ArgumentParser(
        description="IPMI Entity record parser and code generator")

    parser.add_argument(
        '-i', '--entity_yaml', dest='entity_yaml',
        default='example.yaml', help='input entity yaml file to parse')

    parser.add_argument(
        "-o", "--output-dir", dest="outputdir",
        default=".",
        help="output directory")

    parser.add_argument(
        'command', metavar='COMMAND', type=str,
        choices=valid_commands.keys(),
        help='Command to run.')

    args = parser.parse_args()

    if (not (os.path.isfile(os.path.join(script_dir, args.entity_yaml)))):
        sys.exit("Can not find input yaml file " + args.entity_yaml)

    function = valid_commands[args.command]
    function(args.entity_yaml, args.outputdir)


if __name__ == '__main__':
    script_dir = os.path.dirname(os.path.realpath(__file__))
    main()
OpenPOWER on IntegriCloud