Skip to content
Snippets Groups Projects
Commit 6ebe1c4a authored by Campbell Barton's avatar Campbell Barton
Browse files

Cleanup: use type hinting for batch_edit_text and it's users

parent 7e3892bb
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,10 @@ sys.path.append(os.path.join(PWD, "modules")) ...@@ -26,6 +26,10 @@ sys.path.append(os.path.join(PWD, "modules"))
from batch_edit_text import run 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, "..", "..", "..")))) SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.normpath(os.path.join(PWD, "..", "..", ".."))))
# TODO, move to config file # TODO, move to config file
...@@ -42,7 +46,7 @@ SOURCE_EXT = ( ...@@ -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 import re
# eg: # eg:
...@@ -63,7 +67,7 @@ def sort_struct_lists(fn, data_src): ...@@ -63,7 +67,7 @@ def sort_struct_lists(fn, data_src):
lines = data_src.splitlines(keepends=True) lines = data_src.splitlines(keepends=True)
def can_sort(l): def can_sort(l: str) -> Optional[int]:
if re_match_struct.match(l): if re_match_struct.match(l):
return 1 return 1
if re_match_struct_type.match(l): if re_match_struct_type.match(l):
...@@ -94,6 +98,7 @@ def sort_struct_lists(fn, data_src): ...@@ -94,6 +98,7 @@ def sort_struct_lists(fn, data_src):
data_dst = "".join(lines) data_dst = "".join(lines)
if data_src != data_dst: if data_src != data_dst:
return data_dst return data_dst
return None
run( run(
......
...@@ -29,6 +29,11 @@ import os ...@@ -29,6 +29,11 @@ import os
import sys import sys
import re import re
from typing import (
Dict,
Optional,
)
PWD = os.path.dirname(__file__) PWD = os.path.dirname(__file__)
sys.path.append(os.path.join(PWD, "modules")) sys.path.append(os.path.join(PWD, "modules"))
...@@ -52,12 +57,12 @@ SOURCE_EXT = ( ...@@ -52,12 +57,12 @@ SOURCE_EXT = (
re_words = re.compile("[A-Za-z_][A-Za-z_0-9]*") 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*;") 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 import re
word_occurance = {} word_occurance: Dict[str, int] = {}
for w in re_words.finditer(data_src): for w_match in re_words.finditer(data_src):
w = w.group(0) w = w_match.group(0)
try: try:
word_occurance[w] += 1 word_occurance[w] += 1
except KeyError: except KeyError:
...@@ -80,6 +85,7 @@ def clean_structs(fn, data_src): ...@@ -80,6 +85,7 @@ def clean_structs(fn, data_src):
data_dst = "".join(lines) data_dst = "".join(lines)
if data_src != data_dst: if data_src != data_dst:
return data_dst return data_dst
return None
run( run(
directories=[os.path.join(SOURCE_DIR, d) for d in SOURCE_DIRS], directories=[os.path.join(SOURCE_DIR, d) for d in SOURCE_DIRS],
......
...@@ -27,6 +27,10 @@ Sorts CMake path lists ...@@ -27,6 +27,10 @@ Sorts CMake path lists
import os import os
import sys import sys
from typing import (
Optional,
)
PWD = os.path.dirname(__file__) PWD = os.path.dirname(__file__)
sys.path.append(os.path.join(PWD, "modules")) sys.path.append(os.path.join(PWD, "modules"))
...@@ -47,11 +51,11 @@ SOURCE_EXT = ( ...@@ -47,11 +51,11 @@ SOURCE_EXT = (
".m", ".mm", ".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) fn_dir = os.path.dirname(fn)
lines = data_src.splitlines(keepends=True) lines = data_src.splitlines(keepends=True)
def can_sort(l): def can_sort(l: str) -> bool:
l = l.split("#", 1)[0].strip() l = l.split("#", 1)[0].strip()
# Source files. # Source files.
if l.endswith(SOURCE_EXT): if l.endswith(SOURCE_EXT):
...@@ -63,8 +67,9 @@ def sort_cmake_file_lists(fn, data_src): ...@@ -63,8 +67,9 @@ def sort_cmake_file_lists(fn, data_src):
# Libs. # Libs.
if l.startswith(("bf_", "extern_")) and "." not in l and "/" not in l: if l.startswith(("bf_", "extern_")) and "." not in l and "/" not in l:
return True return True
return False
def can_sort_compat(a, b): def can_sort_compat(a: str, b: str) -> bool:
# Strip comments. # Strip comments.
a = a.split("#", 1)[0] a = a.split("#", 1)[0]
b = b.split("#", 1)[0] b = b.split("#", 1)[0]
...@@ -74,14 +79,14 @@ def sort_cmake_file_lists(fn, data_src): ...@@ -74,14 +79,14 @@ def sort_cmake_file_lists(fn, data_src):
# return False # return False
# Compare loading paths. # Compare loading paths.
a = a.split("/") a_ls = a.split("/")
b = b.split("/") b_ls = b.split("/")
if len(a) == 1 and len(b) == 1: if len(a_ls) == 1 and len(b_ls) == 1:
return True return True
if len(a) == len(b): if len(a_ls) == len(b_ls):
if len(a) == 1: if len(a_ls) == 1:
return True return True
if a[:-1] == b[:-1]: if a_ls[:-1] == b_ls[:-1]:
return True return True
return False return False
...@@ -103,6 +108,7 @@ def sort_cmake_file_lists(fn, data_src): ...@@ -103,6 +108,7 @@ def sort_cmake_file_lists(fn, data_src):
data_dst = "".join(lines) data_dst = "".join(lines)
if data_src != data_dst: if data_src != data_dst:
return data_dst return data_dst
return None
run( run(
......
...@@ -16,7 +16,22 @@ ...@@ -16,7 +16,22 @@
# #
# ##### END GPL LICENSE BLOCK ##### # ##### 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 fn, text_operation = args
with open(fn, "r", encoding="utf-8") as f: with open(fn, "r", encoding="utf-8") as f:
data_src = f.read() data_src = f.read()
...@@ -29,16 +44,16 @@ def operation_wrap(args): ...@@ -29,16 +44,16 @@ def operation_wrap(args):
f.write(data_dst) f.write(data_dst)
def run(*, def run(*,
directories, directories: Sequence[str],
is_text, is_text: Callable[[str], bool],
text_operation, text_operation: TextOpFn,
use_multiprocess, use_multiprocess: bool,
): ) -> None:
print(directories) print(directories)
import os import os
def source_files(path): def source_files(path: str) -> Generator[str, None, None]:
for dirpath, dirnames, filenames in os.walk(path): for dirpath, dirnames, filenames in os.walk(path):
dirnames[:] = [d for d in dirnames if not d.startswith(".")] dirnames[:] = [d for d in dirnames if not d.startswith(".")]
for filename in filenames: for filename in filenames:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment