Skip to content

Commit

Permalink
refactor: fixes type annotations and makes the output of the time spe…
Browse files Browse the repository at this point in the history
…nt on work checks more human-readable. (#200)

Refs: #200.
  • Loading branch information
Artanias authored Sep 13, 2024
1 parent c8ab9c3 commit 7560f26
Show file tree
Hide file tree
Showing 35 changed files with 202 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ default_language_version:
python: python3.10
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
rev: v0.6.4
hooks:
- id: ruff
args: [ --fix ]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UTIL_VERSION := 0.5.2
UTIL_VERSION := 0.5.3
UTIL_NAME := codeplag
PWD := $(shell pwd)

Expand Down
7 changes: 4 additions & 3 deletions docs/notebooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from decouple import Config, RepositoryEnv
from scipy.optimize import curve_fit

from codeplag.algorithms.featurebased import counter_metric, struct_compare
from codeplag.algorithms.stringbased import gst
from codeplag.algorithms.tokenbased import value_jakkar_coef
from codeplag.pyplag.utils import get_ast_from_content, get_features_from_ast
from decouple import Config, RepositoryEnv
from scipy.optimize import curve_fit
from webparsers.github_parser import GitHubParser


Expand Down Expand Up @@ -188,7 +189,7 @@ def plot_and_save_result(

def get_time_algorithms(
df: pd.DataFrame,
work,
work: pd.Series,
iterations: int = 5,
metric: Literal["fast", "gst", "structure"] = "fast",
) -> pd.DataFrame:
Expand Down
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ lint.select = [
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"ANN", # flake8-annotations
"SIM", # flake8-simplify
"ERA", # eradicate
"C90", # mccabe
]
lint.ignore = [
"ANN003", # missing type annotations for `*kwargs`
"ANN101",
"ANN102",
"ANN201",
"ANN204",
"ANN206",
"ANN401", # dynamically typed expressions (typing.Any)
"D100",
"D101",
"D102",
Expand All @@ -25,13 +33,17 @@ lint.ignore = [
"D105",
"D107",
]
lint.exclude = ["*.ipynb"]

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.lint.mccabe]
max-complexity = 12

[tool.ruff.format]
exclude = ["*.ipynb"]

[tool.pyright]
pythonVersion = "3.10"

Expand Down
7 changes: 3 additions & 4 deletions src/codeplag/codeplagcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import builtins
from pathlib import Path

from webparsers.types import GitHubContentUrl

from codeplag.consts import (
DEFAULT_MODE,
DEFAULT_REPORT_TYPE,
Expand All @@ -22,6 +20,7 @@
WORKERS_CHOICE,
)
from codeplag.types import Settings
from webparsers.types import GitHubContentUrl

# FIXME: dirty hook for using logic without translations
builtins.__dict__["_"] = builtins.__dict__.get("_", str)
Expand Down Expand Up @@ -51,7 +50,7 @@ def __call__(
class DirPath(Path):
"""Raises `argparse.ArgumentTypeError` if the directory doesn't exist."""

def __new__(cls, *args, **kwargs):
def __new__(cls, *args: str, **kwargs):
path = Path(*args, **kwargs).resolve()
if not path.is_dir():
raise argparse.ArgumentTypeError(
Expand All @@ -64,7 +63,7 @@ def __new__(cls, *args, **kwargs):
class FilePath(Path):
"""Raises `argparse.ArgumentTypeError` if the file doesn't exist."""

def __new__(cls, *args, **kwargs):
def __new__(cls, *args: str, **kwargs):
path = Path(*args, **kwargs).resolve()
if not path.is_file():
raise argparse.ArgumentTypeError(
Expand Down
2 changes: 1 addition & 1 deletion src/codeplag/cplag/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from typing import Final

from clang.cindex import Config, Cursor, Index, TranslationUnit
from webparsers.types import WorkInfo

from codeplag.consts import FILE_DOWNLOAD_PATH, GET_FRAZE, SUPPORTED_EXTENSIONS
from codeplag.cplag.const import COMPILE_ARGS
from codeplag.cplag.tree import get_features
from codeplag.getfeatures import AbstractGetter, get_files_path_from_directory
from codeplag.logger import log_err
from codeplag.types import ASTFeatures
from webparsers.types import WorkInfo

# FIXME: Dirty hook for finding libclang so file
LIBCLANG_SO_FILE_PATH: Final[Path] = Path("/usr/lib/llvm-14/lib/libclang-14.so.1")
Expand Down
5 changes: 2 additions & 3 deletions src/codeplag/getfeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
from pathlib import Path
from typing import Callable, Literal, ParamSpec, overload

from webparsers.github_parser import GitHubParser
from webparsers.types import WorkInfo

from codeplag.consts import (
ALL_EXTENSIONS,
GET_FRAZE,
UTIL_NAME,
)
from codeplag.types import ASTFeatures, Extension, Extensions
from webparsers.github_parser import GitHubParser
from webparsers.types import WorkInfo


def get_files_path_from_directory(
Expand Down
11 changes: 6 additions & 5 deletions src/codeplag/handlers/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import math
import os
from concurrent.futures import Future, ProcessPoolExecutor
from datetime import timedelta
from itertools import combinations
from pathlib import Path
from time import monotonic
Expand All @@ -13,7 +14,6 @@
from decouple import Config, RepositoryEnv
from numpy.typing import NDArray
from requests import Session
from webparsers.github_parser import GitHubParser

from codeplag.algorithms.compare import compare_works
from codeplag.config import read_settings_conf
Expand All @@ -38,6 +38,7 @@
ProcessingWorksInfo,
Threshold,
)
from webparsers.github_parser import GitHubParser


class WorksComparator:
Expand Down Expand Up @@ -163,7 +164,7 @@ def check(
github_project_folders,
github_user,
)
logger.debug("Time for all %.2fs.", monotonic() - begin_time)
logger.debug("Time for all %s.", timedelta(seconds=monotonic() - begin_time))
logger.info("Ending searching for plagiarism ...")
if isinstance(self.reporter, CSVReporter):
self.reporter._write_df_to_fs()
Expand All @@ -175,7 +176,7 @@ def __many_to_many_check(
features_from_gh_files: list[ASTFeatures],
github_project_folders: list[str],
github_user: str,
):
) -> None:
works: list[ASTFeatures] = []
works.extend(features_from_files)
works.extend(self.features_getter.get_from_dirs(directories))
Expand Down Expand Up @@ -208,7 +209,7 @@ def __one_to_one_check(
features_from_gh_files: list[ASTFeatures],
github_project_folders: list[str],
github_user: str,
):
) -> None:
combined_elements = filter(
bool,
(
Expand Down Expand Up @@ -304,7 +305,7 @@ def _handle_compare_result(
def _handle_completed_futures(
self,
processing: list[ProcessingWorksInfo],
):
) -> None:
for proc_works_info in processing:
metrics: CompareInfo = proc_works_info.compare_future.result()
self._handle_compare_result(
Expand Down
2 changes: 1 addition & 1 deletion src/codeplag/handlers/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def _create_report(
environment: jinja2.Environment,
threshold: int = DEFAULT_THRESHOLD,
language: Language = DEFAULT_LANGUAGE,
):
) -> None:
template = environment.from_string(GENERAL_TEMPLATE_PATH.read_text())
if save_path.is_dir():
save_path = save_path / DEFAULT_GENERAL_REPORT_NAME
Expand Down
2 changes: 1 addition & 1 deletion src/codeplag/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def set_handlers(
logger.addHandler(get_stderr_handler())


def log_err(*msgs) -> None:
def log_err(*msgs: str) -> None:
for msg in msgs:
codeplag_logger.error(msg)

Expand Down
3 changes: 1 addition & 2 deletions src/codeplag/pyplag/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import logging
from pathlib import Path

from webparsers.types import WorkInfo

from codeplag.consts import GET_FRAZE, SUPPORTED_EXTENSIONS
from codeplag.display import red_bold
from codeplag.getfeatures import (
Expand All @@ -15,6 +13,7 @@
from codeplag.logger import log_err
from codeplag.pyplag.astwalkers import ASTWalker
from codeplag.types import ASTFeatures
from webparsers.types import WorkInfo


def get_ast_from_content(code: str, path: Path | str) -> ast.Module | None:
Expand Down
4 changes: 2 additions & 2 deletions src/codeplag/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ def __post_init__(self) -> None:
else:
self.modify_date = ""

def __eq__(self, other) -> bool:
def __eq__(self, other: "ASTFeatures") -> bool:
if not isinstance(other, self.__class__):
raise NotImplementedError
return str(self.filepath) == str(other.filepath)

def __lt__(self, other) -> bool:
def __lt__(self, other: "ASTFeatures") -> bool:
if not isinstance(other, self.__class__):
raise NotImplementedError
return str(self.filepath) < str(other.filepath)
Expand Down
5 changes: 3 additions & 2 deletions test/auto/functional/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import re

import pytest
from codeplag.consts import CONFIG_PATH, UTIL_NAME, UTIL_VERSION
from codeplag.types import WorksReport
from const import REPORTS_FOLDER
from utils import modify_settings, run_check, run_util

from codeplag.consts import CONFIG_PATH, UTIL_NAME, UTIL_VERSION
from codeplag.types import WorksReport

CWD = os.getcwd()
CPP_FILES = [
"test/unit/codeplag/cplag/data/sample1.cpp",
Expand Down
5 changes: 3 additions & 2 deletions test/auto/functional/test_report.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from pathlib import Path

import pytest
from codeplag.consts import DEFAULT_GENERAL_REPORT_NAME, DEFAULT_SOURCES_REPORT_NAME
from codeplag.types import ReportType
from const import REPORTS_FOLDER
from utils import create_report, modify_settings, run_check

from codeplag.consts import DEFAULT_GENERAL_REPORT_NAME, DEFAULT_SOURCES_REPORT_NAME
from codeplag.types import ReportType


@pytest.fixture(scope="function", autouse=True)
def setup(create_reports_folder: None):
Expand Down
3 changes: 2 additions & 1 deletion test/auto/functional/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from typing import Literal

import pytest
from utils import modify_settings

from codeplag.consts import CONFIG_PATH, UTIL_NAME
from codeplag.types import Language, LogLevel, ReportsExtension, Threshold
from utils import modify_settings


@pytest.fixture(scope="module", autouse=True)
Expand Down
3 changes: 2 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import logging

import pytest
from codeplag.consts import UTIL_NAME
from pytest_mock import MockerFixture

from codeplag.consts import UTIL_NAME


@pytest.fixture
def dummy_logger(mocker: MockerFixture):
Expand Down
1 change: 1 addition & 0 deletions test/unit/codeplag/algorithms/test_compare.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from codeplag.algorithms.compare import compare_works, fast_compare
from codeplag.types import ASTFeatures, CompareInfo

Expand Down
1 change: 1 addition & 0 deletions test/unit/codeplag/algorithms/test_featurebased.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest

import numpy as np

from codeplag.algorithms.featurebased import (
add_not_counted,
counter_metric,
Expand Down
1 change: 1 addition & 0 deletions test/unit/codeplag/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import pytest

from codeplag.algorithms.compare import compare_works
from codeplag.pyplag.utils import get_ast_from_filename, get_features_from_ast
from codeplag.types import ASTFeatures, CompareInfo
Expand Down
Loading

0 comments on commit 7560f26

Please sign in to comment.