updated usage of tmp and cache direcotories added cache function for evaluation resources acquiring
128 lines
4.5 KiB
Python
128 lines
4.5 KiB
Python
import requests
|
|
import json
|
|
|
|
from typing import Dict, List
|
|
from typing import Any
|
|
|
|
class SetupController:
|
|
def __init__(self, http_server: str):
|
|
self.http_server = http_server + "/setup"
|
|
|
|
def setup(self, config: List[Dict[str, Any]]):
|
|
"""
|
|
Args:
|
|
config (List[Dict[str, Any]]): list of dict like {str: Any}. each
|
|
config dict has the structure like
|
|
{
|
|
"type": str, corresponding to the `_{:}_setup` methods of
|
|
this class
|
|
"parameters": dick like {str, Any} providing the keyword
|
|
parameters
|
|
}
|
|
"""
|
|
|
|
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)
|
|
# can add other setup steps
|
|
|
|
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}).")
|
|
|
|
payload = json.dumps({"url": url, "path": path})
|
|
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)
|
|
if response.status_code == 200:
|
|
print("Command executed successfully:", response.text)
|
|
else:
|
|
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_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}).")
|
|
|
|
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_setup(self, **config):
|
|
raise NotImplementedError
|
|
|
|
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'
|
|
}
|
|
|
|
# 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)
|