blob: 7c325943770dc2d14269b21a040d9d86d2390a0b [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 Bishop09fc2562016-10-21 22:58:08 -040030 default=os.path.join('example', 'events'),
31 help='Location of files to process.')
Brad Bishop92665b22016-10-26 20:51:16 -050032 parser.add_argument(
33 '-t', '--templatedir', dest='template',
34 default='generated.mako.cpp',
35 help='Location of mako template.')
Brad Bishopbf066a62016-10-19 08:09:44 -040036
37 args = parser.parse_args()
38
39 yaml_files = filter(
40 lambda x: x.endswith('.yaml'),
41 os.listdir(args.inputdir))
42
Brad Bishop92665b22016-10-26 20:51:16 -050043 events = []
44 for x in yaml_files:
Brad Bishopbf066a62016-10-19 08:09:44 -040045 with open(os.path.join(args.inputdir, x), 'r') as fd:
Brad Bishop92665b22016-10-26 20:51:16 -050046 for e in yaml.load(fd.read()).get('events', {}):
47 events.append(parse_event(e))
Brad Bishopbf066a62016-10-19 08:09:44 -040048
Brad Bishop92665b22016-10-26 20:51:16 -050049 t = Template(filename=args.template)
Brad Bishopbf066a62016-10-19 08:09:44 -040050 with open(args.output, 'w') as fd:
Brad Bishop92665b22016-10-26 20:51:16 -050051 fd.write(
52 t.render(
53 events=events))
54
Brad Bishopbf066a62016-10-19 08:09:44 -040055
56# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4