merge CliDirector and MitmCliDirector

This commit is contained in:
Martin Plattner 2020-09-04 15:22:04 +02:00
parent 129c03e51a
commit d305c973f5
3 changed files with 22 additions and 40 deletions

View File

@ -1,18 +1,23 @@
import datetime
import json
import libtmux
import random
import requests
import subprocess
import threading
import time
import typing
class InstructionSpec(typing.NamedTuple):
instruction: str
time_from: float
time_to: float
class CliDirector:
def __init__(self):
self.record_start = None
self.pause_between_keys = 0.2
self.instructions: typing.List[InstructionSpec] = []
def start(self, filename: str, width: int = 0, height: int = 0) -> libtmux.Session:
self.start_session(width, height)
@ -46,6 +51,7 @@ class CliDirector:
self.asciinema_proc.terminate()
self.asciinema_proc.wait(timeout=5)
self.record_start = None
self.instructions = []
def end_session(self) -> None:
self.tmux_session.kill_session()
@ -104,7 +110,6 @@ class CliDirector:
self.tmux_session.set_option("display-time", int(duration * 1000)) # milliseconds
self.tmux_pane.display_message(" " + msg)
# todo: this is a hack and needs refactoring (instruction() is only defined in MitmCliDirector)
if add_instruction or instruction_html:
if not instruction_html:
instruction_html = msg
@ -126,28 +131,6 @@ class CliDirector:
self.pause(duration)
self.tmux_pane.cmd("display-popup", "-C")
@property
def current_time(self) -> float:
now = time.time()
return round(now - self.record_start, 1)
@property
def current_pane(self) -> libtmux.Pane:
return self.tmux_pane
class InstructionSpec(typing.NamedTuple):
instruction: str
time_from: float
time_to: float
# todo: merge with CliDirector
class MitmCliDirector(CliDirector):
def __init__(self):
super().__init__()
self.instructions: typing.List[InstructionSpec] = []
def instruction(self, instruction: str, duration: float = 3, time_from: typing.Optional[float] = None) -> None:
if time_from is None:
time_from = self.current_time
@ -165,12 +148,11 @@ class MitmCliDirector(CliDirector):
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(instr_as_dicts, f, ensure_ascii=False, indent=4)
def request(self, url: str, threaded: bool = False) -> None:
if threaded:
threading.Thread(target=lambda: requests.get(url, verify=False)).start()
else:
requests.get(url, verify=False)
@property
def current_time(self) -> float:
now = time.time()
return round(now - self.record_start, 1)
def end_recording(self) -> None:
self.instructions = []
super().end_recording()
@property
def current_pane(self) -> libtmux.Pane:
return self.tmux_pane

View File

@ -1,11 +1,11 @@
#!/usr/bin/env python3
from clidirector import MitmCliDirector
from clidirector import CliDirector
import screenplays
if __name__ == '__main__':
director = MitmCliDirector()
director = CliDirector()
screenplays.record_user_interface(director)
screenplays.record_intercept_requests(director)
screenplays.record_modify_requests(director)

View File

@ -1,9 +1,9 @@
#!/usr/bin/env python3
from clidirector import MitmCliDirector
from clidirector import CliDirector
def record_user_interface(d: MitmCliDirector):
def record_user_interface(d: CliDirector):
tmux = d.start_session(width=120, height=36)
window = tmux.attached_window
@ -98,7 +98,7 @@ def record_user_interface(d: MitmCliDirector):
d.end()
def record_intercept_requests(d: MitmCliDirector):
def record_intercept_requests(d: CliDirector):
tmux = d.start_session(width=120, height=36)
window = tmux.attached_window
@ -160,7 +160,7 @@ def record_intercept_requests(d: MitmCliDirector):
d.end()
def record_modify_requests(d: MitmCliDirector):
def record_modify_requests(d: CliDirector):
tmux = d.start_session(width=120, height=36)
window = tmux.attached_window
@ -231,7 +231,7 @@ def record_modify_requests(d: MitmCliDirector):
d.end()
def record_replay_requests(d: MitmCliDirector):
def record_replay_requests(d: CliDirector):
tmux = d.start_session(width=120, height=36)
window = tmux.attached_window