blob: 91d93d62bd13bb39746428780951131b9824362c [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__':
Brad Bishop79d8f7a2016-11-09 22:40:57 -050022 script_dir = os.path.dirname(os.path.realpath(__file__))
23
Brad Bishopbf066a62016-10-19 08:09:44 -040024 parser = argparse.ArgumentParser(
25 description='Phosphor Inventory Manager (PIM) YAML '
26 'scanner and code generator.')
27 parser.add_argument(
Brad Bishop4d16b692016-11-12 11:45:35 -050028 '-o', '--output-dir', dest='outputdir',
29 default='.', help='Output directory.')
Brad Bishopbf066a62016-10-19 08:09:44 -040030 parser.add_argument(
31 '-d', '--dir', dest='inputdir',
Brad Bishopf30db4c2016-11-09 22:47:05 -050032 default=os.path.join(script_dir, 'example'),
Brad Bishop09fc2562016-10-21 22:58:08 -040033 help='Location of files to process.')
Brad Bishopbf066a62016-10-19 08:09:44 -040034
35 args = parser.parse_args()
36
Brad Bishopbbf13bd2016-11-09 22:29:09 -050037 events_dir = os.path.join(args.inputdir, 'events')
Brad Bishopbf066a62016-10-19 08:09:44 -040038 yaml_files = filter(
39 lambda x: x.endswith('.yaml'),
Brad Bishopbbf13bd2016-11-09 22:29:09 -050040 os.listdir(events_dir))
Brad Bishopbf066a62016-10-19 08:09:44 -040041
Brad Bishop92665b22016-10-26 20:51:16 -050042 events = []
43 for x in yaml_files:
Brad Bishopbbf13bd2016-11-09 22:29:09 -050044 with open(os.path.join(events_dir, x), 'r') as fd:
Brad Bishop92665b22016-10-26 20:51:16 -050045 for e in yaml.load(fd.read()).get('events', {}):
46 events.append(parse_event(e))
Brad Bishopbf066a62016-10-19 08:09:44 -040047
Brad Bishop79d8f7a2016-11-09 22:40:57 -050048 template = os.path.join(script_dir, 'generated.mako.cpp')
49 t = Template(filename=template)
Brad Bishopbbf13bd2016-11-09 22:29:09 -050050 with open(os.path.join(args.inputdir, 'interfaces.yaml'), 'r') as fd:
Brad Bishop561a5652016-10-26 21:13:32 -050051 interfaces = yaml.load(fd.read())
52
Brad Bishop4d16b692016-11-12 11:45:35 -050053 with open(os.path.join(args.outputdir, 'generated.cpp'), 'w') as fd:
Brad Bishop92665b22016-10-26 20:51:16 -050054 fd.write(
55 t.render(
Brad Bishop561a5652016-10-26 21:13:32 -050056 interfaces=interfaces,
Brad Bishop92665b22016-10-26 20:51:16 -050057 events=events))
58
Brad Bishopbf066a62016-10-19 08:09:44 -040059
60# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4