blob: 842bbeb4bd8a20dfc085352bbe0bfddee87b1938 [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
Matthew Barth1080b382017-02-17 15:50:43 -060040if __name__ == '__main__':
41 parser = ArgumentParser()
42 # Input yaml containing how each fan's presence detection should be done
43 parser.add_argument("-y", "--yaml", dest="pres_yaml",
44 default=
45 "example/fan-detect.yaml",
46 help=
47 "Input fan presences definition yaml file to parse")
48 args = parser.parse_args(sys.argv[1:])
49
50 # Verify given yaml file exists
51 yaml_file = os.path.abspath(args.pres_yaml)
52 if not os.path.isfile(yaml_file):
53 print "Unable to find input yaml file " + yaml_file
54 exit(1)
55
Matt Spinler14476ae2017-03-14 14:50:53 -050056 with open(yaml_file, 'r') as yaml_input:
57 presence_data = yaml.safe_load(yaml_input) or {}
58
Brad Bishop9c2994e2017-06-05 22:06:36 -040059 sys.stdout.write(Template(tmpl).render(presence=presence_data))