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( |
| 33 | '-t', '--templatedir', dest='template', |
| 34 | default='generated.mako.cpp', |
| 35 | help='Location of mako template.') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 36 | |
| 37 | args = parser.parse_args() |
| 38 | |
| 39 | yaml_files = filter( |
| 40 | lambda x: x.endswith('.yaml'), |
| 41 | os.listdir(args.inputdir)) |
| 42 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 43 | events = [] |
| 44 | for x in yaml_files: |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 45 | with open(os.path.join(args.inputdir, x), 'r') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 46 | for e in yaml.load(fd.read()).get('events', {}): |
| 47 | events.append(parse_event(e)) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 48 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 49 | t = Template(filename=args.template) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 50 | with open(args.output, 'w') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 51 | fd.write( |
| 52 | t.render( |
| 53 | events=events)) |
| 54 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 55 | |
| 56 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |