xref: /openbmc/phosphor-logging/tools/gen-eventfilter.py (revision 76fe0d1fd40349c1b84d11c311ed8909f45ff193)
1#!/usr/bin/env python3
2
3import argparse
4import json
5import os
6import sys
7
8import jsonschema
9from mako.template import Template
10
11import yaml
12
13# Determine the script's directory to find the schema file relative to it.
14SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
15SCHEMA_FILE = os.path.join(
16    SCRIPT_DIR, "phosphor-logging", "schemas", "eventfilter.schema.yaml"
17)
18TEMPLATE_FILE = os.path.join(
19    SCRIPT_DIR, "phosphor-logging", "templates", "event-filter.cpp.mako"
20)
21
22
23def main() -> int:
24    """
25    Validates a JSON filter file against the eventfilter schema.
26    """
27    parser = argparse.ArgumentParser(
28        description="Validate an event filter JSON file against the schema."
29    )
30    parser.add_argument(
31        "filter",
32        type=str,
33        help="Path to the JSON filter file to validate.",
34    )
35    args = parser.parse_args()
36
37    with open(args.filter, "r") as f:
38        filter_data = json.load(f)
39
40    with open(SCHEMA_FILE, "r") as f:
41        schema_data = yaml.safe_load(f)
42
43    jsonschema.validate(instance=filter_data, schema=schema_data)
44
45    template = Template(filename=TEMPLATE_FILE)
46    output = template.render(data=filter_data)
47    print(output)
48
49    return 0
50
51
52if __name__ == "__main__":
53    sys.exit(main())
54