Add docstrings to CommandsEqual

This commit is contained in:
Kristóf Tóth 2018-08-04 21:49:06 +02:00
parent 16c936b2cd
commit f6d77e1132
3 changed files with 46 additions and 0 deletions

View File

@ -23,3 +23,10 @@ Components
.. autoclass:: BashMonitor .. autoclass:: BashMonitor
:members: :members:
.. autoclass:: FSMManagingEventHandler
:members:
.. autoclass:: CommandsEqual
:members:

View File

@ -11,3 +11,4 @@ from .log_monitoring_event_handler import LogMonitoringEventHandler
from .fsm_managing_event_handler import FSMManagingEventHandler from .fsm_managing_event_handler import FSMManagingEventHandler
from .snapshot_provider import SnapshotProvider from .snapshot_provider import SnapshotProvider
from .directory_snapshotting_event_handler import DirectorySnapshottingEventHandler from .directory_snapshotting_event_handler import DirectorySnapshottingEventHandler
from .commands_equal import CommandsEqual

View File

@ -8,11 +8,49 @@ from tfw.decorators.lazy_property import lazy_property
class CommandsEqual: class CommandsEqual:
"""
This class is useful for comparing executed commands with
excepted commands (i.e. when triggering a state change when
the correct command is executed).
Note that in most cases you should test the changes
caused by the commands instead of just checking command history
(stuff can be done in countless ways and preparing for every
single case is impossible). This should only be used when
testing the changes would be very difficult, like when
explaining stuff with cli tools and such.
This class implicitly converts to bool, use it like
if CommandsEqual(...): ...
It tries detecting differing command parameter orders with similar
semantics and provides fuzzy logic options.
The rationale behind this is that a few false positives
are better than only accepting a single version of a command
(i.e. using ==).
"""
def __init__( def __init__(
self, command_1, command_2, self, command_1, command_2,
fuzzyness=1, begin_similarly=True, fuzzyness=1, begin_similarly=True,
include_patterns=None, exclude_patterns=None include_patterns=None, exclude_patterns=None
): ):
"""
:param command_1: Compared command 1
:param command_2: Compared command 2
:param fuzzyness: float between 0 and 1.
the percentage of arguments required to
match between commands to result in True.
i.e 1 means 100% - all arguments need to be
present in both commands, while 0.75
would mean 75% - in case of 4 arguments
1 could differ between the commands.
:param begin_similarly: bool, the first word of the commands
must match
:param include_patterns: list of regex patterns the commands
must include
:param exclude_patterns: list of regex patterns the commands
must exclude
"""
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