Added loaderon-hooks logic

This commit is contained in:
Alvaro Andrés Rodríguez Scelza 2019-06-02 02:10:25 -03:00
parent 0b70e285e3
commit 0a66e6635d
23 changed files with 612 additions and 0 deletions

View file

@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
import argparse
from abc import abstractmethod
from argparse import ArgumentTypeError
from pre_commit_logic.util.check_failed_exception import CheckFailedException
class CheckerTemplateMethod(object):
def __init__(self, argv):
self.parser = argparse.ArgumentParser()
self._add_arguments_to_parser()
self.args = self.parser.parse_args(argv)
self._check_arguments()
def _add_arguments_to_parser(self):
pass
def _check_arguments(self):
pass
def run(self):
"""Prepare args and then check locations."""
try:
self._perform_checks()
return 0
except ArgumentTypeError as ex:
print ex.message
return 1
except CheckFailedException as ex:
print ex.message
return 2
@abstractmethod
def _perform_checks(self):
pass
def inform_check_failure(self, message):
raise CheckFailedException(message)
def check_arguments_size_match(self, arguments_one, arguments_two):
if not arguments_one:
arguments_one = []
if not arguments_two:
arguments_two = []
if len(arguments_one) != len(arguments_two):
self.inform_check_failure('Las listas de argumentos no tienen el mismo largo.')

View file

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from abc import abstractmethod
from pre_commit_logic.util.template_methods.files_bunches_checker_template_method import \
FileBunchesCheckerTemplateMethod
from pre_commit_logic.util.template_methods.lines_checker_template_method import LinesCheckerTemplateMethod
class FileBunchesLinesCheckerTemplateMethod(FileBunchesCheckerTemplateMethod, LinesCheckerTemplateMethod):
@abstractmethod
def _get_regexp(self):
pass
def _check_bunch(self):
"""
This method uses LinesCheckerTemplateMethod's _check_lines. Which receives self._file_lines. In this case, our
'file lines' will be the bunches of lines got by split_by_classes.
"""
self._file_lines = self._bunch_of_lines
self._check_lines()
@abstractmethod
def _check_line(self):
pass

View file

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from abc import abstractmethod
from pre_commit_logic.util.template_methods.checker_template_method import CheckerTemplateMethod
class FileCheckerTemplateMethod(CheckerTemplateMethod):
def __init__(self, argv):
super(FileCheckerTemplateMethod, self).__init__(argv)
self.filename = ''
def _add_arguments_to_parser(self):
self.parser.add_argument('filenames', nargs='*')
def _perform_checks(self):
"""For each file, check it's location."""
for self.filename in self.args.filenames:
self._check_file()
@abstractmethod
def _check_file(self):
pass

View file

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from abc import abstractmethod
from pre_commit_logic.util.file_helpers import split_by_regexp
from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod
class FileBunchesCheckerTemplateMethod(FileCheckerTemplateMethod):
def __init__(self, argv):
super(FileBunchesCheckerTemplateMethod, self).__init__(argv)
self._bunch_of_lines = []
def _check_file(self):
regexp = self._get_regexp()
bunches_of_lines = split_by_regexp(self.filename, regexp)
for self._bunch_of_lines in bunches_of_lines:
self._check_bunch()
@abstractmethod
def _get_regexp(self):
pass
@abstractmethod
def _check_bunch(self):
pass

View file

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from abc import abstractmethod
from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod
from pre_commit_logic.util.file_helpers import read_file_lines
class LinesCheckerTemplateMethod(FileCheckerTemplateMethod):
def __init__(self, argv):
super(LinesCheckerTemplateMethod, self).__init__(argv)
self._file_lines = []
self._file_line = ''
self._file_line_index = 0
def _check_file(self):
self._file_lines = read_file_lines(self.filename)
self._check_lines()
def _check_lines(self):
for self._file_line_index, self._file_line in enumerate(self._file_lines):
self._check_line()
@abstractmethod
def _check_line(self):
pass