Skip to content

Commit 01c3364

Browse files
author
XuhuaHuang
committed
Add docstring and type hints for script
1 parent c0b20b9 commit 01c3364

1 file changed

Lines changed: 54 additions & 8 deletions

File tree

Scripts/clang_format_cli.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""
22
Batch format C/C++ files using clang-format.
3-
This script searches for C/C++ source files in the specified directories and formats them using clang-format.
43
"""
54

65
import argparse
@@ -9,6 +8,15 @@
98

109

1110
def is_in_site_packages(path: pathlib.Path) -> bool:
11+
"""
12+
Check if the given path is in a site-packages directory.
13+
14+
Args:
15+
path (pathlib.Path): The path to check for site-packages.
16+
17+
Returns:
18+
bool: True if the path is in a site-packages directory, False otherwise.
19+
"""
1220
# Normalize path parts for cross-platform matching
1321
lower_parts = [part.lower() for part in path.parts]
1422
return (
@@ -17,7 +25,16 @@ def is_in_site_packages(path: pathlib.Path) -> bool:
1725
)
1826

1927

20-
def is_standalone_source_file(path):
28+
def is_standalone_source_file(path: pathlib.Path) -> bool:
29+
"""
30+
Check if the given path is a standalone source file.
31+
32+
Args:
33+
path (pathlib.Path): The path to check.
34+
35+
Returns:
36+
bool: True if the path is a standalone source file, False otherwise.
37+
"""
2138
# Heuristics for standalone source file
2239
if not path.is_file():
2340
return False
@@ -27,16 +44,26 @@ def is_standalone_source_file(path):
2744
return False # skip virtual environments
2845
if "build" in path.parts or "out" in path.parts:
2946
return False # skip build outputs
30-
if any(x in path.name.lower() for x in ["generated files", "build", "lib", "generated", "autogen", "moc_", "ui_"]):
47+
if any(x in path.name.lower() for x in ["build", "lib", "generated", "autogen", "moc_", "ui_"]):
3148
return False # skip generated files
3249
# Assume headers are not standalone sources
3350
# if path.suffix in [".h", ".hpp"]:
3451
# return False
3552
return True
3653

3754

38-
def find_files(paths, extensions) -> list[pathlib.Path]:
39-
files = []
55+
def find_files(paths: list[pathlib.Path], extensions: set[str]) -> list[pathlib.Path]:
56+
"""
57+
Find all files in the given paths with the specified extensions.
58+
59+
Args:
60+
paths (list[pathlib.Path]): The directories to search for files.
61+
extensions (set[str]): The file extensions to include in the search.
62+
63+
Returns:
64+
list[pathlib.Path]: A list of found files with the specified extensions.
65+
"""
66+
files: list[pathlib.Path] = []
4067
for path in paths:
4168
root = pathlib.Path(path).resolve()
4269
if not root.exists():
@@ -51,7 +78,16 @@ def find_files(paths, extensions) -> list[pathlib.Path]:
5178
return files
5279

5380

54-
def check_clang_format_config(start_paths):
81+
def check_clang_format_config(start_paths: list[str]) -> bool:
82+
"""
83+
Check if a .clang-format file exists in specified or parent directories.
84+
85+
Args:
86+
start_paths (pathlib.Path): The directories to search for .clang-format files.
87+
88+
Returns:
89+
bool: True if a .clang-format file is found, False otherwise.
90+
"""
5591
for path in start_paths:
5692
dir_path = pathlib.Path(path).resolve()
5793
for parent in [*dir_path.parents, dir_path]:
@@ -60,7 +96,14 @@ def check_clang_format_config(start_paths):
6096
return False
6197

6298

63-
def format_files(files):
99+
def format_files(files: list[pathlib.Path]) -> None:
100+
"""
101+
Format the given files using clang-format.
102+
This function runs clang-format on each file and prints the progress.
103+
104+
Args:
105+
files (list[pathlib.Path]): The list of files to format.
106+
"""
64107
total = len(files)
65108
for i, file_path in enumerate(files, start=1):
66109
percent = int((i / total) * 100)
@@ -72,6 +115,9 @@ def format_files(files):
72115

73116

74117
def main():
118+
"""
119+
Main function to parse command-line arguments and format files.
120+
"""
75121
parser = argparse.ArgumentParser(
76122
description="Batch format C/C++ files using clang-format."
77123
)
@@ -105,7 +151,7 @@ def main():
105151
args = parser.parse_args()
106152

107153
if not check_clang_format_config(args.paths):
108-
print("⚠️ Warning: No .clang-format file found in the specified paths or their parent directories.\n")
154+
print("⚠️ Warning: No .clang-format file found in specified or parent directories.\n")
109155

110156
files = find_files(args.paths, set(args.extensions))
111157

0 commit comments

Comments
 (0)