blob: 232092597b61c665d291cbe9c5b505e202adce2a [file] [log] [blame]
Brad Bishopbf066a62016-10-19 08:09:44 -04001#!/usr/bin/env python
2
3import sys
4import os
5import re
6import argparse
7import yaml
Brad Bishop92665b22016-10-26 20:51:16 -05008from mako.template import Template
Brad Bishopbf066a62016-10-19 08:09:44 -04009
10valid_c_name_pattern = re.compile('[\W_]+')
Brad Bishopbf066a62016-10-19 08:09:44 -040011
12
Brad Bishop92665b22016-10-26 20:51:16 -050013def parse_event(e):
14 e['name'] = valid_c_name_pattern.sub('_', e['name']).lower()
15 if e.get('filter') is None:
16 e.setdefault('filter', {}).setdefault('type', 'none')
17 if e.get('action') is None:
18 e.setdefault('action', {}).setdefault('type', 'noop')
19 return e
Brad Bishopbf066a62016-10-19 08:09:44 -040020
21if __name__ == '__main__':
22 parser = argparse.ArgumentParser(
23 description='Phosphor Inventory Manager (PIM) YAML '
24 'scanner and code generator.')
25 parser.add_argument(
26 '-o', '--output', dest='output',
Brad Bishop789cf832016-10-22 00:47:54 -040027 default='generated.cpp', help='Output file name.')
Brad Bishopbf066a62016-10-19 08:09:44 -040028 parser.add_argument(
29 '-d', '--dir', dest='inputdir',
Brad Bishopbbf13bd2016-11-09 22:29:09 -050030 default='example',
Brad Bishop09fc2562016-10-21 22:58:08 -040031 help='Location of files to process.')
Brad Bishop92665b22016-10-26 20:51:16 -050032 parser.add_argument(
Brad Bishopa56c5ff2016-11-09 22:21:18 -050033 '-t', '--template', dest='template',
Brad Bishop92665b22016-10-26 20:51:16 -050034 default='generated.mako.cpp',
35 help='Location of mako template.')
Brad Bishopbf066a62016-10-19 08:09:44 -040036
37 args = parser.parse_args()
38
Brad Bishopbbf13bd2016-11-09 22:29:09 -050039 events_dir = os.path.join(args.inputdir, 'events')
Brad Bishopbf066a62016-10-19 08:09:44 -040040 yaml_files = filter(
41 lambda x: x.endswith('.yaml'),
Brad Bishopbbf13bd2016-11-09 22:29:09 -050042 os.listdir(events_dir))
Brad Bishopbf066a62016-10-19 08:09:44 -040043
Brad Bishop92665b22016-10-26 20:51:16 -050044 events = []
45 for x in yaml_files:
Brad Bishopbbf13bd2016-11-09 22:29:09 -050046 with open(os.path.join(events_dir, x), 'r') as fd:
Brad Bishop92665b22016-10-26 20:51:16 -050047 for e in yaml.load(fd.read()).get('events', {}):
48 events.append(parse_event(e))
Brad Bishopbf066a62016-10-19 08:09:44 -040049
Brad Bishop92665b22016-10-26 20:51:16 -050050 t = Template(filename=args.template)
Brad Bishopbbf13bd2016-11-09 22:29:09 -050051 with open(os.path.join(args.inputdir, 'interfaces.yaml'), 'r') as fd:
Brad Bishop561a5652016-10-26 21:13:32 -050052 interfaces = yaml.load(fd.read())
53
Brad Bishopbf066a62016-10-19 08:09:44 -040054 with open(args.output, 'w') as fd:
Brad Bishop92665b22016-10-26 20:51:16 -050055 fd.write(
56 t.render(
Brad Bishop561a5652016-10-26 21:13:32 -050057 interfaces=interfaces,
Brad Bishop92665b22016-10-26 20:51:16 -050058 events=events))
59
Brad Bishopbf066a62016-10-19 08:09:44 -040060
61# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4