controllers

This commit is contained in:
Jing Hua
2023-11-02 11:10:55 +08:00
parent 824a9068e5
commit fd3787ee28
4 changed files with 83 additions and 0 deletions

View File

View File

@@ -0,0 +1,37 @@
from abc import ABC, abstractmethod
from fabric import Connection
from xdotool import XDoToolController
class AbstractMouseController(ABC):
@abstractmethod
def type(self, text: str):
raise NotImplementedError
@abstractmethod
def key(self, key: str):
raise NotImplementedError
class XDoToolKeyboardController(AbstractMouseController, XDoToolController):
def __init__(self, ssh_connection: Connection):
super().__init__(ssh_connection=ssh_connection)
def type(self, text: str):
self._execute_xdotool_command(f"type {text}")
def key(self, key: str):
self._execute_xdotool_command(f"key {key}")
class PythonKeyboardController(AbstractMouseController):
def __init__(self, ssh_connection: Connection):
self.ssh_connection = ssh_connection
def _execute_python_command(self, command: list[str]) -> None:
result = self.ssh_connection.run(f"sudo python3 -c 'import keyboard; keyboard.{command}'", hide=True)
return result.stdout.strip()
def type(self, text: str):
self._execute_python_command(f"write({text})")
def key(self, key: str):
self._execute_python_command(f"press_and_release({key})")

View File

@@ -0,0 +1,37 @@
from abc import ABC, abstractmethod
from fabric import Connection
from xdotool import XDoToolController
class AbstractMouseController(ABC):
@abstractmethod
def type(self, text: str):
raise NotImplementedError
@abstractmethod
def key(self, key: str):
raise NotImplementedError
class XDoToolKeyboardController(AbstractMouseController, XDoToolController):
def __init__(self, ssh_connection: Connection):
super().__init__(ssh_connection=ssh_connection)
def type(self, text: str):
self._execute_xdotool_command(f"type {text}")
def key(self, key: str):
self._execute_xdotool_command(f"key {key}")
class PythonKeyboardController(AbstractMouseController):
def __init__(self, ssh_connection: Connection):
self.ssh_connection = ssh_connection
def _execute_python_command(self, command: list[str]) -> None:
result = self.ssh_connection.run(f"sudo python3 -c 'import keyboard; keyboard.{command}'", hide=True)
return result.stdout.strip()
def type(self, text: str):
self._execute_python_command(f"write({text})")
def key(self, key: str):
self._execute_python_command(f"press_and_release({key})")

View File

@@ -0,0 +1,9 @@
from fabric import Connection
class XDoToolController:
def __init__(self, ssh_connection: Connection):
self.ssh_connection = ssh_connection
def _execute_xdotool_command(self, command: list[str]) -> None:
result = self.ssh_connection.run(f"DISPLAY=:0 xdotool {command}", hide=True)
return result.stdout.strip()