Cleanup: use type hints for trailing_space_clean.py utility
This commit is contained in:
@@ -85,8 +85,6 @@ PATHS_EXCLUDE = set(
|
||||
"tools/utils_ide/qtcreator/externaltools/qtc_sort_paths.py",
|
||||
"tools/utils_maintenance/blender_menu_search_coverage.py", # Uses `bpy`.
|
||||
"tools/utils_maintenance/blender_update_themes.py", # Uses `bpy`.
|
||||
"tools/utils_maintenance/trailing_space_clean.py",
|
||||
"tools/utils_maintenance/trailing_space_clean_config.py",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -12,6 +12,12 @@ from os.path import join
|
||||
|
||||
from trailing_space_clean_config import PATHS
|
||||
|
||||
from collections.abc import (
|
||||
Callable,
|
||||
Iterator,
|
||||
Sequence,
|
||||
)
|
||||
|
||||
SOURCE_EXT = (
|
||||
# C/C++
|
||||
".c", ".h", ".cpp", ".hpp", ".cc", ".hh", ".cxx", ".hxx", ".inl",
|
||||
@@ -33,11 +39,14 @@ SOURCE_EXT = (
|
||||
)
|
||||
|
||||
|
||||
def is_source(filename):
|
||||
def is_source(filename: str) -> bool:
|
||||
return filename.endswith(SOURCE_EXT)
|
||||
|
||||
|
||||
def path_iter(path, filename_check=None):
|
||||
def path_iter(
|
||||
path: str,
|
||||
filename_check: Callable[[str], bool] | None = None,
|
||||
) -> Iterator[str]:
|
||||
for dirpath, dirnames, filenames in os.walk(path):
|
||||
# skip ".git"
|
||||
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
|
||||
@@ -50,7 +59,10 @@ def path_iter(path, filename_check=None):
|
||||
yield filepath
|
||||
|
||||
|
||||
def path_expand(paths, filename_check=None):
|
||||
def path_expand(
|
||||
paths: Sequence[str],
|
||||
filename_check: Callable[[str], bool] | None = None,
|
||||
) -> Iterator[str]:
|
||||
for f in paths:
|
||||
if not os.path.exists(f):
|
||||
print("Missing:", f)
|
||||
@@ -60,17 +72,18 @@ def path_expand(paths, filename_check=None):
|
||||
yield f
|
||||
|
||||
|
||||
def rstrip_file(filename):
|
||||
def rstrip_file(filename: str) -> tuple[str, ...]:
|
||||
reports = []
|
||||
with open(filename, "r", encoding="utf-8") as fh:
|
||||
data_src = fh.read()
|
||||
|
||||
# Strip trailing space.
|
||||
data_dst = []
|
||||
data_dst_list = []
|
||||
for l in data_src.rstrip().splitlines(True):
|
||||
data_dst.append(l.rstrip() + "\n")
|
||||
data_dst_list.append(l.rstrip() + "\n")
|
||||
|
||||
data_dst = "".join(data_dst)
|
||||
data_dst = "".join(data_dst_list)
|
||||
del data_dst_list
|
||||
|
||||
# Remove BOM.
|
||||
if data_dst and (data_dst[0] == '\ufeff'):
|
||||
@@ -86,7 +99,7 @@ def rstrip_file(filename):
|
||||
return tuple(reports)
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> None:
|
||||
for f in path_expand(PATHS, is_source):
|
||||
report = rstrip_file(f)
|
||||
if report:
|
||||
|
||||
@@ -9,7 +9,13 @@ __all__ = (
|
||||
|
||||
|
||||
import os
|
||||
PATHS = (
|
||||
from collections.abc import (
|
||||
Callable,
|
||||
Iterator,
|
||||
)
|
||||
|
||||
|
||||
PATHS: tuple[str, ...] = (
|
||||
"build_files/build_environment/cmake",
|
||||
"build_files/cmake",
|
||||
"doc/python_api",
|
||||
@@ -38,7 +44,7 @@ PATHS = tuple(
|
||||
)
|
||||
|
||||
|
||||
def files(path, test_fn):
|
||||
def files(path: str, test_fn: Callable[[str], bool]) -> Iterator[str]:
|
||||
for dirpath, dirnames, filenames in os.walk(path):
|
||||
# skip '.git'
|
||||
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
|
||||
|
||||
Reference in New Issue
Block a user