Improve the logic of env setup; add change wallpaper; add example
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
|
||||
class SetupController:
|
||||
def __init__(self, http_server: str):
|
||||
self.http_server = http_server + "/setup"
|
||||
@@ -14,8 +15,10 @@ class SetupController:
|
||||
}
|
||||
"""
|
||||
self._download_setup(config)
|
||||
self._change_wallpaper(config)
|
||||
# self._tidy_desktop(config) todo: implement this
|
||||
self._open_setup(config)
|
||||
# can add other setup steps
|
||||
|
||||
|
||||
def _download_setup(self, config):
|
||||
if not 'download' in config:
|
||||
@@ -28,7 +31,7 @@ class SetupController:
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
|
||||
# send request to server to download file
|
||||
try:
|
||||
response = requests.post(self.http_server + "/download_file", headers=headers, data=payload)
|
||||
@@ -38,3 +41,50 @@ class SetupController:
|
||||
print("Failed to download file. Status code:", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
|
||||
def _change_wallpaper(self, config):
|
||||
if not 'wallpaper' in config:
|
||||
return
|
||||
path = config['wallpaper']
|
||||
if not path:
|
||||
raise Exception(f"Setup Wallpaper - Invalid path ({path}).")
|
||||
|
||||
payload = json.dumps({"path": path})
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
# send request to server to change wallpaper
|
||||
try:
|
||||
response = requests.post(self.http_server + "/change_wallpaper", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
print("Command executed successfully:", response.text)
|
||||
else:
|
||||
print("Failed to change wallpaper. Status code:", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
|
||||
def _tidy_desktop(self, config):
|
||||
raise NotImplementedError
|
||||
|
||||
def _open_setup(self, config):
|
||||
if not 'open' in config:
|
||||
return
|
||||
for path in config['open']:
|
||||
if not path:
|
||||
raise Exception(f"Setup Open - Invalid path ({path}).")
|
||||
|
||||
payload = json.dumps({"path": path})
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
# send request to server to open file
|
||||
try:
|
||||
response = requests.post(self.http_server + "/open_file", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
print("Command executed successfully:", response.text)
|
||||
else:
|
||||
print("Failed to open file. Status code:", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
|
||||
@@ -26,6 +26,7 @@ class DesktopEnv(gym.Env):
|
||||
self,
|
||||
path_to_vm: str,
|
||||
snapshot_path: str = "base",
|
||||
config: dict = None,
|
||||
action_space: str = "pyautogui",
|
||||
):
|
||||
# Initialize environment variables
|
||||
@@ -38,6 +39,7 @@ class DesktopEnv(gym.Env):
|
||||
self.host = f"http://{self._get_vm_ip()}:5000"
|
||||
self.controller = PythonController(http_server=self.host)
|
||||
self.setup_controller = SetupController(http_server=self.host)
|
||||
self.config = config
|
||||
|
||||
# mode: human or machine
|
||||
assert action_space in ["computer_13", "pyautogui"]
|
||||
@@ -102,6 +104,9 @@ class DesktopEnv(gym.Env):
|
||||
self._start_emulator()
|
||||
print("Emulator started.")
|
||||
|
||||
print("Setting up environment...")
|
||||
self.setup_controller.setup(self.config)
|
||||
|
||||
observation = self._get_obs()
|
||||
return observation
|
||||
|
||||
@@ -120,9 +125,6 @@ class DesktopEnv(gym.Env):
|
||||
done = False # todo: Define episode termination condition for each example
|
||||
info = {}
|
||||
return observation, reward, done, info
|
||||
|
||||
def setup(self, config: dict):
|
||||
self.setup_controller.setup(config)
|
||||
|
||||
def render(self, mode='rgb_array'):
|
||||
if mode == 'rgb_array':
|
||||
|
||||
@@ -84,6 +84,36 @@ def get_cursor_position():
|
||||
return pyautogui.position().x, pyautogui.position().y
|
||||
|
||||
|
||||
@app.route("/setup/change_wallpaper", methods=['POST'])
|
||||
def change_wallpaper():
|
||||
data = request.json
|
||||
path = data.get('path', None)
|
||||
|
||||
if not path:
|
||||
return "Path not supplied!", 400
|
||||
|
||||
path = Path(path)
|
||||
|
||||
if not path.exists():
|
||||
return f"File not found: {path}", 404
|
||||
|
||||
try:
|
||||
user_platform = platform.system()
|
||||
if user_platform == "Windows":
|
||||
import ctypes
|
||||
ctypes.windll.user32.SystemParametersInfoW(20, 0, str(path), 3)
|
||||
elif user_platform == "Linux":
|
||||
import subprocess
|
||||
subprocess.run(["gsettings", "set", "org.gnome.desktop.background", "picture-uri", f"file://{path}"])
|
||||
elif user_platform == "Darwin": # (Mac OS)
|
||||
import subprocess
|
||||
subprocess.run(
|
||||
["osascript", "-e", f'tell application "Finder" to set desktop picture to POSIX file "{path}"'])
|
||||
return "Wallpaper changed successfully"
|
||||
except Exception as e:
|
||||
return f"Failed to change wallpaper. Error: {e}", 500
|
||||
|
||||
|
||||
@app.route("/setup/download_file", methods=['POST'])
|
||||
def download_file():
|
||||
data = request.json
|
||||
|
||||
Reference in New Issue
Block a user