Skip to content

Commit 07ae939

Browse files
Merge pull request #193 from thorinaboenke/feature_log_actual_sleep_duration
calculate sleep time in computed field
2 parents 82a41a3 + 04c8fdf commit 07ae939

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import time
2-
from random import randint
32
from attackmate.executors.baseexecutor import BaseExecutor
43
from attackmate.result import Result
5-
from attackmate.executors.features.cmdvars import CmdVars
64
from attackmate.variablestore import VariableStore
75
from attackmate.processmanager import ProcessManager
86
from attackmate.executors.executor_factory import executor_factory
@@ -12,20 +10,10 @@
1210
class SleepExecutor(BaseExecutor):
1311
def __init__(self, pm: ProcessManager, cmdconfig=None, *, varstore: VariableStore):
1412
super().__init__(pm, varstore, cmdconfig)
15-
self.sleep_time = None
16-
17-
def set_sleeptime(self, command):
18-
self.sleep_time = CmdVars.variable_to_int('seconds', command.seconds)
19-
if command.random:
20-
self.sleep_time = randint(
21-
CmdVars.variable_to_int('min_sec', command.min_sec), # nosec
22-
CmdVars.variable_to_int('seconds', command.seconds),
23-
) # nosec
2413

2514
def log_command(self, command):
26-
self.set_sleeptime(command)
27-
self.logger.info(f'Sleeping {self.sleep_time} seconds')
15+
self.logger.info(f'Sleeping {command.sleep_time} seconds')
2816

2917
def _exec_cmd(self, command):
30-
time.sleep(self.sleep_time)
18+
time.sleep(command.sleep_time)
3119
return Result('Awake O_O', 0)

src/attackmate/schemas/sleep.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from typing import Literal
2+
from pydantic import computed_field
3+
from functools import cached_property
4+
from random import randint
25
from attackmate.schemas.base import BaseCommand, StringNumber
36
from attackmate.command import CommandRegistry
47

@@ -10,3 +13,11 @@ class SleepCommand(BaseCommand):
1013
seconds: StringNumber = '1'
1114
random: bool = False
1215
cmd: str = 'sleep'
16+
17+
@computed_field # type: ignore[misc]
18+
@cached_property
19+
# calculates the actual sleep time based on whether random is enabled
20+
def sleep_time(self) -> int:
21+
if not self.random:
22+
return int(self.seconds)
23+
return randint(int(self.min_sec), int(self.seconds))

0 commit comments

Comments
 (0)