|
10 | 10 |
|
11 | 11 |
|
12 | 12 | import yaml
|
13 |
| -import re |
14 | 13 | import argparse
|
15 | 14 |
|
16 | 15 | from pathlib import Path
|
@@ -54,10 +53,10 @@ def yaml_to_classes(yaml_data):
|
54 | 53 | standards = (function_data.get("standards", None),)
|
55 | 54 | header.add_function(
|
56 | 55 | Function(
|
57 |
| - standards, |
58 | 56 | function_data["return_type"],
|
59 | 57 | function_data["name"],
|
60 | 58 | arguments,
|
| 59 | + standards, |
61 | 60 | guard,
|
62 | 61 | attributes,
|
63 | 62 | )
|
@@ -103,16 +102,92 @@ def fill_public_api(header_str, h_def_content):
|
103 | 102 | return h_def_content.replace("%%public_api()", header_str, 1)
|
104 | 103 |
|
105 | 104 |
|
106 |
| -def main(yaml_file, h_def_file, output_dir): |
| 105 | +def parse_function_details(details): |
| 106 | + """ |
| 107 | + Parse function details from a list of strings and return a Function object. |
| 108 | +
|
| 109 | + Args: |
| 110 | + details: A list containing function details |
| 111 | +
|
| 112 | + Returns: |
| 113 | + Function: An instance of Function initialized with the details. |
| 114 | + """ |
| 115 | + return_type, name, arguments, standards, guard, attributes = details |
| 116 | + standards = standards.split(",") if standards != "null" else [] |
| 117 | + arguments = [arg.strip() for arg in arguments.split(",")] |
| 118 | + attributes = attributes.split(",") if attributes != "null" else [] |
| 119 | + |
| 120 | + return Function( |
| 121 | + return_type=return_type, |
| 122 | + name=name, |
| 123 | + arguments=arguments, |
| 124 | + standards=standards, |
| 125 | + guard=guard if guard != "null" else None, |
| 126 | + attributes=attributes if attributes else [], |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +def add_function_to_yaml(yaml_file, function_details): |
| 131 | + """ |
| 132 | + Add a function to the YAML file. |
| 133 | +
|
| 134 | + Args: |
| 135 | + yaml_file: The path to the YAML file. |
| 136 | + function_details: A list containing function details (return_type, name, arguments, standards, guard, attributes). |
| 137 | + """ |
| 138 | + new_function = parse_function_details(function_details) |
| 139 | + |
| 140 | + with open(yaml_file, "r") as f: |
| 141 | + yaml_data = yaml.safe_load(f) |
| 142 | + |
| 143 | + if "functions" not in yaml_data: |
| 144 | + yaml_data["functions"] = [] |
| 145 | + |
| 146 | + function_dict = { |
| 147 | + "name": new_function.name, |
| 148 | + "standards": new_function.standards, |
| 149 | + "return_type": new_function.return_type, |
| 150 | + "arguments": [{"type": arg} for arg in new_function.arguments], |
| 151 | + } |
| 152 | + |
| 153 | + if new_function.guard: |
| 154 | + function_dict["guard"] = new_function.guard |
| 155 | + |
| 156 | + if new_function.attributes: |
| 157 | + function_dict["attributes"] = new_function.attributes |
| 158 | + |
| 159 | + yaml_data["functions"].append(function_dict) |
| 160 | + |
| 161 | + class IndentYamlListDumper(yaml.Dumper): |
| 162 | + def increase_indent(self, flow=False, indentless=False): |
| 163 | + return super(IndentYamlListDumper, self).increase_indent(flow, False) |
| 164 | + |
| 165 | + with open(yaml_file, "w") as f: |
| 166 | + yaml.dump( |
| 167 | + yaml_data, |
| 168 | + f, |
| 169 | + Dumper=IndentYamlListDumper, |
| 170 | + default_flow_style=False, |
| 171 | + sort_keys=False, |
| 172 | + ) |
| 173 | + |
| 174 | + print(f"Added function {new_function.name} to {yaml_file}") |
| 175 | + |
| 176 | + |
| 177 | +def main(yaml_file, h_def_file, output_dir, add_function=None): |
107 | 178 | """
|
108 | 179 | Main function to generate header files from YAML and .h.def templates.
|
109 | 180 |
|
110 | 181 | Args:
|
111 | 182 | yaml_file: Path to the YAML file containing header specification.
|
112 | 183 | h_def_file: Path to the .h.def template file.
|
113 | 184 | output_dir: Directory to output the generated header file.
|
| 185 | + add_function: Details of the function to be added to the YAML file (if any). |
114 | 186 | """
|
115 | 187 |
|
| 188 | + if add_function: |
| 189 | + add_function_to_yaml(yaml_file, add_function) |
| 190 | + |
116 | 191 | header = load_yaml_file(yaml_file)
|
117 | 192 |
|
118 | 193 | with open(h_def_file, "r") as f:
|
@@ -143,6 +218,19 @@ def main(yaml_file, h_def_file, output_dir):
|
143 | 218 | default=".",
|
144 | 219 | help="Directory to output the generated header file",
|
145 | 220 | )
|
| 221 | + parser.add_argument( |
| 222 | + "--add_function", |
| 223 | + nargs=6, |
| 224 | + metavar=( |
| 225 | + "RETURN_TYPE", |
| 226 | + "NAME", |
| 227 | + "ARGUMENTS", |
| 228 | + "STANDARDS", |
| 229 | + "GUARD", |
| 230 | + "ATTRIBUTES", |
| 231 | + ), |
| 232 | + help="Add a function to the YAML file", |
| 233 | + ) |
146 | 234 | args = parser.parse_args()
|
147 | 235 |
|
148 |
| - main(args.yaml_file, args.h_def_file, args.output_dir) |
| 236 | + main(args.yaml_file, args.h_def_file, args.output_dir, args.add_function) |
0 commit comments