Final renaming and type hints

This commit is contained in:
disinvite 2023-11-20 21:48:38 -05:00
parent 7c65e5bd3e
commit 5c7da9f6b3
5 changed files with 20 additions and 18 deletions

View File

@ -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')

View File

@ -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:

View File

@ -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

View File

@ -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."""

View File

@ -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