Skip to content

Commit fe2d79d

Browse files
Extract Comparator class from CompareCommand
Move JSON loading, batching, and cross-referencing logic into a dedicated Comparator class. CompareCommand.run() now delegates to the Comparator and iterates over it to build the comment.
1 parent 345504d commit fe2d79d

31 files changed

Lines changed: 134 additions & 66 deletions
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2+
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
3+
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
4+
5+
from __future__ import annotations
6+
7+
import json
8+
from collections.abc import Generator
9+
from pathlib import Path
10+
11+
from pylint.reporters.json_reporter import OldJsonExport
12+
from pylint.testutils._primer.primer_command import (
13+
PackageData,
14+
PackageMessages,
15+
)
16+
17+
18+
class Comparator:
19+
def __init__(
20+
self, base_file: str, new_file: str, batches: int | None = None
21+
) -> None:
22+
self._base_file = base_file
23+
self._new_file = new_file
24+
self._batches = batches
25+
26+
def __iter__(self) -> Generator[tuple[str, PackageData, PackageData]]:
27+
main_data: PackageMessages
28+
if self._batches is None:
29+
main_data = self._load_json(self._base_file)
30+
pr_data = self._load_json(self._new_file)
31+
else:
32+
main_data = {}
33+
pr_data = {}
34+
for idx in range(self._batches):
35+
main_data.update(
36+
self._load_json(
37+
self._base_file.replace("BATCHIDX", "batch" + str(idx))
38+
)
39+
)
40+
pr_data.update(
41+
self._load_json(
42+
self._new_file.replace("BATCHIDX", "batch" + str(idx))
43+
)
44+
)
45+
46+
missing_messages: PackageMessages = {}
47+
for package, data in main_data.items():
48+
package_missing_messages: list[OldJsonExport] = []
49+
for message in data["messages"]:
50+
try:
51+
pr_data[package]["messages"].remove(message)
52+
except ValueError:
53+
package_missing_messages.append(message)
54+
missing_messages[package] = PackageData(
55+
commit=pr_data[package]["commit"],
56+
messages=package_missing_messages,
57+
)
58+
59+
for package, pkg_missing in missing_messages.items():
60+
new_messages = pr_data[package]
61+
if not pkg_missing["messages"] and not new_messages["messages"]:
62+
continue
63+
yield package, pkg_missing, new_messages
64+
65+
@staticmethod
66+
def _load_json(file_path: Path | str) -> PackageMessages:
67+
with open(file_path, encoding="utf-8") as f:
68+
result: PackageMessages = json.load(f)
69+
return result

pylint/testutils/_primer/primer_compare_command.py

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,28 @@
33
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
44
from __future__ import annotations
55

6-
import json
7-
from pathlib import Path, PurePosixPath
6+
from pathlib import PurePosixPath
87

9-
from pylint.reporters.json_reporter import OldJsonExport
10-
from pylint.testutils._primer.primer_command import (
11-
PackageData,
12-
PackageMessages,
13-
PrimerCommand,
14-
)
8+
from pylint.testutils._primer.comparator import Comparator
9+
from pylint.testutils._primer.primer_command import PackageData, PrimerCommand
1510

1611
MAX_GITHUB_COMMENT_LENGTH = 65536
1712

1813

1914
class CompareCommand(PrimerCommand):
2015
def run(self) -> None:
21-
if self.config.batches is None:
22-
main_data = self._load_json(self.config.base_file)
23-
pr_data = self._load_json(self.config.new_file)
24-
else:
25-
main_data = {}
26-
pr_data = {}
27-
for idx in range(self.config.batches):
28-
main_data.update(
29-
self._load_json(
30-
self.config.base_file.replace("BATCHIDX", "batch" + str(idx))
31-
)
32-
)
33-
pr_data.update(
34-
self._load_json(
35-
self.config.new_file.replace("BATCHIDX", "batch" + str(idx))
36-
)
37-
)
38-
39-
missing_messages_data, new_messages_data = self._cross_reference(
40-
main_data, pr_data
16+
comparator = Comparator(
17+
self.config.base_file, self.config.new_file, self.config.batches
4118
)
42-
comment = self._create_comment(missing_messages_data, new_messages_data)
19+
comment = self._create_comment(comparator)
4320
with open(self.primer_directory / "comment.txt", "w", encoding="utf-8") as f:
4421
f.write(comment)
4522

46-
@staticmethod
47-
def _cross_reference(
48-
main_data: PackageMessages, pr_data: PackageMessages
49-
) -> tuple[PackageMessages, PackageMessages]:
50-
missing_messages_data: PackageMessages = {}
51-
for package, data in main_data.items():
52-
package_missing_messages: list[OldJsonExport] = []
53-
for message in data["messages"]:
54-
try:
55-
pr_data[package]["messages"].remove(message)
56-
except ValueError:
57-
package_missing_messages.append(message)
58-
missing_messages_data[package] = PackageData(
59-
commit=pr_data[package]["commit"], messages=package_missing_messages
60-
)
61-
return missing_messages_data, pr_data
62-
63-
@staticmethod
64-
def _load_json(file_path: Path | str) -> PackageMessages:
65-
with open(file_path, encoding="utf-8") as f:
66-
result: PackageMessages = json.load(f)
67-
return result
68-
69-
def _create_comment(
70-
self, all_missing_messages: PackageMessages, all_new_messages: PackageMessages
71-
) -> str:
23+
def _create_comment(self, comparator: Comparator) -> str:
7224
comment = ""
73-
for package, missing_messages in all_missing_messages.items():
25+
for package, missing_messages, new_messages in comparator:
7426
if len(comment) >= MAX_GITHUB_COMMENT_LENGTH:
7527
break
76-
new_messages = all_new_messages[package]
77-
if not missing_messages["messages"] and not new_messages["messages"]:
78-
continue
7928
comment += self._create_comment_for_package(
8029
package, new_messages, missing_messages
8130
)
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{ "package": "astroid", "missing": 2, "new": 0 }]

tests/testutils/_primer/fixtures/batched/main_batch0.json renamed to tests/testutils/_primer/batched_cases/main_batch0.json

File renamed without changes.

tests/testutils/_primer/fixtures/batched/main_batch1.json renamed to tests/testutils/_primer/batched_cases/main_batch1.json

File renamed without changes.

tests/testutils/_primer/fixtures/batched/pr_batch0.json renamed to tests/testutils/_primer/batched_cases/pr_batch0.json

File renamed without changes.

tests/testutils/_primer/fixtures/batched/pr_batch1.json renamed to tests/testutils/_primer/batched_cases/pr_batch1.json

File renamed without changes.

tests/testutils/_primer/fixtures/both_empty/expected.txt renamed to tests/testutils/_primer/cases/both_empty/expected.txt

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)