diff --git a/pre_commit_hooks/loaderon-hooks/__init__.py b/pre_commit_hooks/loaderon-hooks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pre_commit_hooks/loaderon-hooks/general_hooks/__init__.py b/pre_commit_hooks/loaderon-hooks/general_hooks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pre_commit_hooks/loaderon-hooks/general_hooks/check_branch_name.py b/pre_commit_hooks/loaderon-hooks/general_hooks/check_branch_name.py new file mode 100644 index 0000000..e100847 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/general_hooks/check_branch_name.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +import re + +from pre_commit_logic.util.template_methods.checker_template_method import CheckerTemplateMethod +from pre_commit_logic.util.git_helpers import get_current_branch_name + + +class BranchNameChecker(CheckerTemplateMethod): + def _add_arguments_to_parser(self): + self.parser.add_argument('-r', '--regex', help='Regex that git current branch must match.') + + def _perform_checks(self): + regular_expression = self.args.regex + pattern = re.compile(regular_expression) + current_branch_name = get_current_branch_name() + if not pattern.match(current_branch_name): + self.inform_check_failure('El nombre de la rama debe ser del estilo {}'.format(regular_expression)) + + +def main(argv=None): + return BranchNameChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/general_hooks/check_class_docstring.py b/pre_commit_hooks/loaderon-hooks/general_hooks/check_class_docstring.py new file mode 100644 index 0000000..4098e73 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/general_hooks/check_class_docstring.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +import re + +from pre_commit_logic.util.template_methods.lines_checker_template_method import LinesCheckerTemplateMethod + + +class ClassDocstringChecker(LinesCheckerTemplateMethod): + def _check_line(self): + regular_expression = r'^(\t| )*class .+\(.*\):' + pattern = re.compile(regular_expression) + if pattern.match(self._file_line): + self.__check_class_docstring(self._file_line_index) + + def __check_class_docstring(self, class_line_index): + class_first_line = self._file_lines[class_line_index + 1] + if class_first_line in ['\n', '\r\n']: + self.inform_check_failure('El docstring de la clase {} está separado de su clase por una o más líneas en ' + 'blanco.'.format(self._file_lines[class_line_index])) + if not class_first_line.strip().startswith('\"\"\"'): + self.inform_check_failure('La clase {} no tiene docstring.'.format(self._file_lines[class_line_index])) + + +def main(argv=None): + return ClassDocstringChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/general_hooks/check_line.py b/pre_commit_hooks/loaderon-hooks/general_hooks/check_line.py new file mode 100644 index 0000000..6fd4789 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/general_hooks/check_line.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +import re + +from pre_commit_logic.util.template_methods.lines_checker_template_method import LinesCheckerTemplateMethod + + +class LinesChecker(LinesCheckerTemplateMethod): + def _add_arguments_to_parser(self): + super(LinesChecker, self)._add_arguments_to_parser() + self.parser.add_argument('-l', '--line-to-check', action='append', help='Regex to check.') + self.parser.add_argument('-r', '--regexp-to-match', action='append', help='Regex to match.') + + def _check_arguments(self): + self.check_arguments_size_match(self.args.line_to_check, self.args.regexp_to_match) + + def _check_line(self): + for line_index, line_to_check in enumerate(self.args.line_to_check): + line_to_check_pattern = re.compile(line_to_check) + if line_to_check_pattern.match(self._file_line): + line_regexp_to_match = self.args.regexp_to_match[line_index] + correct_pattern = re.compile(line_regexp_to_match) + if not correct_pattern.match(self._file_line): + self.inform_check_failure( + "Una de las líneas con '{}' no está correctamente formulada. Línea {}: \n\n{}\n" + "Debería cumplir la expresión regular: {}".format( + line_to_check, + self._file_line_index + 1, + self._file_line, + line_regexp_to_match + ) + ) + + +def main(argv=None): + return LinesChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/general_hooks/check_location.py b/pre_commit_hooks/loaderon-hooks/general_hooks/check_location.py new file mode 100644 index 0000000..3fe84c9 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/general_hooks/check_location.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +"""Checks files locations.""" +import os +import re + +from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod +from pre_commit_logic.util.string_helpers import matches_any_regexp + + +class LocationChecker(FileCheckerTemplateMethod): + def _add_arguments_to_parser(self): + super(LocationChecker, self)._add_arguments_to_parser() + self.parser.add_argument( + '-ed', '--directories', action='append', + help='Directory regex to be added to white list. Can be set multiple times to add' + 'multiple directories to white list.', + ) + self.parser.add_argument( + '-ef', '--files', action='append', + help="Files regex, separated by a white space, to be added to it's corresponded" + "directory's white list. Order of this argument declaration matches order of" + "--directories declaration, so as to add files to that directory's files whitelist." + "This argument can be therefore be set multiple times so as to match --directories sets.", + ) + + def _check_file(self): + """Check filename location against enabled directories and their enabled files.""" + self.check_arguments_size_match(self.args.directories, self.args.files) + file_directory_path = os.path.dirname(self.filename) + file_name = os.path.basename(self.filename) + location_enabled = False + for directory_regexp in self.args.directories: + pattern = re.compile(directory_regexp) + if pattern.match(file_directory_path): + location_enabled = True + file_enabled = self.file_enabled_for_directory(directory_regexp, file_name) + if not file_enabled: + self.inform_check_failure('El archivo {} no está permitido para el directorio {}' + .format(self.filename, file_directory_path)) + if not location_enabled: + self.inform_check_failure('El directorio {} no está habilitado'.format(file_directory_path)) + + def file_enabled_for_directory(self, directory_regexp, filename): + directory_position_in_arguments = self.args.directories.index(directory_regexp) + directory_files_whitelist = self.args.files[directory_position_in_arguments] + files_reg_expressions = directory_files_whitelist.split(' ') + return matches_any_regexp(filename, files_reg_expressions) + + +def main(argv=None): + return LocationChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/general_hooks/check_using_pylint.py b/pre_commit_hooks/loaderon-hooks/general_hooks/check_using_pylint.py new file mode 100644 index 0000000..83e0265 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/general_hooks/check_using_pylint.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +"""Executes Pylint checks on all git modified and added files.""" +import re + +from pylint.lint import Run +from pylint.reporters.text import TextReporter + +from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod +from pre_commit_logic.util.file_helpers import find_file_starting_from_reference_file_directory + + +class PylintChecker(FileCheckerTemplateMethod): + def __init__(self, argv): + super(PylintChecker, self).__init__(argv) + self.pylint_output = WritableObject() + + def _add_arguments_to_parser(self): + super(PylintChecker, self)._add_arguments_to_parser() + self.parser.add_argument('-e', '--exclude', help='Excluded files to check.') + + def _check_file(self): + try: + self.run_pylint_except_in_excluded_files() + except SystemExit as exception: + self.manage_pylint_result(exception) + + def run_pylint_except_in_excluded_files(self): + excluded_files_regex = self.args.exclude + pattern = re.compile(excluded_files_regex) + if not pattern.match(self.filename): + self.run_pylint_on_file() + + def run_pylint_on_file(self): + """Runs pylint with .pylint config file if found, default config otherwise.""" + pylint_configuration_file = find_file_starting_from_reference_file_directory(__file__, '.pylintrc') + if pylint_configuration_file: + Run(['--rcfile', pylint_configuration_file, self.filename], reporter=TextReporter(self.pylint_output)) + else: + Run([self.filename], reporter=TextReporter(self.pylint_output)) + + def manage_pylint_result(self, exception): + print self.pylint_output.read() + if exception.code != 0: + self.inform_check_failure('') + + +class WritableObject(object): + """Simple writable object for pylint.""" + + def __init__(self): + self.__content = '' + + def write(self, line): + """Adds line to object content.""" + self.__content = self.__content + '\n' + line + + def read(self): + """Returns object content.""" + return self.__content + + +def main(argv=None): + return PylintChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/general_hooks/check_xml_encoding.py b/pre_commit_hooks/loaderon-hooks/general_hooks/check_xml_encoding.py new file mode 100644 index 0000000..81748d4 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/general_hooks/check_xml_encoding.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod +from pre_commit_logic.util.file_helpers import read_file_line + + +class XMLEncodingChecker(FileCheckerTemplateMethod): + def _add_arguments_to_parser(self): + super(XMLEncodingChecker, self)._add_arguments_to_parser() + self.parser.add_argument('-e', '--encoding', help='Desired encoding.') + + def _check_file(self): + first_line = read_file_line(self.filename) + desired_encoding = self.args.encoding.rstrip() + first_line = first_line.rstrip('\n') + if first_line != desired_encoding: + self.inform_check_failure( + 'El archivo {} no comienza con la línea "{}" (verifica también espacios en blanco)'.format( + self.filename, + desired_encoding + ) + ) + + +def main(argv=None): + return XMLEncodingChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/__init__.py b/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_model_name.py b/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_model_name.py new file mode 100644 index 0000000..e0de223 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_model_name.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +import os +import re + +from pre_commit_logic.util.template_methods.file_bunches_lines_checker_template_method import \ + FileBunchesLinesCheckerTemplateMethod + + +class ModelNameAttributeChecker(FileBunchesLinesCheckerTemplateMethod): + def __init__(self, argv): + super(ModelNameAttributeChecker, self).__init__(argv) + self.__module_name = '' + name_attribute_regex = r'^(\t| )*_name = .+' + self.__name_pattern = re.compile(name_attribute_regex) + inherit_attribute_regex = r'^(\t| )*_inherit = .+' + self.__inherit_pattern = re.compile(inherit_attribute_regex) + self.__name_line = '' + self.__inherit_line = '' + + def _get_regexp(self): + return r'^(\t| )*class.+' + + def _check_file(self): + self.__module_name = self._set_module_name() + super(ModelNameAttributeChecker, self)._check_file() + + def _set_module_name(self): + models_folder_directory_path = os.path.dirname(self.filename) + module_directory_path = os.path.dirname(models_folder_directory_path) + return os.path.basename(module_directory_path) + + def _check_bunch(self): + super(ModelNameAttributeChecker, self)._check_bunch() + if self.__name_line and not self.__has_multiple_inheritance(): + self.__check_name() + + def _check_line(self): + """ + We will use this inherited method (which runs through all file lines) in order to gather lines that are required + to perform the lines bunch check. + """ + if self.__name_pattern.match(self._file_line): + self.__name_line = self._file_line.strip('_name = ') + if self.__inherit_pattern.match(self._file_line): + self.__inherit_line = self._file_line.strip('_inherit = ') + + def __has_multiple_inheritance(self): + return '[' in self.__inherit_line and ',' in self.__inherit_line + + def __check_name(self): + correct_name_regex = r'[\'\"]' + self.__module_name + r'\..+[\'\"]' + correct_name_pattern = re.compile(correct_name_regex) + if not correct_name_pattern.match(self.__name_line): + self.inform_check_failure( + 'El nombre de modelo {} no incluye como prefijo el nombre del módulo {}.'.format( + self.__name_line.strip('_name = '), + self.__module_name + ) + ) + + +def main(argv=None): + return ModelNameAttributeChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_view_fields_order.py b/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_view_fields_order.py new file mode 100644 index 0000000..22195a3 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_view_fields_order.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +import re + +from pre_commit_logic.util.template_methods.files_bunches_checker_template_method import \ + FileBunchesCheckerTemplateMethod + + +class ViewFieldsOrderChecker(FileBunchesCheckerTemplateMethod): + def __init__(self, argv): + super(ViewFieldsOrderChecker, self).__init__(argv) + name_line_regex = r'^(\t| )*.+<\/field>(\t| )*' + self._name_line_pattern = re.compile(name_line_regex) + model_line_regex = r'^(\t| )*.+<\/field>(\t| )*' + self._model_line_pattern = re.compile(model_line_regex) + self._record_line = None + self._name_line = None + self._model_line = None + + def _get_regexp(self): + return r'^(\t| )*(\t| )*' + + def _check_bunch(self): + self._record_line = self._bunch_of_lines[0].strip() + self._name_line = self._bunch_of_lines[1] + self._model_line = self._bunch_of_lines[2] + self._perform_check() + + def _perform_check(self): + if not self._name_line_pattern.match(self._name_line): + self.inform_check_failure("El primer campo (field) declarado en un record de vista debe ser 'name'. " + "Vista: {}".format(self._record_line)) + if not self._model_line_pattern.match(self._model_line): + self.inform_check_failure("El segundo campo (field) declarado en un record de vista debe ser 'model'. " + "Vista: {}".format(self._record_line)) + + +def main(argv=None): + return ViewFieldsOrderChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_view_name.py b/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_view_name.py new file mode 100644 index 0000000..c627b21 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/odoo_specific_hooks/check_view_name.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +import re + +from pre_commit_logic.odoo_specific_hooks.check_view_fields_order import ViewFieldsOrderChecker + + +class ViewNameChecker(ViewFieldsOrderChecker): + def __init__(self, argv): + super(ViewNameChecker, self).__init__(argv) + self._name_line = None + self._model_line = None + + def _perform_check(self): + self.__make_required_strips() + correct_name_regex = r'' + self._model_line + \ + r'\.(search|form|tree|filter)(\.inherit)?<\/field>' + correct_name_pattern = re.compile(correct_name_regex) + if not correct_name_pattern.match(self._name_line): + strip_name_line = self._name_line.strip() + strip_name_line = strip_name_line.strip('') + strip_name_line = strip_name_line.strip('') + self.inform_check_failure("El nombre de la vista {} no cumple el formato {}." + .format(strip_name_line, correct_name_regex)) + + def __make_required_strips(self): + self._model_line = self._model_line.strip() + self._model_line = self._model_line[20:-8] + self._name_line = self._name_line.strip() + + +def main(argv=None): + return ViewNameChecker(argv).run() + + +if __name__ == '__main__': + exit(main()) diff --git a/pre_commit_hooks/loaderon-hooks/util/__init__.py b/pre_commit_hooks/loaderon-hooks/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pre_commit_hooks/loaderon-hooks/util/check_failed_exception.py b/pre_commit_hooks/loaderon-hooks/util/check_failed_exception.py new file mode 100644 index 0000000..c2dbe25 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/check_failed_exception.py @@ -0,0 +1,2 @@ +class CheckFailedException(Exception): + pass diff --git a/pre_commit_hooks/loaderon-hooks/util/file_helpers.py b/pre_commit_hooks/loaderon-hooks/util/file_helpers.py new file mode 100644 index 0000000..c7737ba --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/file_helpers.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +import os +import re + + +def read_file(file_path, only_first_line=False): + opened_file = open(file_path) + if only_first_line: + lines = opened_file.readline() + else: + lines = opened_file.readlines() + opened_file.close() + return lines + + +def read_file_line(file_path): + return read_file(file_path, True) + + +def read_file_lines(file_path): + return read_file(file_path) + + +def find_file_starting_from_reference_file_directory(reference_file, file_to_find): + """Attempts to find file_to_find by navigating through directories from reference_file parent directory.""" + file_path = os.path.realpath(reference_file) + folder_path = os.path.dirname(file_path) + for root, unused_dirs, files in os.walk(folder_path): + if file_to_find in files: + return os.path.join(root, file_to_find) + return None + + +def get_indexes_of_lines_per_regex(file_lines, regex): + regex_pattern = re.compile(regex) + indexes_of_lines_defining_regex = [] + for index, line in enumerate(file_lines): + if regex_pattern.match(line): + indexes_of_lines_defining_regex.append(index) + return indexes_of_lines_defining_regex + + +def get_bunches_of_lines_dividing_by_regex(file_lines, regex): + lines = [] + indexes_of_lines_per_regex = get_indexes_of_lines_per_regex(file_lines, regex) + for index, index_of_lines in enumerate(indexes_of_lines_per_regex): + try: + next_regex_index = indexes_of_lines_per_regex[index + 1] + current_regex_lines = file_lines[index_of_lines:next_regex_index] + except IndexError: + current_regex_lines = file_lines[index_of_lines:] + lines.append(current_regex_lines) + return lines + + +def split_by_regexp(filename, regex): + file_lines = read_file_lines(filename) + return get_bunches_of_lines_dividing_by_regex(file_lines, regex) diff --git a/pre_commit_hooks/loaderon-hooks/util/git_helpers.py b/pre_commit_hooks/loaderon-hooks/util/git_helpers.py new file mode 100644 index 0000000..6771b8f --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/git_helpers.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +import subprocess + + +def get_current_branch_name(): + output = subprocess.check_output(['git', 'symbolic-ref', 'HEAD']) + output = output.splitlines() + head_branches = output[0] + return head_branches.strip().split('/')[-1] diff --git a/pre_commit_hooks/loaderon-hooks/util/string_helpers.py b/pre_commit_hooks/loaderon-hooks/util/string_helpers.py new file mode 100644 index 0000000..5c41bf5 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/string_helpers.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +import re + + +def matches_any_regexp(string, regexp_list): + """Checks if string matches any of regexp in regexp_list.""" + for regexp in regexp_list: + pattern = re.compile(regexp) + if pattern.match(string): + return True + return False diff --git a/pre_commit_hooks/loaderon-hooks/util/template_methods/__init__.py b/pre_commit_hooks/loaderon-hooks/util/template_methods/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pre_commit_hooks/loaderon-hooks/util/template_methods/checker_template_method.py b/pre_commit_hooks/loaderon-hooks/util/template_methods/checker_template_method.py new file mode 100644 index 0000000..2e45350 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/template_methods/checker_template_method.py @@ -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.') diff --git a/pre_commit_hooks/loaderon-hooks/util/template_methods/file_bunches_lines_checker_template_method.py b/pre_commit_hooks/loaderon-hooks/util/template_methods/file_bunches_lines_checker_template_method.py new file mode 100644 index 0000000..5dc27e2 --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/template_methods/file_bunches_lines_checker_template_method.py @@ -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 diff --git a/pre_commit_hooks/loaderon-hooks/util/template_methods/file_checker_template_method.py b/pre_commit_hooks/loaderon-hooks/util/template_methods/file_checker_template_method.py new file mode 100644 index 0000000..bf80ecb --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/template_methods/file_checker_template_method.py @@ -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 diff --git a/pre_commit_hooks/loaderon-hooks/util/template_methods/files_bunches_checker_template_method.py b/pre_commit_hooks/loaderon-hooks/util/template_methods/files_bunches_checker_template_method.py new file mode 100644 index 0000000..4e4404f --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/template_methods/files_bunches_checker_template_method.py @@ -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 diff --git a/pre_commit_hooks/loaderon-hooks/util/template_methods/lines_checker_template_method.py b/pre_commit_hooks/loaderon-hooks/util/template_methods/lines_checker_template_method.py new file mode 100644 index 0000000..4d64bfa --- /dev/null +++ b/pre_commit_hooks/loaderon-hooks/util/template_methods/lines_checker_template_method.py @@ -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