Skip to content

Commit b207cf9

Browse files
committed
Linting fixes
1 parent 1d52284 commit b207cf9

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/vendetect/_cli.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,13 @@ def output_json(
146146
json.dump(results, output, indent=2)
147147

148148

149-
def output_rich( # noqa: PLR0912 PLR0915 C901
149+
def output_rich( # noqa: PLR0912 PLR0913 PLR0915 C901
150150
detections: Iterable[Detection],
151151
console: Console,
152152
min_similarity: float = 0.5,
153153
output_file: TextIO | None = None,
154154
collapse_identical_lines_threshold: int = 10,
155+
edit_distance_threshold: float = 0.75,
155156
) -> None:
156157
# If an output file is specified, create a new Console for it
157158
file_console = Console(file=output_file) if output_file else console
@@ -232,14 +233,16 @@ def read_file_content(file: File) -> str:
232233
else:
233234
if diff_line.status == DiffLineStatus.COPIED:
234235
status_col = Text("←", style="red reverse bold")
236+
# calculate the normalized edit distance
237+
elif (
238+
diff_line.left is not None
239+
and diff_line.right is not None
240+
and normalized_edit_distance(diff_line.left, diff_line.right) < edit_distance_threshold
241+
):
242+
# the lines are at least 25% similar!
243+
status_col = Text("↜", style="yellow reverse")
235244
else:
236-
# calculate the normalized edit distance
237-
if diff_line.left is not None and diff_line.right is not None and \
238-
normalized_edit_distance(diff_line.left, diff_line.right) < 0.75:
239-
# the lines are at least 25% similar!
240-
status_col = Text("↜", style="yellow reverse")
241-
else:
242-
status_col = Text("✓", style="green reverse")
245+
status_col = Text("✓", style="green reverse")
243246
if diff_line.left is None:
244247
left: ConsoleRenderable = Text("")
245248
else:

src/vendetect/diffing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ def diff_from_offsets(
203203

204204

205205
def edit_distance(s1: str, s2: str) -> int:
206-
"""Calculates the minimum number of edits to convert s1 into s2"""
207-
# HACK: insert a newline after every character, then use the Myers Diff algorithm since it's built into Python;
208-
# the result should be the same as Levenshtein distance!
206+
"""Calculate the minimum number of edits to convert s1 into s2."""
207+
# insert a newline after every character, then use the Myers Diff algorithm since it's built into Python;
208+
# the result should be the same as Levenshtein distance!
209209
newline = "\n"
210210
s1 = f"{newline.join(s1)}\n"
211211
s2 = f"{newline.join(s2)}\n"

src/vendetect/repo.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,11 @@ def __init__(self, path: Path, repo: Repository):
294294
@property
295295
def line_start_offsets(self) -> list[int]:
296296
if not self._line_start_offsets:
297-
with self.repo:
298-
with open(self.path, "r") as f:
299-
self._line_start_offsets.append(0)
300-
for line in f:
301-
self._line_start_offsets.append(self._line_start_offsets[-1] + len(line))
302-
self._line_start_offsets.pop()
297+
with self.repo, self.path.open("r") as f:
298+
self._line_start_offsets.append(0)
299+
for line in f:
300+
self._line_start_offsets.append(self._line_start_offsets[-1] + len(line))
301+
self._line_start_offsets.pop()
303302
return self._line_start_offsets
304303

305304
def __hash__(self) -> int:
@@ -316,13 +315,12 @@ def resolve(self) -> Self:
316315
return self.__class__(self.path.readlink(), self.repo)
317316
return self
318317

319-
def get_line(self, byte_offset: int, rounding: Rounding = Rounding.DOWN, min_line: int = 0):
320-
"""Returns the zero-indexed line associated with byte offset `byte_offset`"""
318+
def get_line(self, byte_offset: int, rounding: Rounding = Rounding.DOWN, min_line: int = 0) -> int:
319+
"""Return the zero-indexed line associated with byte offset `byte_offset`."""
321320
if byte_offset < 0:
322-
with self.repo:
323-
with open(self.path, "r") as f:
324-
f.seek(byte_offset, SEEK_END)
325-
byte_offset = f.tell()
321+
with self.repo, self.path.open("r") as f:
322+
f.seek(byte_offset, SEEK_END)
323+
byte_offset = f.tell()
326324
if rounding == Rounding.DOWN:
327325
start = max(min_line, 0)
328326
while start + 1 < len(self.line_start_offsets) and self.line_start_offsets[start + 1] < byte_offset:

0 commit comments

Comments
 (0)