From 5c7da9f6b3d729d125b73050f9b38e525ca64325 Mon Sep 17 00:00:00 2001 From: disinvite Date: Mon, 20 Nov 2023 21:48:38 -0500 Subject: [PATCH] Final renaming and type hints --- tools/checkorder/checkorder.py | 4 ++-- tools/isledecomp/isledecomp/dir.py | 4 ++-- tools/isledecomp/isledecomp/parser/parser.py | 14 +++++++------- tools/isledecomp/isledecomp/parser/util.py | 5 +++-- tools/isledecomp/tests/test_parser_util.py | 11 ++++++----- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tools/checkorder/checkorder.py b/tools/checkorder/checkorder.py index e1235fad..eb243c2d 100644 --- a/tools/checkorder/checkorder.py +++ b/tools/checkorder/checkorder.py @@ -3,7 +3,7 @@ import argparse from isledecomp.dir import ( walk_source_dir, - file_is_cpp + is_file_cpp ) from isledecomp.parser import find_code_blocks from isledecomp.parser.util import ( @@ -92,7 +92,7 @@ def main(): if os.path.isdir(args['target']): files_to_check = list(walk_source_dir(args['target'])) - elif os.path.isfile(args['target']) and file_is_cpp(args['target']): + elif os.path.isfile(args['target']) and is_file_cpp(args['target']): files_to_check = [args['target']] else: sys.exit('Invalid target') diff --git a/tools/isledecomp/isledecomp/dir.py b/tools/isledecomp/isledecomp/dir.py index 3d4dea64..7904c1c0 100644 --- a/tools/isledecomp/isledecomp/dir.py +++ b/tools/isledecomp/isledecomp/dir.py @@ -2,7 +2,7 @@ from typing import Iterator -def file_is_cpp(filename: str) -> bool: +def is_file_cpp(filename: str) -> bool: (basefile, ext) = os.path.splitext(filename) return ext.lower() in ('.h', '.cpp') @@ -14,7 +14,7 @@ def walk_source_dir(source: str, recursive: bool = True) -> Iterator[str]: source = os.path.abspath(source) for subdir, dirs, files in os.walk(source): for file in files: - if file_is_cpp(file): + if is_file_cpp(file): yield os.path.join(subdir, file) if not recursive: diff --git a/tools/isledecomp/isledecomp/parser/parser.py b/tools/isledecomp/isledecomp/parser/parser.py index f5638eb0..345c8a98 100644 --- a/tools/isledecomp/isledecomp/parser/parser.py +++ b/tools/isledecomp/isledecomp/parser/parser.py @@ -8,9 +8,9 @@ is_blank_or_comment, match_offset_comment, is_exact_offset_comment, - template_function_name, + get_template_function_name, remove_trailing_comment, - distinct_module, + distinct_by_module, ) @@ -30,16 +30,16 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]: binary. We expect the result to be ordered by line number because we are reading the file from start to finish.""" - blocks = [] + blocks: List[CodeBlock] = [] - offset_matches = [] + offset_matches: List[OffsetMatch] = [] function_sig = None start_line = None end_line = None state = ReaderState.WANT_OFFSET - # 1-based to match cvdump and your text-editor + # 1-based to match cvdump and your text editor # I know it says 0, but we will increment before each readline() line_no = 0 can_seek = True @@ -50,7 +50,7 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]: if state == ReaderState.FUNCTION_DONE: # Our list of offset marks could have duplicates on # module name, so we'll eliminate those now. - for offset_match in distinct_module(offset_matches): + for offset_match in distinct_by_module(offset_matches): block = CodeBlock(offset=offset_match.address, signature=function_sig, start_line=start_line, @@ -98,7 +98,7 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]: elif state == ReaderState.IN_TEMPLATE: # TEMPLATE functions are a special case. The signature is # given on the next line (in a // comment) - function_sig = template_function_name(line) + function_sig = get_template_function_name(line) start_line = line_no end_line = line_no state = ReaderState.FUNCTION_DONE diff --git a/tools/isledecomp/isledecomp/parser/util.py b/tools/isledecomp/isledecomp/parser/util.py index c7ad91a3..848fd5ff 100644 --- a/tools/isledecomp/isledecomp/parser/util.py +++ b/tools/isledecomp/isledecomp/parser/util.py @@ -1,6 +1,7 @@ # C++ Parser utility functions and data structures from __future__ import annotations # python <3.10 compatibility import re +from typing import List from collections import namedtuple @@ -34,7 +35,7 @@ trailingCommentRegex = re.compile(r'(\s*(?://|/\*).*)$') -def template_function_name(line: str) -> str: +def get_template_function_name(line: str) -> str: """Parse function signature for special TEMPLATE functions""" template_match = templateCommentRegex.match(line) @@ -78,7 +79,7 @@ def match_offset_comment(line: str) -> OffsetMatch | None: comment=line.strip()) -def distinct_module(offsets: [OffsetMatch]) -> [OffsetMatch]: +def distinct_by_module(offsets: List) -> List: """Given a list of offset markers, return a list with distinct module names. If module names (case-insensitive) are repeated, choose the offset that appears first.""" diff --git a/tools/isledecomp/tests/test_parser_util.py b/tools/isledecomp/tests/test_parser_util.py index 4030c56a..3fc9bb61 100644 --- a/tools/isledecomp/tests/test_parser_util.py +++ b/tools/isledecomp/tests/test_parser_util.py @@ -1,10 +1,11 @@ import pytest from collections import namedtuple +from typing import List from isledecomp.parser.util import ( is_blank_or_comment, match_offset_comment, is_exact_offset_comment, - distinct_module, + distinct_by_module, ) @@ -92,7 +93,7 @@ def test_exact_offset_comment(line: str, exact: bool, match): # Helper for the next test: cut down version of OffsetMatch MiniOfs = namedtuple('MiniOfs', ['module', 'value']) -distinct_module_samples = [ +distinct_by_module_samples = [ # empty set ([], []), # same module name @@ -107,6 +108,6 @@ def test_exact_offset_comment(line: str, exact: bool, match): ] -@pytest.mark.parametrize('sample, expected', distinct_module_samples) -def test_distinct_module(sample: [MiniOfs], expected: [MiniOfs]): - assert distinct_module(sample) == expected +@pytest.mark.parametrize('sample, expected', distinct_by_module_samples) +def test_distinct_by_module(sample: List[MiniOfs], expected: List[MiniOfs]): + assert distinct_by_module(sample) == expected