Skip to content

Commit

Permalink
Code refactoring and fix issues (#141)
Browse files Browse the repository at this point in the history
* Code refactoring - move src package into hyperstyle package
* Fix issue#129 and issue #139
  • Loading branch information
nbirillo authored Oct 6, 2021
1 parent 44e5025 commit bf3c6e2
Show file tree
Hide file tree
Showing 103 changed files with 374 additions and 285 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ jobs:
- name: Sort whitelists
run: |
for file in "whitelist.txt" "src/python/review/inspectors/flake8/whitelist.txt"
for file in "whitelist.txt" "hyperstyle/src/python/review/inspectors/flake8/whitelist.txt"
do
LC_ALL=C sort $file -o $file
done
- name: Commit sorted whitelists
uses: EndBug/[email protected]
with:
add: "['whitelist.txt', 'src/python/review/inspectors/flake8/whitelist.txt']"
add: "['whitelist.txt', 'hyperstyle/src/python/review/inspectors/flake8/whitelist.txt']"
message: 'Sort whitelists (Github Actions)'

- name: Set up Eslint
Expand All @@ -57,7 +57,7 @@ jobs:
- name: Test with pytest
run: |
pytest
pytest -vv
- name: Install review module
run: |
Expand Down
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include VERSION.md
include requirements.txt
recursive-exclude __pycache__ *.pyc *.pyo *.orig
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Or use the public docker image, that we use in the [build.yml](.github/workflows

## Usage

Run the [run_tool.py](./src/python/review/run_tool.py) with the arguments.
Run the [run_tool.py](hyperstyle/src/python/review/run_tool.py) with the arguments.

A simple configuration: `python run_tool.py <path>`.

Expand Down
2 changes: 1 addition & 1 deletion VERSION.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1
1.2.2
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from enum import Enum, unique
from typing import List, Optional

from src.python.review.application_config import LanguageVersion
from src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.application_config import LanguageVersion
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType


@unique
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from enum import Enum, unique
from typing import Dict, List, Optional, Set

from src.python.review.common.file_system import Extension
from src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.common.file_system import Extension
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType


@dataclass
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from typing import Union

from src.python.review.common.file_system import Encoding, Extension
from hyperstyle.src.python.review.common.file_system import Encoding, Extension

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from pathlib import Path
from typing import List

from src.python.review.application_config import LanguageVersion
from src.python.review.common.file_system import Extension
from hyperstyle.src.python.review.application_config import LanguageVersion
from hyperstyle.src.python.review.common.file_system import Extension


@unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from pathlib import Path
from typing import List

from src.python.review.application_config import ApplicationConfig
from src.python.review.inspectors.base_inspector import BaseInspector
from src.python.review.inspectors.issue import BaseIssue
from hyperstyle.src.python.review.application_config import ApplicationConfig
from hyperstyle.src.python.review.inspectors.base_inspector import BaseInspector
from hyperstyle.src.python.review.inspectors.issue import BaseIssue

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from pathlib import Path
from typing import Any, Dict, List

from src.python.review.inspectors.inspector_type import InspectorType
from src.python.review.inspectors.issue import BaseIssue
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.inspectors.issue import BaseIssue


class BaseInspector(abc.ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from pathlib import Path
from typing import Any, Dict, List

from src.python.review.common.file_system import check_set_up_env_variable, new_temp_dir
from src.python.review.common.subprocess_runner import run_in_subprocess
from src.python.review.inspectors.base_inspector import BaseInspector
from src.python.review.inspectors.checkstyle.issue_types import CHECK_CLASS_NAME_TO_ISSUE_TYPE
from src.python.review.inspectors.inspector_type import InspectorType
from src.python.review.inspectors.issue import BaseIssue, IssueDifficulty, IssueType
from src.python.review.inspectors.parsers.xml_parser import parse_xml_file_result
from hyperstyle.src.python.review.common.file_system import check_set_up_env_variable, new_temp_dir
from hyperstyle.src.python.review.common.subprocess_runner import run_in_subprocess
from hyperstyle.src.python.review.inspectors.base_inspector import BaseInspector
from hyperstyle.src.python.review.inspectors.checkstyle.issue_types import CHECK_CLASS_NAME_TO_ISSUE_TYPE
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.inspectors.issue import BaseIssue, IssueDifficulty, IssueType
from hyperstyle.src.python.review.inspectors.parsers.xml_parser import parse_xml_file_result
from hyperstyle.src.python.review.inspectors.tips import get_magic_number_tip

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -39,6 +40,11 @@ class CheckstyleInspector(BaseInspector):

'LineLengthCheck':
r'Line is longer than \d+ characters \(found (\d+)\)',

}

origin_class_to_description = {
'MagicNumberCheck': get_magic_number_tip(),
}

@classmethod
Expand All @@ -59,7 +65,8 @@ def inspect(self, path: Path, config: Dict[str, Any]) -> List[BaseIssue]:
self.inspector_type,
self.choose_issue_type,
IssueDifficulty.get_by_issue_type,
self.origin_class_to_pattern)
self.origin_class_to_pattern,
self.origin_class_to_description)

@classmethod
def choose_issue_type(cls, check_class: str) -> IssueType:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from src.python.review.inspectors.issue import IssueType
from hyperstyle.src.python.review.inspectors.issue import IssueType

CHECK_CLASS_NAME_TO_ISSUE_TYPE: Dict[str, IssueType] = {
# ---- Annotations ----
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from pathlib import Path
from typing import Any, Dict, List

from src.python.review.common.file_system import check_set_up_env_variable, new_temp_dir
from src.python.review.common.subprocess_runner import run_in_subprocess
from src.python.review.inspectors.base_inspector import BaseInspector
from src.python.review.inspectors.detekt.issue_types import DETEKT_CLASS_NAME_TO_ISSUE_TYPE
from src.python.review.inspectors.inspector_type import InspectorType
from src.python.review.inspectors.issue import BaseIssue, IssueDifficulty, IssueType
from src.python.review.inspectors.parsers.xml_parser import parse_xml_file_result
from hyperstyle.src.python.review.common.file_system import check_set_up_env_variable, new_temp_dir
from hyperstyle.src.python.review.common.subprocess_runner import run_in_subprocess
from hyperstyle.src.python.review.inspectors.base_inspector import BaseInspector
from hyperstyle.src.python.review.inspectors.detekt.issue_types import DETEKT_CLASS_NAME_TO_ISSUE_TYPE
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.inspectors.issue import BaseIssue, IssueDifficulty, IssueType
from hyperstyle.src.python.review.inspectors.parsers.xml_parser import parse_xml_file_result

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -58,7 +58,8 @@ def inspect(self, path: Path, config: Dict[str, Any]) -> List[BaseIssue]:
self.inspector_type,
self.choose_issue_type,
IssueDifficulty.get_by_issue_type,
self.origin_class_to_pattern)
self.origin_class_to_pattern,
{})

@classmethod
def choose_issue_type(cls, issue_class: str) -> IssueType:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from src.python.review.inspectors.issue import IssueType
from hyperstyle.src.python.review.inspectors.issue import IssueType

DETEKT_CLASS_NAME_TO_ISSUE_TYPE: Dict[str, IssueType] = {
# Comments
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from pathlib import Path
from typing import Any, Dict, List

from src.python.review.common.file_system import new_temp_dir
from src.python.review.common.subprocess_runner import run_in_subprocess
from src.python.review.inspectors.base_inspector import BaseInspector
from src.python.review.inspectors.eslint.issue_types import ESLINT_CLASS_NAME_TO_ISSUE_TYPE
from src.python.review.inspectors.inspector_type import InspectorType
from src.python.review.inspectors.issue import BaseIssue, IssueDifficulty, IssueType
from src.python.review.inspectors.parsers.xml_parser import parse_xml_file_result
from hyperstyle.src.python.review.common.file_system import new_temp_dir
from hyperstyle.src.python.review.common.subprocess_runner import run_in_subprocess
from hyperstyle.src.python.review.inspectors.base_inspector import BaseInspector
from hyperstyle.src.python.review.inspectors.eslint.issue_types import ESLINT_CLASS_NAME_TO_ISSUE_TYPE
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.inspectors.issue import BaseIssue, IssueDifficulty, IssueType
from hyperstyle.src.python.review.inspectors.parsers.xml_parser import parse_xml_file_result

PATH_ESLINT_CONFIG = Path(__file__).parent / '.eslintrc'

Expand Down Expand Up @@ -42,7 +42,8 @@ def inspect(self, path: Path, config: Dict[str, Any]) -> List[BaseIssue]:
self.inspector_type,
self.choose_issue_type,
IssueDifficulty.get_by_issue_type,
self.origin_class_to_pattern)
self.origin_class_to_pattern,
{})

output_path.unlink()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from src.python.review.inspectors.issue import IssueType
from hyperstyle.src.python.review.inspectors.issue import IssueType

ESLINT_CLASS_NAME_TO_ISSUE_TYPE: Dict[str, IssueType] = {
# Possible errors (according to Eslint doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ignore=W291, # trailing whitespaces
WPS125, # Forbid variable or module names which shadow builtin names. TODO: Collision with flake8-builtins
# WPS: Complexity
WPS200, # Forbid modules with complex lines. (Jones Complexity)
WPS210, # Too many local variables
WPS221, # Forbid complex lines. (Jones Complexity)
# WPS: Consistency
WPS303, # Forbid underscores in numbers.
Expand All @@ -49,6 +50,7 @@ ignore=W291, # trailing whitespaces
WPS430, # Forbid nested functions.
WPS431, # Forbid nested classes.
WPS435, # Forbid multiplying lists.
WPS510, # Found `in` used with a non-set container
# WPS: Refactoring
WPS518, # Forbid implicit enumerate() calls. TODO: Collision with "C0200"
WPS527, # Require tuples as arguments for frozenset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from pathlib import Path
from typing import Any, Dict, List

from src.python.review.common.subprocess_runner import run_in_subprocess
from src.python.review.inspectors.base_inspector import BaseInspector
from src.python.review.inspectors.common import convert_percentage_of_value_to_lack_of_value
from src.python.review.inspectors.flake8.issue_types import CODE_PREFIX_TO_ISSUE_TYPE, CODE_TO_ISSUE_TYPE
from src.python.review.inspectors.inspector_type import InspectorType
from src.python.review.inspectors.issue import (
from hyperstyle.src.python.review.common.subprocess_runner import run_in_subprocess
from hyperstyle.src.python.review.inspectors.base_inspector import BaseInspector
from hyperstyle.src.python.review.inspectors.common import convert_percentage_of_value_to_lack_of_value
from hyperstyle.src.python.review.inspectors.flake8.issue_types import CODE_PREFIX_TO_ISSUE_TYPE, CODE_TO_ISSUE_TYPE
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.inspectors.issue import (
BaseIssue,
CodeIssue,
CohesionIssue,
Expand All @@ -18,7 +18,9 @@
IssueType,
LineLenIssue,
)
from src.python.review.inspectors.tips import get_cyclomatic_complexity_tip, get_line_len_tip
from hyperstyle.src.python.review.inspectors.tips import (
get_cohesion_tip, get_cyclomatic_complexity_tip, get_line_len_tip, get_magic_number_tip,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -80,7 +82,7 @@ def parse(cls, output: str) -> List[BaseIssue]:
issues.append(CyclomaticComplexityIssue(**issue_data))
elif cohesion_match is not None: # flake8-cohesion
issue_type = IssueType.COHESION
issue_data[IssueData.DESCRIPTION.value] = description # TODO: Add tip
issue_data[IssueData.DESCRIPTION.value] = f'{get_cohesion_tip(f"{description.capitalize()}.")}'
issue_data[IssueData.COHESION_LACK.value] = convert_percentage_of_value_to_lack_of_value(
float(cohesion_match.group(1)),
)
Expand All @@ -98,7 +100,11 @@ def parse(cls, output: str) -> List[BaseIssue]:
issue_type = cls.choose_issue_type(origin_class)
issue_data[IssueData.ISSUE_TYPE.value] = issue_type
issue_data[IssueData.DIFFICULTY.value] = IssueDifficulty.get_by_issue_type(issue_type)
issue_data[IssueData.DESCRIPTION.value] = description
# Magic number
if origin_class == 'WPS432':
issue_data[IssueData.DESCRIPTION.value] = get_magic_number_tip(description)
else:
issue_data[IssueData.DESCRIPTION.value] = description
issues.append(CodeIssue(**issue_data))

return issues
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from src.python.review.inspectors.issue import IssueType
from hyperstyle.src.python.review.inspectors.issue import IssueType

CODE_TO_ISSUE_TYPE: Dict[str, IssueType] = {
'E800': IssueType.BEST_PRACTICES, # flake8-eradicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from typing import Any, Dict, List, Union

from src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import Any, Callable, Dict, List, Optional
from xml.etree import ElementTree

from src.python.review.common.file_system import get_content_from_file
from src.python.review.inspectors.inspector_type import InspectorType
from src.python.review.inspectors.issue import (
from hyperstyle.src.python.review.common.file_system import get_content_from_file
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.inspectors.issue import (
BaseIssue,
BoolExprLenIssue,
CodeIssue,
Expand All @@ -17,7 +17,7 @@
IssueType,
LineLenIssue,
)
from src.python.review.inspectors.tips import (
from hyperstyle.src.python.review.inspectors.tips import (
get_bool_expr_len_tip,
get_cyclomatic_complexity_tip,
get_func_len_tip,
Expand Down Expand Up @@ -102,6 +102,7 @@ def parse_xml_file_result(
inspector_type: InspectorType,
issue_type_selector: Callable[[str], IssueType],
difficulty_selector: Callable[[IssueType], IssueDifficulty],
origin_class_to_pattern: Dict[str, str],
origin_class_to_description: Dict[str, str]) -> List[BaseIssue]:
"""
Parses the output, which is a xml file, and returns a list of the issues found there.
Expand Down Expand Up @@ -140,14 +141,14 @@ def parse_xml_file_result(
issue_data[IssueData.ISSUE_TYPE.value] = issue_type
issue_data[IssueData.DIFFICULTY.value] = difficulty_selector(issue_type)

if origin_class in origin_class_to_description:
pattern = origin_class_to_description.get(origin_class)
if origin_class in origin_class_to_pattern:
pattern = origin_class_to_pattern.get(origin_class)
measure_value = int(re.search(pattern, message,
flags=re.IGNORECASE).groups()[0])

issue = __parse_measurable_issue(issue_data, issue_type, measure_value)
else:
issue_data[IssueData.DESCRIPTION.value] = message
issue_data[IssueData.DESCRIPTION.value] = origin_class_to_description.get(origin_class, message)
issue = CodeIssue(**issue_data)

if issue is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@
<rule ref="category/java/errorprone.xml/MethodWithSameNameAsEnclosingClass"/>
<rule ref="category/java/errorprone.xml/MisplacedNullCheck"/>
<rule ref="category/java/errorprone.xml/MissingBreakInSwitch"/>
<rule ref="category/java/errorprone.xml/MissingSerialVersionUID"/>
<rule ref="category/java/errorprone.xml/MissingStaticMethodInNonInstantiatableClass"/>
<rule ref="category/java/errorprone.xml/NonCaseLabelInSwitchStatement"/>
<rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from src.python.review.inspectors.issue import IssueType
from hyperstyle.src.python.review.inspectors.issue import IssueType

PMD_RULE_TO_ISSUE_TYPE: Dict[str, IssueType] = {
# ---- Best Practices ----
Expand Down
Loading

0 comments on commit bf3c6e2

Please sign in to comment.