Improve the logic of env setup; add change wallpaper; add example

This commit is contained in:
Timothyxxx
2023-12-05 17:32:24 +08:00
parent 8301096fd4
commit 4ba053998d
4 changed files with 323 additions and 11 deletions

View File

@@ -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)