Update codegen to support accepting JSON file as input
This commit is contained in:
@@ -1,23 +1,60 @@
|
||||
import json
|
||||
from typing import Dict
|
||||
from codegen.datatypes import CDataType
|
||||
from pathlib import Path
|
||||
from codegen.datatypes import CDataType, CStruct
|
||||
from codegen.constants import WAPP_REPO_ROOT, DBL_LIST_DATA, ARRAY_DATA
|
||||
from codegen.dbl_list.make_dbl_list import DblListData, make_dbl_list
|
||||
from codegen.array.make_array import ArrayData, make_array
|
||||
|
||||
|
||||
def main():
|
||||
gen_dbl_list()
|
||||
gen_array()
|
||||
def main(types_file: Path | None):
|
||||
dbl_list_datatypes: Dict[CDataType, DblListData] = {}
|
||||
array_datatypes: Dict[CDataType, ArrayData] = {}
|
||||
|
||||
if types_file is not None:
|
||||
with types_file.open("r") as infile:
|
||||
datatypes = json.load(infile)
|
||||
dbl_list_data = datatypes.get(DBL_LIST_DATA)
|
||||
array_data = datatypes.get(ARRAY_DATA)
|
||||
|
||||
def gen_dbl_list():
|
||||
datatypes: Dict[CDataType, DblListData] = {}
|
||||
make_dbl_list(datatypes)
|
||||
if dbl_list_data is not None and isinstance(dbl_list_data, dict):
|
||||
dbl_list_datatypes = {k: DblListData.from_dict(v) for k, v in dbl_list_data.items()}
|
||||
|
||||
if array_data is not None and isinstance(array_data, dict):
|
||||
array_datatypes = {k: ArrayData.from_dict(v) for k, v in array_data.items()}
|
||||
|
||||
def gen_array():
|
||||
datatypes: Dict[CDataType, ArrayData] = {}
|
||||
make_array(datatypes)
|
||||
make_dbl_list(dbl_list_datatypes)
|
||||
make_array(array_datatypes)
|
||||
|
||||
# Save example types file
|
||||
custom_struct = CStruct(name="custom_type", cargs=[], typedef_name="CustomType")
|
||||
example = {
|
||||
DBL_LIST_DATA: {
|
||||
"CustomType": DblListData(
|
||||
node_typename="CustomTypeNode",
|
||||
list_typename="CustomTypeList",
|
||||
hdr_decl_types=[custom_struct],
|
||||
).to_dict()
|
||||
},
|
||||
ARRAY_DATA: {
|
||||
"CustomType": ArrayData(
|
||||
array_typename="CustomTypeArray",
|
||||
hdr_decl_types=[custom_struct],
|
||||
).to_dict()
|
||||
},
|
||||
}
|
||||
|
||||
example_file = WAPP_REPO_ROOT / "codegen_custom_data_example.json"
|
||||
with example_file.open("w") as outfile:
|
||||
json.dump(example, outfile, indent=2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
from argparse import ArgumentParser
|
||||
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("-f", "--types-file", type=Path, help="JSON file containing custom types for codegen")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
main(args.types_file)
|
||||
|
Reference in New Issue
Block a user