Skip to content

Commit

Permalink
feat: include fixers to run
Browse files Browse the repository at this point in the history
  • Loading branch information
guilatrova committed Sep 12, 2021
1 parent a0fa22d commit e885b34
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
21 changes: 10 additions & 11 deletions src/tryceratops/fixers/exception_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,29 @@

class BaseFixer(ABC):
violation_code: Tuple[str, str]
fixes_made = 0

@classmethod
def _filter_violations_in_scope(cls, violations: List[Violation]) -> List[Violation]:
scope_code, _ = cls.violation_code
def _filter_violations_in_scope(self, violations: List[Violation]) -> List[Violation]:
scope_code, _ = self.violation_code
relevant = [vio for vio in violations if vio.code == scope_code]
return relevant

@classmethod
@abstractmethod
def _perform_fix(cls, violation: Violation):
def _perform_fix(self, violation: Violation):
pass

@classmethod
def fix(cls, violations: List[Violation]):
relevant_violations = cls._filter_violations_in_scope(violations)
def fix(self, violations: List[Violation]):
relevant_violations = self._filter_violations_in_scope(violations)

for violation in relevant_violations:
cls._perform_fix(violation)
self._perform_fix(violation)
self.fixes_made += 1


class VerboseReraiseFixer(BaseFixer):
violation_code = codes.VERBOSE_RERAISE

@classmethod
def _perform_fix(cls, violation: Violation):
def _perform_fix(self, violation: Violation):
with open(violation.filename, "r+") as file:
all_lines = file.readlines()

Expand Down
3 changes: 3 additions & 0 deletions src/tryceratops/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def _present_status(self):
else:
print("Nothing to check!")

if self.runner.fixes_made:
print(f"Fixed {self.runner.fixes_made} violations")

if self.discovery.had_issues:
print(
wrap_color(f"Failed to process {len(self.discovery.failures)} files", COLORS.ERROR)
Expand Down
11 changes: 9 additions & 2 deletions src/tryceratops/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from tryceratops.analyzers import BaseAnalyzer, get_analyzer_chain
from tryceratops.filters import GlobalFilter
from tryceratops.fixers import VerboseReraiseFixer
from tryceratops.types import ParsedFilesType
from tryceratops.violations import Violation

Expand All @@ -21,8 +22,9 @@ class Runner:
def __init__(self):
self.runtime_errors: List[RuntimeError] = []
self.violations: List[Violation] = []
self.analyzed_files: int = 0
self.excluded_files: int = 0
self.analyzed_files = 0
self.excluded_files = 0
self.fixes_made = 0

def _clear(self):
self.violations = []
Expand Down Expand Up @@ -55,6 +57,11 @@ def analyze(self, trees: ParsedFilesType, global_filter: GlobalFilter) -> List[V
)
self.runtime_errors.append(RuntimeError(filename, type(analyzer), ex))

if global_filter.autofix and self.any_violation:
fixer = VerboseReraiseFixer()
fixer.fix(self.violations)
self.fixes_made += 1

return self.violations

@property
Expand Down

0 comments on commit e885b34

Please sign in to comment.