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
Branches
No related tags found
No related merge requests found
......@@ -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(
......
......@@ -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],
......
......@@ -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(
......
......@@ -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:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment