-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
145 lines (113 loc) · 4.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os, json, base64, ssl, certifi
from pathlib import Path
from json import dumps as jsonDumps
from itertools import chain
import decky_plugin
from subprocess import Popen, PIPE
from urllib.error import HTTPError
from urllib.request import urlopen, Request
confdir = os.environ["DECKY_PLUGIN_SETTINGS_DIR"]
send_buffer = []
def split_string(string):
return [
string[i : i + 1024 * 60] for i in range(0, len(string), 1024 * 60)
] # every 60KB
class Plugin:
async def get_flatpaks(self):
def read_proc(cmd):
proc = Popen(cmd, stdout=PIPE, stderr=None, shell=True)
flatpaks = proc.communicate()[0]
flatpaks = flatpaks.decode("utf-8")
return flatpaks
# Global flatpak list
global_flatpaks = read_proc('flatpak list --app --columns="name,application" | awk \'BEGIN {FS="\\t"} {print "{\\"name\\":\\""$1"\\",\\"exec\\":\\"/usr/bin/flatpak run "$2"\\"},"}\\\'')
# User flatpak list
local_flatpaks = read_proc('runuser -l '+decky_plugin.DECKY_USER+' -c \'flatpak list --app --columns="name,application"\' | awk \'BEGIN {FS="\\t"} {print "{\\"name\\":\\""$1"\\",\\"exec\\":\\"/usr/bin/flatpak run "$2"\\"},"}\\\'')
packages = global_flatpaks + local_flatpaks[:-2] # Remove trailing comma
return "[" + packages + "]"
async def get_desktops(self):
packages = []
for desktopFile in chain(
Path("/usr/share/applications").glob("*.desktop"),
Path(f"{decky_plugin.DECKY_USER_HOME}/.local/share/applications/").glob("*.desktop"),
):
if not desktopFile.is_file():
continue
with open(desktopFile) as f:
package = {}
foundName = foundExec = False
for line in f:
line = line.strip()
if line.startswith("Name="):
foundName = True
package["name"] = line[5:]
elif line.startswith("Exec="):
foundExec = True
package["exec"] = line[5:]
if foundName and foundExec:
packages.append(package)
break
return jsonDumps(packages)
async def get_DECKY_USER_HOME(self):
return decky_plugin.DECKY_USER_HOME
async def get_config(self):
with open(os.path.join(confdir, "config.json"), "r") as f:
return json.load(f)
async def set_config_value(self, key, value):
config = json.load(open(os.path.join(confdir, "config.json")))
config[key] = value
with open(os.path.join(confdir, "config.json"), "w") as f:
json.dump(config, f)
return config
async def get_id(self):
with open(os.path.join(confdir, "scid.txt"), "r") as sc:
id = sc.read()
try:
id = int(id)
return id
except ValueError:
return -1
async def set_id(self, id):
with open(os.path.join(confdir, "scid.txt"), "w") as sc:
sc.write(str(id))
async def get_req_imgb64(self, url):
global send_buffer
if len(send_buffer) != 0:
return
req = Request(url)
req.add_header("User-Agent", "SDH-QuickLaunch")
try:
content = urlopen(
req, context=ssl.create_default_context(cafile=certifi.where())
).read()
img = base64.b64encode(content).decode("ascii")
send_buffer = split_string(img)
new_chunk = send_buffer.pop(0)
return {"data": new_chunk, "is_last": len(send_buffer) == 0}
except HTTPError:
decky_plugin.logger.error("HTTPError while requesting " + url)
pass
async def receive_next_chunk(self):
global send_buffer
new_chunk = send_buffer.pop(0)
return {"data": new_chunk, "is_last": len(send_buffer) == 0}
async def _main(self):
decky_plugin.logger.info("Loading plugin")
try:
os.mkdir(confdir)
except FileExistsError:
pass
try:
sc = open(os.path.join(confdir, "scid.txt"), "x")
sc.close()
except FileExistsError:
pass
try:
sc = open(os.path.join(confdir, "config.json"), "x")
sc.write("{}")
sc.close()
except FileExistsError:
pass
decky_plugin.logger.info("Plugin loaded")
async def _unload(self):
pass