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_ide/qtcreator/externaltools/qtc_sort_paths.py",
|
||||||
"tools/utils_maintenance/blender_menu_search_coverage.py", # Uses `bpy`.
|
"tools/utils_maintenance/blender_menu_search_coverage.py", # Uses `bpy`.
|
||||||
"tools/utils_maintenance/blender_update_themes.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 trailing_space_clean_config import PATHS
|
||||||
|
|
||||||
|
from collections.abc import (
|
||||||
|
Callable,
|
||||||
|
Iterator,
|
||||||
|
Sequence,
|
||||||
|
)
|
||||||
|
|
||||||
SOURCE_EXT = (
|
SOURCE_EXT = (
|
||||||
# C/C++
|
# C/C++
|
||||||
".c", ".h", ".cpp", ".hpp", ".cc", ".hh", ".cxx", ".hxx", ".inl",
|
".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)
|
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):
|
for dirpath, dirnames, filenames in os.walk(path):
|
||||||
# skip ".git"
|
# skip ".git"
|
||||||
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
|
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
|
||||||
@@ -50,7 +59,10 @@ def path_iter(path, filename_check=None):
|
|||||||
yield filepath
|
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:
|
for f in paths:
|
||||||
if not os.path.exists(f):
|
if not os.path.exists(f):
|
||||||
print("Missing:", f)
|
print("Missing:", f)
|
||||||
@@ -60,17 +72,18 @@ def path_expand(paths, filename_check=None):
|
|||||||
yield f
|
yield f
|
||||||
|
|
||||||
|
|
||||||
def rstrip_file(filename):
|
def rstrip_file(filename: str) -> tuple[str, ...]:
|
||||||
reports = []
|
reports = []
|
||||||
with open(filename, "r", encoding="utf-8") as fh:
|
with open(filename, "r", encoding="utf-8") as fh:
|
||||||
data_src = fh.read()
|
data_src = fh.read()
|
||||||
|
|
||||||
# Strip trailing space.
|
# Strip trailing space.
|
||||||
data_dst = []
|
data_dst_list = []
|
||||||
for l in data_src.rstrip().splitlines(True):
|
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.
|
# Remove BOM.
|
||||||
if data_dst and (data_dst[0] == '\ufeff'):
|
if data_dst and (data_dst[0] == '\ufeff'):
|
||||||
@@ -86,7 +99,7 @@ def rstrip_file(filename):
|
|||||||
return tuple(reports)
|
return tuple(reports)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
for f in path_expand(PATHS, is_source):
|
for f in path_expand(PATHS, is_source):
|
||||||
report = rstrip_file(f)
|
report = rstrip_file(f)
|
||||||
if report:
|
if report:
|
||||||
|
|||||||
@@ -9,7 +9,13 @@ __all__ = (
|
|||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
PATHS = (
|
from collections.abc import (
|
||||||
|
Callable,
|
||||||
|
Iterator,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
PATHS: tuple[str, ...] = (
|
||||||
"build_files/build_environment/cmake",
|
"build_files/build_environment/cmake",
|
||||||
"build_files/cmake",
|
"build_files/cmake",
|
||||||
"doc/python_api",
|
"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):
|
for dirpath, dirnames, filenames in os.walk(path):
|
||||||
# skip '.git'
|
# skip '.git'
|
||||||
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
|
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
|
||||||
|
|||||||
Reference in New Issue
Block a user