From 984142448b149cbcaf0380d8466bdb71b5eb33aa Mon Sep 17 00:00:00 2001 From: Dashamir Hoxha Date: Sun, 28 Jul 2024 02:40:10 +0000 Subject: [PATCH] Add delay when using sendcharacter --- asciinema_automation/instruction.py | 23 +++++++---------------- asciinema_automation/script.py | 7 +++++++ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/asciinema_automation/instruction.py b/asciinema_automation/instruction.py index 6f364ca..96b7442 100644 --- a/asciinema_automation/instruction.py +++ b/asciinema_automation/instruction.py @@ -1,5 +1,4 @@ import logging -import random import re import time from typing import List @@ -8,7 +7,6 @@ logger = logging.getLogger(__name__) - class ChangeWaitInstruction(Instruction): def __init__(self, wait: float): super().__init__() @@ -60,18 +58,12 @@ def run(self, script: Script) -> None: for send_character, receive_character in zip( self.send_value, self.receive_value ): - if script.standard_deviation is None: - time.sleep(script.delay) - else: - time.sleep(abs(random.gauss(script.delay, script.standard_deviation))) + script.typing_delay() script.process.send(str(send_character)) script.process.expect(str(receive_character)) # End instruction - if script.standard_deviation is None: - time.sleep(script.delay) - else: - time.sleep(abs(random.gauss(script.delay, script.standard_deviation))) + script.typing_delay() class SendCharacterInstruction(Instruction): @@ -81,8 +73,10 @@ def __init__(self, send_value: str): def run(self, script: Script) -> None: super().run(script) - logger.debug("Send '%s'", self.send_value) - script.process.send(self.send_value) + logger.debug("SendCharacter '%s'", self.send_value) + for character in self.send_value: + script.typing_delay() + script.process.send(character) class SendShellInstruction(SendInstruction): @@ -127,10 +121,7 @@ def run(self, script: Script) -> None: super().run(script) logger.debug("Send %s arrow %i times", self.send, self.num) for _ in range(self.num): - if script.standard_deviation is None: - time.sleep(script.delay) - else: - time.sleep(abs(random.gauss(script.delay, script.standard_deviation))) + script.typing_delay() if self.enter: script.process.sendline(self.mapping[self.send]) else: diff --git a/asciinema_automation/script.py b/asciinema_automation/script.py index f3d59be..dbf78ac 100644 --- a/asciinema_automation/script.py +++ b/asciinema_automation/script.py @@ -1,6 +1,7 @@ import logging import pathlib import time +import random from dataclasses import dataclass import pexpect @@ -24,6 +25,12 @@ class Script: instructions: list[Instruction] delaybeforesend: float | None = None + def typing_delay(self): + if self.standard_deviation is None: + time.sleep(self.delay) + else: + time.sleep(abs(random.gauss(self.delay, self.standard_deviation))) + def execute(self) -> None: spawn_command = ( "asciinema rec " + str(self.outputfile) + " " + self.asciinema_arguments