Merge branch 'zdy'

This commit is contained in:
David Chang
2023-12-21 10:42:18 +08:00
6 changed files with 106 additions and 64 deletions

View File

@@ -1,6 +1,8 @@
import requests
import json
from typing import Dict, List
from typing import Any
class SetupController:
def __init__(self, http_server: str):
@@ -14,18 +16,42 @@ class SetupController:
...
}
"""
self._download_setup(config)
self._change_wallpaper(config)
for cfg in config:
config_type: str = cfg["type"]
parameters: Dict[str, Any] = cfg["parameters"]
# Assumes all the setup the functions should follow this name
# protocol
setup_function: str = "_{:}_setup".format(config_type)
assert hasattr(self, setup_function)
getattr(self, setup_function)(**parameters)
#self._download_setup(config)
#self._change_wallpaper(config)
# self._tidy_desktop(config) todo: implement this
self._open_setup(config)
#self._open_setup(config)
# can add other setup steps
def _download_setup(self, config):
if not config:
return
if not 'download' in config:
return
for url, path in config['download']:
def _download_setup(self, files: List[Dict[str, str]]):
"""
Args:
files (List[Dict[str, str]]): files to download. lisf of dict like
{
"url": str, the url to download
"path": str, the path on the VM to store the downloaded file
}
"""
#if not config:
#return
#if not 'download' in config:
#return
#for url, path in config['download']:
for f in files:
url: str = f["url"]
path: str = f["path"]
if not url or not path:
raise Exception(f"Setup Download - Invalid URL ({url}) or path ({path}).")
@@ -44,12 +70,13 @@ class SetupController:
except requests.exceptions.RequestException as e:
print("An error occurred while trying to send the request:", e)
def _change_wallpaper(self, config):
if not config:
return
if not 'wallpaper' in config:
return
path = config['wallpaper']
def _change_wallpaper_setup(self, path: str):
#if not config:
#return
#if not 'wallpaper' in config:
#return
#path = config['wallpaper']
if not path:
raise Exception(f"Setup Wallpaper - Invalid path ({path}).")
@@ -68,29 +95,29 @@ class SetupController:
except requests.exceptions.RequestException as e:
print("An error occurred while trying to send the request:", e)
def _tidy_desktop(self, config):
def _tidy_desktop_setup(self, **config):
raise NotImplementedError
def _open_setup(self, config):
if not config:
return
if not 'open' in config:
return
for path in config['open']:
if not path:
raise Exception(f"Setup Open - Invalid path ({path}).")
def _open_setup(self, path: str):
#if not config:
#return
#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'
}
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)
# 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)

View File

@@ -60,7 +60,7 @@ class DesktopEnv(gym.Env):
def _start_emulator(self):
while True:
try:
output = subprocess.check_output(f"vmrun -T ws list", shell=True, stderr=subprocess.STDOUT)
output = subprocess.check_output("vmrun -T ws list", shell=True, stderr=subprocess.STDOUT)
output = output.decode()
if self.path_to_vm.lstrip("~/") in output:
print("VM is running.")

View File

@@ -4,9 +4,10 @@ import platform
import subprocess
import requests
import Xlib.display
#import Xlib.display
import pyautogui
from PIL import ImageGrab, Image
#from PIL import ImageGrab, Image
from PIL import Image
from flask import Flask, request, jsonify, send_file
app = Flask(__name__)
@@ -180,7 +181,11 @@ def open_file():
return f"File not found: {path}", 404
try:
os.startfile(path)
if platform.system()=="Windows":
os.startfile(path)
else:
open_cmd: str = "open" if platform.system()=="Darwin" else "xdg-open"
subprocess.Popen([open_cmd, str(path)])
return "File opened successfully"
except Exception as e:
return f"Failed to open {path}. Error: {e}", 500