Add exclude_patterns support for CommandsEqual

This commit is contained in:
Kristóf Tóth 2018-08-04 21:12:06 +02:00
parent 4f881a0ea0
commit 16c936b2cd

View File

@ -8,20 +8,32 @@ from tfw.decorators.lazy_property import lazy_property
class CommandsEqual: class CommandsEqual:
def __init__(self, command_1, command_2, fuzzyness=1, must_begin_similarly=True, must_contain_patterns=None): def __init__(
self, command_1, command_2,
fuzzyness=1, begin_similarly=True,
include_patterns=None, exclude_patterns=None
):
self.command_1 = split(command_1) self.command_1 = split(command_1)
self.command_2 = split(command_2) self.command_2 = split(command_2)
self.fuzzyness = fuzzyness self.fuzzyness = fuzzyness
self.must_begin_similarly = must_begin_similarly self.begin_similarly = begin_similarly
self.must_contain_patterns = must_contain_patterns self.include_patterns = include_patterns
self.exclude_patterns = exclude_patterns
def __bool__(self): def __bool__(self):
if self.must_begin_similarly and not self.beginnings_are_equal: if self.begin_similarly:
return False if not self.beginnings_are_equal:
return False
if self.must_contain_patterns is not None and not self.commands_contain_necessary_parts: if self.include_patterns is not None:
return False if not self.commands_contain_include_patterns:
return False
if self.exclude_patterns is not None:
if not self.commands_contain_no_exclude_patterns:
return False
print(self.similarity)
return self.similarity >= self.fuzzyness return self.similarity >= self.fuzzyness
@lazy_property @lazy_property
@ -29,15 +41,23 @@ class CommandsEqual:
return self.command_1[0] == self.command_2[0] return self.command_1[0] == self.command_2[0]
@lazy_property @lazy_property
def commands_contain_necessary_parts(self): def commands_contain_include_patterns(self):
return all(( return all((
self.contains_neccessary_parts(self.command_1), self.contains_regex_patterns(self.command_1, self.include_patterns),
self.contains_neccessary_parts(self.command_2) self.contains_regex_patterns(self.command_2, self.include_patterns)
)) ))
def contains_neccessary_parts(self, command): @lazy_property
def commands_contain_no_exclude_patterns(self):
return all((
not self.contains_regex_patterns(self.command_1, self.exclude_patterns),
not self.contains_regex_patterns(self.command_2, self.exclude_patterns)
))
@staticmethod
def contains_regex_patterns(command, regex_parts):
command = ' '.join(command) command = ' '.join(command)
for pattern in self.must_contain_patterns: for pattern in regex_parts:
if not search(pattern, command): if not search(pattern, command):
return False return False
return True return True