linter: pre-compile disable/enable rules regexes

Not only this should improve performance, but I find the code more
readable.
This commit is contained in:
Dimitri Papadopoulos 2022-08-05 21:56:21 +02:00 committed by Adrien Vergé
parent 868350681a
commit 6b6fdba3bf

View File

@ -29,6 +29,9 @@ PROBLEM_LEVELS = {
'error': 2, '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): class LintProblem(object):
"""Represents a linting problem found by yamllint.""" """Represents a linting problem found by yamllint."""
@ -82,7 +85,7 @@ def get_cosmetic_problems(buffer, conf, filepath):
def process_comment(self, comment): def process_comment(self, comment):
comment = str(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(' ') items = comment[18:].rstrip().split(' ')
rules = [item[5:] for item in items][1:] rules = [item[5:] for item in items][1:]
if len(rules) == 0: if len(rules) == 0:
@ -92,7 +95,7 @@ def get_cosmetic_problems(buffer, conf, filepath):
if id in self.all_rules: if id in self.all_rules:
self.rules.add(id) 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(' ') items = comment[17:].rstrip().split(' ')
rules = [item[5:] for item in items][1:] rules = [item[5:] for item in items][1:]
if len(rules) == 0: if len(rules) == 0: