ver Dec21st

updated setup configs from dict-style to list-style to support more
flexible setup steps
This commit is contained in:
David Chang
2023-12-21 10:30:23 +08:00
parent f4949c3e90
commit 4a643abc31
2 changed files with 83 additions and 48 deletions

View File

@@ -1,6 +1,8 @@
import requests import requests
import json import json
from typing import Dict, List
from typing import Any
class SetupController: class SetupController:
def __init__(self, http_server: str): 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._tidy_desktop(config) todo: implement this
self._open_setup(config) #self._open_setup(config)
# can add other setup steps # can add other setup steps
def _download_setup(self, config): def _download_setup(self, files: List[Dict[str, str]]):
if not config: """
return Args:
if not 'download' in config: files (List[Dict[str, str]]): files to download. lisf of dict like
return {
for url, path in config['download']: "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: if not url or not path:
raise Exception(f"Setup Download - Invalid URL ({url}) or path ({path}).") raise Exception(f"Setup Download - Invalid URL ({url}) or path ({path}).")
@@ -44,12 +70,13 @@ class SetupController:
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print("An error occurred while trying to send the request:", e) print("An error occurred while trying to send the request:", e)
def _change_wallpaper(self, config): def _change_wallpaper_setup(self, path: str):
if not config: #if not config:
return #return
if not 'wallpaper' in config: #if not 'wallpaper' in config:
return #return
path = config['wallpaper']
#path = config['wallpaper']
if not path: if not path:
raise Exception(f"Setup Wallpaper - Invalid path ({path}).") raise Exception(f"Setup Wallpaper - Invalid path ({path}).")
@@ -68,29 +95,29 @@ class SetupController:
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print("An error occurred while trying to send the request:", 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 raise NotImplementedError
def _open_setup(self, config): def _open_setup(self, path: str):
if not config: #if not config:
return #return
if not 'open' in config: #if not 'open' in config:
return #return
for path in config['open']: #for path in config['open']:
if not path: if not path:
raise Exception(f"Setup Open - Invalid path ({path}).") raise Exception(f"Setup Open - Invalid path ({path}).")
payload = json.dumps({"path": path}) payload = json.dumps({"path": path})
headers = { headers = {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
# send request to server to open file # send request to server to open file
try: try:
response = requests.post(self.http_server + "/open_file", headers=headers, data=payload) response = requests.post(self.http_server + "/open_file", headers=headers, data=payload)
if response.status_code == 200: if response.status_code == 200:
print("Command executed successfully:", response.text) print("Command executed successfully:", response.text)
else: else:
print("Failed to open file. Status code:", response.text) print("Failed to open file. Status code:", response.text)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print("An error occurred while trying to send the request:", e) print("An error occurred while trying to send the request:", e)

View File

@@ -3,17 +3,25 @@
"snapshot": "libreoffice_calc", "snapshot": "libreoffice_calc",
"instruction": "Fill the missing row and column which show the total value", "instruction": "Fill the missing row and column which show the total value",
"source": "https://youtube.com/shorts/feldd-Pn48c?si=9xJiem2uAHm6Jshb", "source": "https://youtube.com/shorts/feldd-Pn48c?si=9xJiem2uAHm6Jshb",
"config": { "config": [
"download": [ {
[ "type": "download",
"http://101.43.24.67/s/DbaHsQpPA7dxrA8/download/Quarterly_Product_Sales_by_Zone.xlsx", "parameters": {
"/home/david/Quarterly_Product_Sales_by_Zone.xlsx" "files": [
] {
], "url": "http://101.43.24.67/s/DbaHsQpPA7dxrA8/download/Quarterly_Product_Sales_by_Zone.xlsx",
"open": [ "path": "/home/david/Quarterly_Product_Sales_by_Zone.xlsx"
"/home/david/Quarterly_Product_Sales_by_Zone.xlsx" }
] ]
}, }
},
{
"type": "open",
"parameters": {
"path": "/home/david/Quarterly_Product_Sales_by_Zone.xlsx"
}
}
],
"trajectory": "trajectories/f9584479-3d0d-4c79-affa-9ad7afdd8850", "trajectory": "trajectories/f9584479-3d0d-4c79-affa-9ad7afdd8850",
"related_apps": [ "related_apps": [
"libreoffice calc" "libreoffice calc"