From 6ebe1c4a55feb90fac03cfdf3582437526813636 Mon Sep 17 00:00:00 2001 From: Campbell Barton <ideasman42@gmail.com> Date: Mon, 12 Apr 2021 22:46:38 +1000 Subject: [PATCH] Cleanup: use type hinting for batch_edit_text and it's users --- utils_maintenance/c_sort_blocks.py | 9 ++++-- utils_maintenance/c_struct_clean.py | 14 +++++++--- utils_maintenance/cmake_sort_filelists.py | 24 ++++++++++------ utils_maintenance/modules/batch_edit_text.py | 29 +++++++++++++++----- 4 files changed, 54 insertions(+), 22 deletions(-) diff --git a/utils_maintenance/c_sort_blocks.py b/utils_maintenance/c_sort_blocks.py index ba06bd0..bc3f318 100755 --- a/utils_maintenance/c_sort_blocks.py +++ b/utils_maintenance/c_sort_blocks.py @@ -26,6 +26,10 @@ sys.path.append(os.path.join(PWD, "modules")) from batch_edit_text import run +from typing import ( + Optional, +) + SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.normpath(os.path.join(PWD, "..", "..", "..")))) # TODO, move to config file @@ -42,7 +46,7 @@ SOURCE_EXT = ( ) -def sort_struct_lists(fn, data_src): +def sort_struct_lists(fn: str, data_src: str) -> Optional[str]: import re # eg: @@ -63,7 +67,7 @@ def sort_struct_lists(fn, data_src): lines = data_src.splitlines(keepends=True) - def can_sort(l): + def can_sort(l: str) -> Optional[int]: if re_match_struct.match(l): return 1 if re_match_struct_type.match(l): @@ -94,6 +98,7 @@ def sort_struct_lists(fn, data_src): data_dst = "".join(lines) if data_src != data_dst: return data_dst + return None run( diff --git a/utils_maintenance/c_struct_clean.py b/utils_maintenance/c_struct_clean.py index e327ad8..7fe6059 100755 --- a/utils_maintenance/c_struct_clean.py +++ b/utils_maintenance/c_struct_clean.py @@ -29,6 +29,11 @@ import os import sys import re +from typing import ( + Dict, + Optional, +) + PWD = os.path.dirname(__file__) sys.path.append(os.path.join(PWD, "modules")) @@ -52,12 +57,12 @@ SOURCE_EXT = ( re_words = re.compile("[A-Za-z_][A-Za-z_0-9]*") re_match_struct = re.compile(r"struct\s+([A-Za-z_][A-Za-z_0-9]*)\s*;") -def clean_structs(fn, data_src): +def clean_structs(fn: str, data_src: str) -> Optional[str]: import re - word_occurance = {} - for w in re_words.finditer(data_src): - w = w.group(0) + word_occurance: Dict[str, int] = {} + for w_match in re_words.finditer(data_src): + w = w_match.group(0) try: word_occurance[w] += 1 except KeyError: @@ -80,6 +85,7 @@ def clean_structs(fn, data_src): data_dst = "".join(lines) if data_src != data_dst: return data_dst + return None run( directories=[os.path.join(SOURCE_DIR, d) for d in SOURCE_DIRS], diff --git a/utils_maintenance/cmake_sort_filelists.py b/utils_maintenance/cmake_sort_filelists.py index 6ee34c2..7ca5565 100755 --- a/utils_maintenance/cmake_sort_filelists.py +++ b/utils_maintenance/cmake_sort_filelists.py @@ -27,6 +27,10 @@ Sorts CMake path lists import os import sys +from typing import ( + Optional, +) + PWD = os.path.dirname(__file__) sys.path.append(os.path.join(PWD, "modules")) @@ -47,11 +51,11 @@ SOURCE_EXT = ( ".m", ".mm", ) -def sort_cmake_file_lists(fn, data_src): +def sort_cmake_file_lists(fn: str, data_src: str) -> Optional[str]: fn_dir = os.path.dirname(fn) lines = data_src.splitlines(keepends=True) - def can_sort(l): + def can_sort(l: str) -> bool: l = l.split("#", 1)[0].strip() # Source files. if l.endswith(SOURCE_EXT): @@ -63,8 +67,9 @@ def sort_cmake_file_lists(fn, data_src): # Libs. if l.startswith(("bf_", "extern_")) and "." not in l and "/" not in l: return True + return False - def can_sort_compat(a, b): + def can_sort_compat(a: str, b: str) -> bool: # Strip comments. a = a.split("#", 1)[0] b = b.split("#", 1)[0] @@ -74,14 +79,14 @@ def sort_cmake_file_lists(fn, data_src): # return False # Compare loading paths. - a = a.split("/") - b = b.split("/") - if len(a) == 1 and len(b) == 1: + a_ls = a.split("/") + b_ls = b.split("/") + if len(a_ls) == 1 and len(b_ls) == 1: return True - if len(a) == len(b): - if len(a) == 1: + if len(a_ls) == len(b_ls): + if len(a_ls) == 1: return True - if a[:-1] == b[:-1]: + if a_ls[:-1] == b_ls[:-1]: return True return False @@ -103,6 +108,7 @@ def sort_cmake_file_lists(fn, data_src): data_dst = "".join(lines) if data_src != data_dst: return data_dst + return None run( diff --git a/utils_maintenance/modules/batch_edit_text.py b/utils_maintenance/modules/batch_edit_text.py index 2e63df8..381a2d9 100644 --- a/utils_maintenance/modules/batch_edit_text.py +++ b/utils_maintenance/modules/batch_edit_text.py @@ -16,7 +16,22 @@ # # ##### END GPL LICENSE BLOCK ##### -def operation_wrap(args): +from typing import ( + Callable, + Generator, + Optional, + Sequence, + Tuple, +) + +TextOpFn = Callable[ + # file_name, data_src + [str, str], + # data_dst or None when no change is made. + Optional[str] +] + +def operation_wrap(args: Tuple[str, TextOpFn]) -> None: fn, text_operation = args with open(fn, "r", encoding="utf-8") as f: data_src = f.read() @@ -29,16 +44,16 @@ def operation_wrap(args): f.write(data_dst) def run(*, - directories, - is_text, - text_operation, - use_multiprocess, -): + directories: Sequence[str], + is_text: Callable[[str], bool], + text_operation: TextOpFn, + use_multiprocess: bool, +) -> None: print(directories) import os - def source_files(path): + def source_files(path: str) -> Generator[str, None, None]: for dirpath, dirnames, filenames in os.walk(path): dirnames[:] = [d for d in dirnames if not d.startswith(".")] for filename in filenames: -- GitLab