blob: 306cc49b714461ea90ec5a3aa16163acc6a483c2 [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(
Brad Bishop561a5652016-10-26 21:13:32 -050033 '-i', '--interfaces', dest='interfaces',
34 default=os.path.join('example', 'interfaces.yaml'),
35 help='Location of interface file.'),
36 parser.add_argument(
Brad Bishop92665b22016-10-26 20:51:16 -050037 '-t', '--templatedir', dest='template',
38 default='generated.mako.cpp',
39 help='Location of mako template.')
Brad Bishopbf066a62016-10-19 08:09:44 -040040
41 args = parser.parse_args()
42
43 yaml_files = filter(
44 lambda x: x.endswith('.yaml'),
45 os.listdir(args.inputdir))
46
Brad Bishop92665b22016-10-26 20:51:16 -050047 events = []
48 for x in yaml_files:
Brad Bishopbf066a62016-10-19 08:09:44 -040049 with open(os.path.join(args.inputdir, x), 'r') as fd:
Brad Bishop92665b22016-10-26 20:51:16 -050050 for e in yaml.load(fd.read()).get('events', {}):
51 events.append(parse_event(e))
Brad Bishopbf066a62016-10-19 08:09:44 -040052
Brad Bishop92665b22016-10-26 20:51:16 -050053 t = Template(filename=args.template)
Brad Bishop561a5652016-10-26 21:13:32 -050054 with open(args.interfaces, 'r') as fd:
55 interfaces = yaml.load(fd.read())
56
Brad Bishopbf066a62016-10-19 08:09:44 -040057 with open(args.output, 'w') as fd:
Brad Bishop92665b22016-10-26 20:51:16 -050058 fd.write(
59 t.render(
Brad Bishop561a5652016-10-26 21:13:32 -050060 interfaces=interfaces,
Brad Bishop92665b22016-10-26 20:51:16 -050061 events=events))
62
Brad Bishopbf066a62016-10-19 08:09:44 -040063
64# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4