blob: 1bc980a64c0dd46bcab633da13ca342cc6f70c84 [file] [log] [blame]
Matthew Barth1080b382017-02-17 15:50:43 -06001#!/usr/bin/env python
2
3"""
4This script parses the given fan presence definition yaml file and generates
5a header file based on the defined methods for determining when a fan is
6present.
7"""
8
9import os
10import sys
11import yaml
12from argparse import ArgumentParser
Matt Spinler14476ae2017-03-14 14:50:53 -050013from mako.template import Template
14
15tmpl = '''/* This is a generated file. */
16#include "fan_detect_defs.hpp"
17
18const std::map<std::string, std::set<phosphor::fan::Properties>>
19fanDetectMap = {
20%for methods in presence:
21 %for method in methods:
22 <% m = method.lower() %> \
23 {"${m}", {
24 %for fan in methods[method]:
25 std::make_tuple("${fan['Inventory']}",
Matt Spinler2b44a6c2017-03-27 11:02:46 -050026 "${fan['PrettyName']}",
Matt Spinler14476ae2017-03-14 14:50:53 -050027 std::vector<std::string>{
28 %for s in fan['Sensors']:
29 "${s}",
30 %endfor
31 }),
32 %endfor
33 %endfor
34 }},
35%endfor
36};
37'''
Matthew Barth1080b382017-02-17 15:50:43 -060038
39
40def get_filename():
41 """
42 Constructs and returns the fully qualified header filename.
43 """
44 script_dir = os.path.dirname(os.path.abspath(__file__))
45 header_file = os.path.join(script_dir, "fan_detect_defs.cpp")
46
47 return header_file
48
49
Matthew Barth1080b382017-02-17 15:50:43 -060050if __name__ == '__main__':
51 parser = ArgumentParser()
52 # Input yaml containing how each fan's presence detection should be done
53 parser.add_argument("-y", "--yaml", dest="pres_yaml",
54 default=
55 "example/fan-detect.yaml",
56 help=
57 "Input fan presences definition yaml file to parse")
58 args = parser.parse_args(sys.argv[1:])
59
60 # Verify given yaml file exists
61 yaml_file = os.path.abspath(args.pres_yaml)
62 if not os.path.isfile(yaml_file):
63 print "Unable to find input yaml file " + yaml_file
64 exit(1)
65
Matt Spinler14476ae2017-03-14 14:50:53 -050066 with open(yaml_file, 'r') as yaml_input:
67 presence_data = yaml.safe_load(yaml_input) or {}
68
69 output_file = get_filename()
70
71 with open(output_file, 'w') as out:
72 out.write(Template(tmpl).render(presence=presence_data))