From 6b6fdba3bf831a075c36fa6d56262b422bf96355 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Fri, 5 Aug 2022 21:56:21 +0200 Subject: [PATCH] linter: pre-compile disable/enable rules regexes Not only this should improve performance, but I find the code more readable. --- yamllint/linter.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/yamllint/linter.py b/yamllint/linter.py index 3984970..58dbba4 100644 --- a/yamllint/linter.py +++ b/yamllint/linter.py @@ -29,6 +29,9 @@ PROBLEM_LEVELS = { 'error': 2, } +DISABLE_RULE_PATTERN = re.compile(r'^# yamllint disable( rule:\S+)*\s*$') +ENABLE_RULE_PATTERN = re.compile(r'^# yamllint enable( rule:\S+)*\s*$') + class LintProblem(object): """Represents a linting problem found by yamllint.""" @@ -82,7 +85,7 @@ def get_cosmetic_problems(buffer, conf, filepath): def process_comment(self, comment): comment = str(comment) - if re.match(r'^# yamllint disable( rule:\S+)*\s*$', comment): + if DISABLE_RULE_PATTERN.match(comment): items = comment[18:].rstrip().split(' ') rules = [item[5:] for item in items][1:] if len(rules) == 0: @@ -92,7 +95,7 @@ def get_cosmetic_problems(buffer, conf, filepath): if id in self.all_rules: self.rules.add(id) - elif re.match(r'^# yamllint enable( rule:\S+)*\s*$', comment): + elif ENABLE_RULE_PATTERN.match(comment): items = comment[17:].rstrip().split(' ') rules = [item[5:] for item in items][1:] if len(rules) == 0: