Skip to content

Commit 03e4647

Browse files
authored
[libc] implemented add_function to yaml hdrgen (#97079)
users can now input a new function in the CLI: the yaml, and .h will be updated instantly tested on different cases and does not change the yaml format in anyway except decreasing extra spaces if present
1 parent 78804f8 commit 03e4647

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

libc/newhdrgen/yaml_to_classes.py

+92-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
import yaml
13-
import re
1413
import argparse
1514

1615
from pathlib import Path
@@ -54,10 +53,10 @@ def yaml_to_classes(yaml_data):
5453
standards = (function_data.get("standards", None),)
5554
header.add_function(
5655
Function(
57-
standards,
5856
function_data["return_type"],
5957
function_data["name"],
6058
arguments,
59+
standards,
6160
guard,
6261
attributes,
6362
)
@@ -103,16 +102,92 @@ def fill_public_api(header_str, h_def_content):
103102
return h_def_content.replace("%%public_api()", header_str, 1)
104103

105104

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):
107178
"""
108179
Main function to generate header files from YAML and .h.def templates.
109180
110181
Args:
111182
yaml_file: Path to the YAML file containing header specification.
112183
h_def_file: Path to the .h.def template file.
113184
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).
114186
"""
115187

188+
if add_function:
189+
add_function_to_yaml(yaml_file, add_function)
190+
116191
header = load_yaml_file(yaml_file)
117192

118193
with open(h_def_file, "r") as f:
@@ -143,6 +218,19 @@ def main(yaml_file, h_def_file, output_dir):
143218
default=".",
144219
help="Directory to output the generated header file",
145220
)
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+
)
146234
args = parser.parse_args()
147235

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

Comments
 (0)