controllers
This commit is contained in:
0
desktop_env/controllers/__init__.py
Normal file
0
desktop_env/controllers/__init__.py
Normal file
37
desktop_env/controllers/keyboard.py
Normal file
37
desktop_env/controllers/keyboard.py
Normal 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})")
|
||||||
37
desktop_env/controllers/mouse.py
Normal file
37
desktop_env/controllers/mouse.py
Normal 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})")
|
||||||
9
desktop_env/controllers/xdotool.py
Normal file
9
desktop_env/controllers/xdotool.py
Normal 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()
|
||||||
Reference in New Issue
Block a user