[libc] Use idiomatic main() function in newhdrgen/yaml_to_classes.py#113419
Merged
[libc] Use idiomatic main() function in newhdrgen/yaml_to_classes.py#113419
Conversation
This changes the entry-point Python script of newhdrgen to use the idiomatic main() function of no arguments that does the whole job of the script. This replaces the unusual pattern of having an idiosyncratic main(...) signature and having the script's direct code do argument parsing outside the main function. The idiomatic pattern makes it possible to usefully wrap the script in Python and still use its full command-line functionality.
Member
|
@llvm/pr-subscribers-libc Author: Roland McGrath (frobtech) ChangesThis changes the entry-point Python script of newhdrgen to use Full diff: https://github.com/llvm/llvm-project/pull/113419.diff 1 Files Affected:
diff --git a/libc/newhdrgen/yaml_to_classes.py b/libc/newhdrgen/yaml_to_classes.py
index 237dd21aa5dff5..a295058f7dc821 100644
--- a/libc/newhdrgen/yaml_to_classes.py
+++ b/libc/newhdrgen/yaml_to_classes.py
@@ -216,52 +216,7 @@ def increase_indent(self, flow=False, indentless=False):
print(f"Added function {new_function.name} to {yaml_file}")
-def main(
- yaml_file,
- output_dir=None,
- h_def_file=None,
- add_function=None,
- entry_points=None,
- export_decls=False,
-):
- """
- Main function to generate header files from YAML and .h.def templates.
-
- Args:
- yaml_file: Path to the YAML file containing header specification.
- h_def_file: Path to the .h.def template file.
- output_dir: Directory to output the generated header file.
- add_function: Details of the function to be added to the YAML file (if any).
- entry_points: A list of specific function names to include in the header.
- export_decls: Flag to use GpuHeader for exporting declarations.
- """
- if add_function:
- add_function_to_yaml(yaml_file, add_function)
-
- header_class = GpuHeader if export_decls else HeaderFile
- header = load_yaml_file(yaml_file, header_class, entry_points)
-
- header_str = str(header)
-
- if output_dir:
- output_file_path = Path(output_dir)
- if output_file_path.is_dir():
- output_file_path /= f"{Path(yaml_file).stem}.h"
- else:
- output_file_path = Path(f"{Path(yaml_file).stem}.h")
-
- if not export_decls and h_def_file:
- with open(h_def_file, "r") as f:
- h_def_content = f.read()
- final_header_content = fill_public_api(header_str, h_def_content)
- with open(output_file_path, "w") as f:
- f.write(final_header_content)
- else:
- with open(output_file_path, "w") as f:
- f.write(header_str)
-
-
-if __name__ == "__main__":
+def main():
parser = argparse.ArgumentParser(description="Generate header files from YAML")
parser.add_argument(
"yaml_file", help="Path to the YAML file containing header specification"
@@ -297,11 +252,31 @@ def main(
)
args = parser.parse_args()
- main(
- args.yaml_file,
- args.output_dir,
- args.h_def_file,
- args.add_function,
- args.entry_points,
- args.export_decls,
- )
+ if args.add_function:
+ add_function_to_yaml(yaml_file, args.add_function)
+
+ header_class = GpuHeader if args.export_decls else HeaderFile
+ header = load_yaml_file(args.yaml_file, header_class, args.entry_points)
+
+ header_str = str(header)
+
+ if args.output_dir:
+ output_file_path = Path(args.output_dir)
+ if output_file_path.is_dir():
+ output_file_path /= f"{Path(args.yaml_file).stem}.h"
+ else:
+ output_file_path = Path(f"{Path(args.yaml_file).stem}.h")
+
+ if not args.export_decls and args.h_def_file:
+ with open(args.h_def_file, "r") as f:
+ h_def_content = f.read()
+ final_header_content = fill_public_api(header_str, h_def_content)
+ with open(output_file_path, "w") as f:
+ f.write(final_header_content)
+ else:
+ with open(output_file_path, "w") as f:
+ f.write(header_str)
+
+
+if __name__ == "__main__":
+ main()
|
fabio-d
approved these changes
Oct 24, 2024
Closed
NoumanAmir657
pushed a commit
to NoumanAmir657/llvm-project
that referenced
this pull request
Nov 4, 2024
…lvm#113419) This changes the entry-point Python script of newhdrgen to use the idiomatic main() function of no arguments that does the whole job of the script. This replaces the unusual pattern of having an idiosyncratic main(...) signature and having the script's direct code do argument parsing outside the main function. The idiomatic pattern makes it possible to usefully wrap the script in Python and still use its full command-line functionality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This changes the entry-point Python script of newhdrgen to use
the idiomatic main() function of no arguments that does the whole
job of the script. This replaces the unusual pattern of having
an idiosyncratic main(...) signature and having the script's
direct code do argument parsing outside the main function. The
idiomatic pattern makes it possible to usefully wrap the script
in Python and still use its full command-line functionality.