1#!/usr/bin/env python
2
3import os
4import sys
5import yaml
6from argparse import ArgumentParser
7from mako.template import Template
8
9"""
10This script generates the data structures for the
11phosphor-fan-monitor application.
12
13A future improvement is to get the fan inventory names
14from a separate file, so just that file could be generated
15from the MRW.
16"""
17
18
19tmpl = '''/* This is a generated file. */
20#include "fan_defs.hpp"
21#include "types.hpp"
22
23using namespace phosphor::fan::monitor;
24
25const std::vector<FanDefinition> fanDefinitions
26{
27%for fan_data in data:
28    FanDefinition{"${fan_data['inventory']}",
29                  ${fan_data['allowed_out_of_range_time']},
30                  ${fan_data['deviation']},
31                  ${fan_data['num_sensors_nonfunc_for_fan_nonfunc']},
32                  std::vector<SensorDefinition>{
33                  %for sensor in fan_data['sensors']:
34                  <%
35                      #has_target is a bool, and we need a true instead of True
36                      has_target = str(sensor['has_target']).lower()
37                  %> \
38                      SensorDefinition{"${sensor['name']}", ${has_target}},
39                  %endfor
40                  },
41    },
42%endfor
43};
44'''
45
46
47if __name__ == '__main__':
48    parser = ArgumentParser(
49        description="Phosphor fan monitor definition parser")
50
51    parser.add_argument('-m', '--monitor_yaml', dest='monitor_yaml',
52                        default="example/monitor.yaml",
53                        help='fan monitor definitional yaml')
54    parser.add_argument('-o', '--output_dir', dest='output_dir',
55                        default=".",
56                        help='output directory')
57    args = parser.parse_args()
58
59    if not args.monitor_yaml:
60        parser.print_usage()
61        sys.exit(-1)
62
63    with open(args.monitor_yaml, 'r') as monitor_input:
64        monitor_data = yaml.safe_load(monitor_input) or {}
65
66    #Do some minor input validation
67    for fan in monitor_data:
68        if ((fan['deviation'] < 0) or (fan['deviation'] > 100)):
69            sys.exit("Invalid deviation value " + str(fan['deviation']))
70
71    output_file = os.path.join(args.output_dir, "fan_monitor_defs.cpp")
72    with open(output_file, 'w') as output:
73        output.write(Template(tmpl).render(data=monitor_data))
74