mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-28 00:21:48 +03:00
refactored generate_driver_wrappers.py
Signed-off-by: Asfandyar Orakzai <asfandyar.orakzai@silabs.com>
This commit is contained in:
@ -23,7 +23,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from typing import Tuple, NewType
|
from typing import Tuple, NewType, Dict, Any
|
||||||
import argparse
|
import argparse
|
||||||
import jsonschema
|
import jsonschema
|
||||||
import jinja2
|
import jinja2
|
||||||
@ -34,6 +34,13 @@ JSONSchema = NewType('JSONSchema', object)
|
|||||||
# keep MyPy happy till MyPy comes with a more composite type for JsonObjects.
|
# keep MyPy happy till MyPy comes with a more composite type for JsonObjects.
|
||||||
Driver = NewType('Driver', dict)
|
Driver = NewType('Driver', dict)
|
||||||
|
|
||||||
|
|
||||||
|
class JsonValidationException(Exception):
|
||||||
|
def __init__(self, message="Json Validation Failed"):
|
||||||
|
self.message = message
|
||||||
|
super().__init__(self.message)
|
||||||
|
|
||||||
|
|
||||||
def render(template_path: str, driver_jsoncontext: list) -> str:
|
def render(template_path: str, driver_jsoncontext: list) -> str:
|
||||||
"""
|
"""
|
||||||
Render template from the input file and driver JSON.
|
Render template from the input file and driver JSON.
|
||||||
@ -93,6 +100,15 @@ def validate_json(driverjson_data: Driver, driverschema_list: dict) -> bool:
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def load_driver(schemas: Dict[str, Any], driver_file: str) -> Any:
|
||||||
|
with open(driver_file, 'r') as f:
|
||||||
|
json_data = json.load(f)
|
||||||
|
if not validate_json(json_data, schemas):
|
||||||
|
raise JsonValidationException()
|
||||||
|
return json_data
|
||||||
|
|
||||||
|
|
||||||
def read_driver_descriptions(mbedtls_root: str, json_directory: str, \
|
def read_driver_descriptions(mbedtls_root: str, json_directory: str, \
|
||||||
jsondriver_list: str) -> Tuple[bool, list]:
|
jsondriver_list: str) -> Tuple[bool, list]:
|
||||||
"""
|
"""
|
||||||
@ -112,18 +128,17 @@ def read_driver_descriptions(mbedtls_root: str, json_directory: str, \
|
|||||||
'driver_opaque_schema.json'), 'r') as file:
|
'driver_opaque_schema.json'), 'r') as file:
|
||||||
opaque_driver_schema = json.load(file)
|
opaque_driver_schema = json.load(file)
|
||||||
|
|
||||||
driver_schema_list = {'transparent':transparent_driver_schema,
|
driver_schema = {'transparent': transparent_driver_schema,
|
||||||
'opaque':opaque_driver_schema}
|
'opaque': opaque_driver_schema}
|
||||||
|
|
||||||
with open(os.path.join(json_directory, jsondriver_list), 'r') as driverlistfile:
|
with open(os.path.join(json_directory, jsondriver_list), 'r') as driverlistfile:
|
||||||
driverlist = json.load(driverlistfile)
|
driver_list = json.load(driverlistfile)
|
||||||
for file_name in driverlist:
|
|
||||||
with open(os.path.join(json_directory, file_name), 'r') as infile:
|
try:
|
||||||
json_data = json.load(infile)
|
result = [load_driver(driver_schema, driver_file=os.path.join(json_directory, driver_file_name))
|
||||||
ret = validate_json(json_data, driver_schema_list)
|
for driver_file_name in driver_list]
|
||||||
if ret is False:
|
except JsonValidationException as _:
|
||||||
return ret, []
|
return False, []
|
||||||
result.append(json_data)
|
|
||||||
return True, result
|
return True, result
|
||||||
|
|
||||||
|
|
||||||
@ -172,5 +187,6 @@ def main() -> int:
|
|||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
Reference in New Issue
Block a user