Skip to content

Commit 4d44f1c

Browse files
authored
refactor: removes deprecated report extension 'json'.
Refs: #205.
1 parent 01996c9 commit 4d44f1c

File tree

7 files changed

+11
-126
lines changed

7 files changed

+11
-126
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
UTIL_VERSION := 0.5.6
1+
UTIL_VERSION := 0.5.7
22
UTIL_NAME := codeplag
33
PWD := $(shell pwd)
44

src/codeplag/handlers/check.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from codeplag.getfeatures import AbstractGetter
3030
from codeplag.logger import codeplag_logger as logger
3131
from codeplag.pyplag.utils import PyFeaturesGetter
32-
from codeplag.reporters import CSVReporter, JSONReporter
32+
from codeplag.reporters import CSVReporter
3333
from codeplag.types import (
3434
ASTFeatures,
3535
CompareInfo,
@@ -92,7 +92,10 @@ def __init__(
9292
reports = settings_conf.get("reports")
9393
if reports is not None:
9494
reports_extension = settings_conf["reports_extension"]
95-
Reporter = CSVReporter if reports_extension == "csv" else JSONReporter
95+
if reports_extension == "csv":
96+
Reporter = CSVReporter
97+
else:
98+
raise ValueError(f"Unsupported reports extension '{reports_extension}'.")
9699
self.reporter = Reporter(reports)
97100
else:
98101
self.reporter = None

src/codeplag/reporters.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""This module contains logic for saving a comparison result into JSON or CSV."""
22

33
import json
4-
import uuid
54
from abc import ABC, abstractmethod
65
from datetime import datetime
76
from pathlib import Path
@@ -11,15 +10,13 @@
1110
import pandas as pd
1211
from typing_extensions import Self
1312

14-
from codeplag.config import write_config
1513
from codeplag.consts import CSV_REPORT_COLUMNS, CSV_REPORT_FILENAME, CSV_SAVE_TICK_SEC
1614
from codeplag.logger import codeplag_logger as logger
1715
from codeplag.types import (
1816
ASTFeatures,
1917
CompareInfo,
2018
FastMetrics,
2119
StructuresInfo,
22-
WorksReport,
2320
)
2421

2522

@@ -107,40 +104,6 @@ def get_compare_result_from_cache(
107104
return deserialize_compare_result(cache_val.iloc[0])
108105

109106

110-
# DEPRECATED
111-
class JSONReporter(AbstractReporter):
112-
def save_result(
113-
self: Self,
114-
first_work: ASTFeatures,
115-
second_work: ASTFeatures,
116-
compare_info: CompareInfo,
117-
) -> None:
118-
if not self.reports.is_dir():
119-
logger.error("The folder for reports isn't exists.")
120-
return
121-
assert compare_info.structure is not None
122-
123-
struct_info_dict = compare_info.structure._asdict()
124-
struct_info_dict["compliance_matrix"] = struct_info_dict["compliance_matrix"].tolist()
125-
report = WorksReport(
126-
date=_get_current_date(),
127-
first_path=first_work.filepath.__str__(),
128-
first_modify_date=first_work.modify_date,
129-
second_path=second_work.filepath.__str__(),
130-
second_modify_date=second_work.modify_date,
131-
first_heads=first_work.head_nodes,
132-
second_heads=second_work.head_nodes,
133-
fast=compare_info.fast._asdict(),
134-
structure=struct_info_dict,
135-
)
136-
137-
try:
138-
report_file = self.reports / f"{uuid.uuid4().hex}.json"
139-
write_config(report_file, report)
140-
except PermissionError:
141-
logger.error("Not enough rights to write reports to the folder.")
142-
143-
144107
def read_df(path: Path) -> pd.DataFrame:
145108
return pd.read_csv(path, sep=";", index_col=0, dtype=object) # type: ignore
146109

src/codeplag/types.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Flag = Literal[0, 1]
2222
Mode = Literal["many_to_many", "one_to_one"]
2323
NgramsLength = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
24-
ReportsExtension = Literal["json", "csv"]
24+
ReportsExtension = Literal["csv"]
2525
ReportType = Literal["general", "sources"]
2626
Language = Literal["en", "ru"]
2727
LogLevel = Literal["debug", "info", "warning", "error"]
@@ -130,19 +130,6 @@ class CompareInfo(NamedTuple):
130130
structure: StructuresInfo | None = None
131131

132132

133-
# TODO: Rework it structure
134-
class WorksReport(TypedDict):
135-
date: str
136-
first_path: str
137-
second_path: str
138-
first_modify_date: str
139-
second_modify_date: str
140-
first_heads: list[str]
141-
second_heads: list[str]
142-
fast: dict[str, int] # dict from FastMetrics
143-
structure: dict # dict from StructuresInfo
144-
145-
146133
# Exceptions
147134
# ----------------------------------------------------------------------------
148135

test/auto/functional/test_check.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
from __future__ import annotations
22

3-
import json
43
import os
5-
import re
64

75
import pytest
8-
from const import REPORTS_FOLDER
96
from utils import modify_settings, run_check, run_util
107

118
from codeplag.consts import CONFIG_PATH, UTIL_NAME, UTIL_VERSION
12-
from codeplag.types import WorksReport
139

1410
CWD = os.getcwd()
1511
CPP_FILES = [
@@ -145,23 +141,3 @@ def test_check_failed_when_path_regexp_provided_without_required_args(
145141

146142
result.assert_failed()
147143
assert result.cmd_res.returncode == 2
148-
149-
150-
def test_save_reports(create_reports_folder: None):
151-
modify_settings(reports=REPORTS_FOLDER, reports_extension="json").assert_success()
152-
run_check(
153-
[
154-
"--directories",
155-
"./src",
156-
]
157-
).assert_success()
158-
reports_files = os.listdir(REPORTS_FOLDER)
159-
160-
assert len(reports_files) > 0
161-
for file in reports_files:
162-
assert re.search(".*[.]json$", file)
163-
filepath = f"{REPORTS_FOLDER}/{file}"
164-
with open(filepath, "r") as f:
165-
report = json.loads(f.read())
166-
for key in set(WorksReport.__annotations__.keys()):
167-
assert key in report

test/auto/functional/test_settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class TestSettingsModify:
2323
@pytest.mark.parametrize(
2424
"env,reports,threshold,ngrams_length,show_progress,reports_extension,language,log_level,workers",
2525
[
26-
(f"src/{UTIL_NAME}/types.py", "src", 83, 2, 0, "json", "en", "debug", 1),
26+
(f"src/{UTIL_NAME}/types.py", "src", 83, 2, 0, "csv", "en", "debug", 1),
2727
("setup.py", "test", 67, 3, 1, "csv", "ru", "info", os.cpu_count() or 1),
28-
(f"src/{UTIL_NAME}/utils.py", "debian", 93, 4, 0, "json", "en", "warning", 1),
28+
(f"src/{UTIL_NAME}/utils.py", "debian", 93, 4, 0, "csv", "en", "warning", 1),
2929
],
3030
)
3131
def test_modify_settings(

test/unit/codeplag/test_reporters.py

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from unittest.mock import MagicMock, call
2+
from unittest.mock import MagicMock
33

44
import pandas as pd
55
import pytest
@@ -8,11 +8,10 @@
88

99
from codeplag.consts import CSV_REPORT_COLUMNS, CSV_REPORT_FILENAME
1010
from codeplag.handlers.report import deserialize_compare_result
11-
from codeplag.reporters import CSVReporter, JSONReporter
11+
from codeplag.reporters import CSVReporter
1212
from codeplag.types import (
1313
ASTFeatures,
1414
CompareInfo,
15-
WorksReport,
1615
)
1716

1817

@@ -26,49 +25,6 @@ def mock_write_config(mocker: MockerFixture) -> MagicMock:
2625
return mocker.patch("codeplag.reporters.write_config")
2726

2827

29-
class TestJSONReporter:
30-
REPORTER = JSONReporter(Path("."))
31-
32-
def test_save_result_not_occurred_due_absent_dir(
33-
self: Self,
34-
mock_default_logger: MagicMock,
35-
first_features: ASTFeatures,
36-
second_features: ASTFeatures,
37-
first_compare_result: CompareInfo,
38-
) -> None:
39-
self.REPORTER.reports = Path("/bad_directory")
40-
self.REPORTER.save_result(first_features, second_features, first_compare_result)
41-
assert mock_default_logger.error.call_args == call("The folder for reports isn't exists.")
42-
43-
def test_save_result_not_occurred_due_permission_error(
44-
self: Self,
45-
mocker: MockerFixture,
46-
mock_default_logger: MagicMock,
47-
first_features: ASTFeatures,
48-
second_features: ASTFeatures,
49-
first_compare_result: CompareInfo,
50-
) -> None:
51-
mocker.patch.object(Path, "open", side_effect=PermissionError)
52-
self.REPORTER.reports = Path("/etc")
53-
self.REPORTER.save_result(first_features, second_features, first_compare_result)
54-
Path.open.assert_called_once()
55-
assert mock_default_logger.error.call_args == call(
56-
"Not enough rights to write reports to the folder."
57-
)
58-
59-
def test_save_result_with_modify_date(
60-
self: Self,
61-
mock_write_config: MagicMock,
62-
first_features: ASTFeatures,
63-
second_features: ASTFeatures,
64-
first_compare_result: CompareInfo,
65-
) -> None:
66-
mock_write_config.reset_mock()
67-
self.REPORTER.save_result(first_features, second_features, first_compare_result)
68-
mock_write_config.assert_called_once()
69-
assert mock_write_config.call_args[0][1].keys() == WorksReport.__annotations__.keys()
70-
71-
7228
class TestCSVReporter:
7329
REPORTER = CSVReporter(Path("./src"))
7430

0 commit comments

Comments
 (0)