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__': |
Brad Bishop | 79d8f7a | 2016-11-09 22:40:57 -0500 | [diff] [blame] | 22 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 23 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 24 | parser = argparse.ArgumentParser( |
| 25 | description='Phosphor Inventory Manager (PIM) YAML ' |
| 26 | 'scanner and code generator.') |
| 27 | parser.add_argument( |
Brad Bishop | 4d16b69 | 2016-11-12 11:45:35 -0500 | [diff] [blame^] | 28 | '-o', '--output-dir', dest='outputdir', |
| 29 | default='.', help='Output directory.') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 30 | parser.add_argument( |
| 31 | '-d', '--dir', dest='inputdir', |
Brad Bishop | f30db4c | 2016-11-09 22:47:05 -0500 | [diff] [blame] | 32 | default=os.path.join(script_dir, 'example'), |
Brad Bishop | 09fc256 | 2016-10-21 22:58:08 -0400 | [diff] [blame] | 33 | help='Location of files to process.') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 34 | |
| 35 | args = parser.parse_args() |
| 36 | |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 37 | events_dir = os.path.join(args.inputdir, 'events') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 38 | yaml_files = filter( |
| 39 | lambda x: x.endswith('.yaml'), |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 40 | os.listdir(events_dir)) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 41 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 42 | events = [] |
| 43 | for x in yaml_files: |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 44 | with open(os.path.join(events_dir, x), 'r') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 45 | for e in yaml.load(fd.read()).get('events', {}): |
| 46 | events.append(parse_event(e)) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 47 | |
Brad Bishop | 79d8f7a | 2016-11-09 22:40:57 -0500 | [diff] [blame] | 48 | template = os.path.join(script_dir, 'generated.mako.cpp') |
| 49 | t = Template(filename=template) |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 50 | with open(os.path.join(args.inputdir, 'interfaces.yaml'), 'r') as fd: |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 51 | interfaces = yaml.load(fd.read()) |
| 52 | |
Brad Bishop | 4d16b69 | 2016-11-12 11:45:35 -0500 | [diff] [blame^] | 53 | with open(os.path.join(args.outputdir, 'generated.cpp'), 'w') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 54 | fd.write( |
| 55 | t.render( |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 56 | interfaces=interfaces, |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 57 | events=events)) |
| 58 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 59 | |
| 60 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |