From fd3787ee28f8d381efd3df802be8cf3caf07d348 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Thu, 2 Nov 2023 11:10:55 +0800 Subject: [PATCH] controllers --- desktop_env/controllers/__init__.py | 0 desktop_env/controllers/keyboard.py | 37 +++++++++++++++++++++++++++++ desktop_env/controllers/mouse.py | 37 +++++++++++++++++++++++++++++ desktop_env/controllers/xdotool.py | 9 +++++++ 4 files changed, 83 insertions(+) create mode 100644 desktop_env/controllers/__init__.py create mode 100644 desktop_env/controllers/keyboard.py create mode 100644 desktop_env/controllers/mouse.py create mode 100644 desktop_env/controllers/xdotool.py diff --git a/desktop_env/controllers/__init__.py b/desktop_env/controllers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/desktop_env/controllers/keyboard.py b/desktop_env/controllers/keyboard.py new file mode 100644 index 0000000..f818f01 --- /dev/null +++ b/desktop_env/controllers/keyboard.py @@ -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})") \ No newline at end of file diff --git a/desktop_env/controllers/mouse.py b/desktop_env/controllers/mouse.py new file mode 100644 index 0000000..f818f01 --- /dev/null +++ b/desktop_env/controllers/mouse.py @@ -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})") \ No newline at end of file diff --git a/desktop_env/controllers/xdotool.py b/desktop_env/controllers/xdotool.py new file mode 100644 index 0000000..620bee6 --- /dev/null +++ b/desktop_env/controllers/xdotool.py @@ -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() \ No newline at end of file