Skip to content

Commit

Permalink
Add a new optional argument (#179)
Browse files Browse the repository at this point in the history
* Add language parameter to inspect only one specific language (#178)

* Add language parameter to inspect only one specific language

* Fix linters

* Move language version enum to separated module to avoid circular imports in application_config

* Move language assignment to ApplicationConfig

* Fix flake

* Fix imports order

* Fix tests

* Update actions/checkout

* Fix docker image

* Add new argument into readme file

---------

Co-authored-by: Artur <atnartur@users.noreply.github.com>
  • Loading branch information
nbirillo and atnartur committed May 30, 2023
1 parent 0a1eafb commit f0d4110
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ ignore =
T001,
# Subprocess call
S603,
# Import statements are in the wrong order.
I100,

exclude = .git, */data, env/, venv/

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
# Consistent with base image in Dockerfile
# container: stepik/hyperstyle-base:py3.8.11-java11.0.11-node14.17.3-go1.18.5
container: stepik/hyperstyle:1.4.2
container: nastyabirillo/hyperstyle:1.4.3

steps:
- name: Install git
Expand All @@ -23,7 +23,7 @@ jobs:
echo $PMD_DIRECTORY && echo PMD_VERSION
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install requirements
run: |
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/i

# Install third party golang libraries and pre-cache them by compiling main.go
# Taken from: https://github.com/StepicOrg/epicbox-images/blob/a5eadb5211909fc7ef99724ee0b8bf3a758ae1b7/epicbox-go/Dockerfile
RUN curl -sSfLO https://raw.githubusercontent.com/StepicOrg/epicbox-images/master/epicbox-go/go.mod && \
curl -sSfLO https://raw.githubusercontent.com/StepicOrg/epicbox-images/master/epicbox-go/go.sum && \
curl -sSfLO https://raw.githubusercontent.com/StepicOrg/epicbox-images/master/epicbox-go/main.go && \
RUN curl -sSfLO https://raw.githubusercontent.com/StepicOrg/epicbox-images/a5eadb5211909fc7ef99724ee0b8bf3a758ae1b7/epicbox-go/go.mod && \
curl -sSfLO https://raw.githubusercontent.com/StepicOrg/epicbox-images/a5eadb5211909fc7ef99724ee0b8bf3a758ae1b7/epicbox-go/go.sum && \
curl -sSfLO https://raw.githubusercontent.com/StepicOrg/epicbox-images/a5eadb5211909fc7ef99724ee0b8bf3a758ae1b7/epicbox-go/main.go && \
go mod download && \
go mod verify && \
go mod tidy && \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Argument | Description
**&#8209;&#8209;history** | JSON string with a list of issues for each language. For each issue its class and quantity are specified. Example: `--history "{\"python\": [{\"origin_class\": \"SC200\", \"number\": 20}, {\"origin_class\": \"WPS314\", \"number\": 3}]}"`
**&#8209;&#8209;with&#8209;all&#8209;categories** | Without this flag, all issues will be categorized into 5 main categories: `CODE_STYLE`, `BEST_PRACTICES`, `ERROR_PRONE`, `COMPLEXITY`, `INFO`.
**&#8209;&#8209;group&#8209;by&#8209;difficulty** | With this flag, the final grade and influence on penalty will be grouped by the issue difficulty.
**&#8209;&#8209;language** | Specify the language to inspect. The tool will check all languages by default. The default value is `None`.

The output examples:

Expand Down
2 changes: 1 addition & 1 deletion VERSION.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.2
1.4.3
10 changes: 8 additions & 2 deletions hyperstyle/src/python/common/tool_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from enum import Enum, unique
from typing import List, Optional

from hyperstyle.src.python.review.application_config import LanguageVersion
from hyperstyle.src.python.review.common.language import Language
from hyperstyle.src.python.review.common.language_version import LanguageVersion
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType


Expand Down Expand Up @@ -50,8 +51,13 @@ class RunToolArgument(Enum):
'Allow duplicate issues found by different linters. '
'By default, duplicates are skipped.')

LANG = ArgumentsInfo(None, '--language',
'Specify the language to inspect. The tool will check all languages by default. '
'Available values are: '
f'{", ".join([l.lower() for l in Language.values()])}.')

LANG_VERSION = ArgumentsInfo(None, '--language-version',
'Specify the language version for JAVA inspectors.'
'Specify the language version for JAVA inspectors. '
'Available values are: '
f'{LanguageVersion.PYTHON_3.value}, {LanguageVersion.JAVA_8.value}, '
f'{LanguageVersion.JAVA_11.value}, {LanguageVersion.KOTLIN.value}.')
Expand Down
60 changes: 4 additions & 56 deletions hyperstyle/src/python/review/application_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from dataclasses import dataclass
from enum import Enum, unique
from typing import Dict, List, Optional, Set
from typing import Optional, Set

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


Expand All @@ -14,6 +13,7 @@ class ApplicationConfig:
inspectors_config: dict
with_all_categories: bool
start_line: int = 1
language: Optional[Language] = None
end_line: Optional[int] = None
new_format: bool = False
history: Optional[str] = None
Expand All @@ -28,59 +28,7 @@ def get_default_config() -> 'ApplicationConfig':
inspectors_config={'n_cpu': 1},
with_all_categories=True,
start_line=1,
language=None,
new_format=False,
group_by_difficulty=False,
)


@unique
class LanguageVersion(Enum):
JAVA_7 = 'java7'
JAVA_8 = 'java8'
JAVA_9 = 'java9'
JAVA_11 = 'java11'
JAVA_15 = 'java15'
JAVA_17 = 'java17'
PYTHON_3 = 'python3'
KOTLIN = 'kotlin'
JS = 'javascript'
GO = 'go'

@classmethod
def values(cls) -> List[str]:
return [member.value for member in cls.__members__.values()]

@classmethod
def language_to_extension_dict(cls) -> Dict['LanguageVersion', Extension]:
return {
cls.JAVA_7: Extension.JAVA,
cls.JAVA_8: Extension.JAVA,
cls.JAVA_9: Extension.JAVA,
cls.JAVA_11: Extension.JAVA,
cls.JAVA_15: Extension.JAVA,
cls.JAVA_17: Extension.JAVA,
cls.PYTHON_3: Extension.PY,
cls.KOTLIN: Extension.KT,
cls.JS: Extension.JS,
cls.GO: Extension.GO,
}

def extension_by_language(self) -> Extension:
return self.language_to_extension_dict()[self]

def is_java(self) -> bool:
return (
self == LanguageVersion.JAVA_7
or self == LanguageVersion.JAVA_8
or self == LanguageVersion.JAVA_9
or self == LanguageVersion.JAVA_11
or self == LanguageVersion.JAVA_15
or self == LanguageVersion.JAVA_17
)

@classmethod
def from_value(cls, value: str, default=None):
try:
return LanguageVersion(value)
except ValueError:
return default
2 changes: 1 addition & 1 deletion hyperstyle/src/python/review/common/language.py
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 hyperstyle.src.python.review.application_config import LanguageVersion
from hyperstyle.src.python.review.common.file_system import Extension
from hyperstyle.src.python.review.common.language_version import LanguageVersion


@unique
Expand Down
57 changes: 57 additions & 0 deletions hyperstyle/src/python/review/common/language_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from enum import Enum, unique
from typing import Dict, List

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


@unique
class LanguageVersion(Enum):
JAVA_7 = 'java7'
JAVA_8 = 'java8'
JAVA_9 = 'java9'
JAVA_11 = 'java11'
JAVA_15 = 'java15'
JAVA_17 = 'java17'
PYTHON_3 = 'python3'
KOTLIN = 'kotlin'
JS = 'javascript'
GO = 'go'

@classmethod
def values(cls) -> List[str]:
return [member.value for member in cls.__members__.values()]

@classmethod
def language_to_extension_dict(cls) -> Dict['LanguageVersion', Extension]:
return {
cls.JAVA_7: Extension.JAVA,
cls.JAVA_8: Extension.JAVA,
cls.JAVA_9: Extension.JAVA,
cls.JAVA_11: Extension.JAVA,
cls.JAVA_15: Extension.JAVA,
cls.JAVA_17: Extension.JAVA,
cls.PYTHON_3: Extension.PY,
cls.KOTLIN: Extension.KT,
cls.JS: Extension.JS,
cls.GO: Extension.GO,
}

def extension_by_language(self) -> Extension:
return self.language_to_extension_dict()[self]

def is_java(self) -> bool:
return (
self == LanguageVersion.JAVA_7
or self == LanguageVersion.JAVA_8
or self == LanguageVersion.JAVA_9
or self == LanguageVersion.JAVA_11
or self == LanguageVersion.JAVA_15
or self == LanguageVersion.JAVA_17
)

@classmethod
def from_value(cls, value: str, default=None):
try:
return LanguageVersion(value)
except ValueError:
return default
2 changes: 1 addition & 1 deletion hyperstyle/src/python/review/inspectors/pmd/pmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from pathlib import Path
from typing import Any, Dict, List

from hyperstyle.src.python.review.application_config import LanguageVersion
from hyperstyle.src.python.review.common.file_system import check_set_up_env_variable, new_temp_dir
from hyperstyle.src.python.review.common.language_version import LanguageVersion
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.base_issue_converter import convert_base_issue
Expand Down
5 changes: 5 additions & 0 deletions hyperstyle/src/python/review/reviewers/perform_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def perform_review(path: Path, config: ApplicationConfig) -> GeneralReviewResult
logger.error(f'Unsupported language. Extensions {metadata.extensions} for project {path}')
raise UnsupportedLanguage(path, metadata.extensions)
languages = list(metadata.languages.difference({Language.UNKNOWN}))

if config.language is not None:
languages = [config.language]
languages.sort()

return _preform_review(metadata, languages, config)


Expand Down
11 changes: 10 additions & 1 deletion hyperstyle/src/python/review/run_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
sys.path.append('../../../..')

from hyperstyle.src.python.common.tool_arguments import RunToolArgument, VerbosityLevel
from hyperstyle.src.python.review.application_config import ApplicationConfig, LanguageVersion
from hyperstyle.src.python.review.application_config import ApplicationConfig
from hyperstyle.src.python.review.common.language import Language
from hyperstyle.src.python.review.common.language_version import LanguageVersion
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.logging_config import logging_config
from hyperstyle.src.python.review.reviewers.perform_review import (
Expand Down Expand Up @@ -64,6 +66,12 @@ def configure_arguments(parser: argparse.ArgumentParser) -> None:
action='store_true',
help=RunToolArgument.DUPLICATES.value.description)

parser.add_argument(RunToolArgument.LANG.value.long_name,
help=RunToolArgument.LANG.value.description,
default=None,
choices=[lang.lower() for lang in Language.values()],
type=str)

# TODO: deprecated argument: language_version. Delete after several releases.
parser.add_argument('--language_version',
RunToolArgument.LANG_VERSION.value.long_name,
Expand Down Expand Up @@ -158,6 +166,7 @@ def main() -> int:
config = ApplicationConfig(
args.disable, args.allow_duplicates,
n_cpu, inspectors_config,
language=Language(args.language.upper()) if args.language is not None else None,
start_line=start_line,
end_line=args.end_line,
new_format=args.new_format,
Expand Down
2 changes: 1 addition & 1 deletion test/python/inspectors/test_pmd_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List

import pytest
from hyperstyle.src.python.review.application_config import LanguageVersion
from hyperstyle.src.python.review.common.language_version import LanguageVersion
from hyperstyle.src.python.review.inspectors.inspector_type import InspectorType
from hyperstyle.src.python.review.inspectors.issue import CodeIssue, IssueDifficulty, IssueType
from hyperstyle.src.python.review.inspectors.pmd.pmd import DEFAULT_JAVA_VERSION, PMDInspector
Expand Down

0 comments on commit f0d4110

Please sign in to comment.