mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 12:51:32 +00:00
Implement must_contain_patterns CommandsEqual
This commit is contained in:
parent
8454236bc8
commit
4f881a0ea0
@ -2,27 +2,46 @@
|
|||||||
# All Rights Reserved. See LICENSE file for details.
|
# All Rights Reserved. See LICENSE file for details.
|
||||||
|
|
||||||
from shlex import split
|
from shlex import split
|
||||||
|
from re import search
|
||||||
|
|
||||||
from tfw.decorators.lazy_property import lazy_property
|
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):
|
def __init__(self, command_1, command_2, fuzzyness=1, must_begin_similarly=True, must_contain_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.must_begin_similarly = must_begin_similarly
|
||||||
|
self.must_contain_patterns = must_contain_patterns
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
if self.must_begin_similarly and not self.beginnings_are_equal:
|
if self.must_begin_similarly and not self.beginnings_are_equal:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if self.must_contain_patterns is not None and not self.commands_contain_necessary_parts:
|
||||||
|
return False
|
||||||
|
|
||||||
return self.similarity >= self.fuzzyness
|
return self.similarity >= self.fuzzyness
|
||||||
|
|
||||||
@lazy_property
|
@lazy_property
|
||||||
def beginnings_are_equal(self):
|
def beginnings_are_equal(self):
|
||||||
return self.command_1[0] == self.command_2[0]
|
return self.command_1[0] == self.command_2[0]
|
||||||
|
|
||||||
|
@lazy_property
|
||||||
|
def commands_contain_necessary_parts(self):
|
||||||
|
return all((
|
||||||
|
self.contains_neccessary_parts(self.command_1),
|
||||||
|
self.contains_neccessary_parts(self.command_2)
|
||||||
|
))
|
||||||
|
|
||||||
|
def contains_neccessary_parts(self, command):
|
||||||
|
command = ' '.join(command)
|
||||||
|
for pattern in self.must_contain_patterns:
|
||||||
|
if not search(pattern, command):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
@lazy_property
|
@lazy_property
|
||||||
def similarity(self):
|
def similarity(self):
|
||||||
parts_1 = set(self.command_1)
|
parts_1 = set(self.command_1)
|
||||||
|
Loading…
Reference in New Issue
Block a user