Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import re |
| 6 | import argparse |
| 7 | import yaml |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 8 | from mako.template import Template |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 9 | |
| 10 | valid_c_name_pattern = re.compile('[\W_]+') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 11 | |
| 12 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 13 | def 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 Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 20 | |
| 21 | if __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 Bishop | 789cf83 | 2016-10-22 00:47:54 -0400 | [diff] [blame] | 27 | default='generated.cpp', help='Output file name.') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 28 | parser.add_argument( |
| 29 | '-d', '--dir', dest='inputdir', |
Brad Bishop | 09fc256 | 2016-10-21 22:58:08 -0400 | [diff] [blame] | 30 | default=os.path.join('example', 'events'), |
| 31 | help='Location of files to process.') |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 32 | parser.add_argument( |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame^] | 33 | '-i', '--interfaces', dest='interfaces', |
| 34 | default=os.path.join('example', 'interfaces.yaml'), |
| 35 | help='Location of interface file.'), |
| 36 | parser.add_argument( |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 37 | '-t', '--templatedir', dest='template', |
| 38 | default='generated.mako.cpp', |
| 39 | help='Location of mako template.') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 40 | |
| 41 | args = parser.parse_args() |
| 42 | |
| 43 | yaml_files = filter( |
| 44 | lambda x: x.endswith('.yaml'), |
| 45 | os.listdir(args.inputdir)) |
| 46 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 47 | events = [] |
| 48 | for x in yaml_files: |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 49 | with open(os.path.join(args.inputdir, x), 'r') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 50 | for e in yaml.load(fd.read()).get('events', {}): |
| 51 | events.append(parse_event(e)) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 52 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 53 | t = Template(filename=args.template) |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame^] | 54 | with open(args.interfaces, 'r') as fd: |
| 55 | interfaces = yaml.load(fd.read()) |
| 56 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 57 | with open(args.output, 'w') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 58 | fd.write( |
| 59 | t.render( |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame^] | 60 | interfaces=interfaces, |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 61 | events=events)) |
| 62 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 63 | |
| 64 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |