forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
182 lines (143 loc) · 6.4 KB
/
Copy path__init__.py
File metadata and controls
182 lines (143 loc) · 6.4 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
# Copyright (c) 2024 Manuel Schneider
import json
import urllib.error
import urllib.request
from pathlib import Path
from albert import *
md_iid = "3.0"
md_version = "3.0"
md_name = "Syncthing"
md_description = "Control the local Syncthing instance."
md_license = "MIT"
md_url = "https://github.com/albertlauncher/python/tree/main/syncthing"
md_authors = "@manuelschneid3r"
# https://docs.syncthing.net/dev/rest.html
class Syncthing:
def __init__(self, api_key, base_url="http://localhost:8384"):
self.api_key = api_key
self.base_url = base_url
def _request(self, method, endpoint, data=None) -> dict:
url = f"{self.base_url}{endpoint}"
headers = {
"X-API-Key": self.api_key,
"Content-Type": "application/json"
}
body = json.dumps(data).encode("utf-8") if data else None
req = urllib.request.Request(url, data=body, headers=headers, method=method)
try:
with urllib.request.urlopen(req) as resp:
if not (200 <= resp.status < 300):
raise Exception(f"Unexpected status {resp.status}")
content = resp.read().decode()
return json.loads(content) if content else {}
except urllib.error.HTTPError as e:
raise Exception(f"HTTP {e.code}: {e.read().decode()}")
def _get(self, endpoint):
return self._request("GET", endpoint)
def _post(self, endpoint, data=None):
return self._request("POST", endpoint, data)
def _patch(self, endpoint, data=None):
return self._request("PATCH", endpoint, data)
def config(self):
return self._get('/rest/config')
def resumeDevice(self, device_id:str):
return self._patch(f'/rest/config/devices/{device_id}', {'paused': False})
def pauseDevice(self, device_id:str):
return self._patch(f'/rest/config/devices/{device_id}', {'paused': True})
def resumeFolder(self, folder_id:str):
return self._patch(f'/rest/config/folders/{folder_id}', {'paused': False})
def pauseFolder(self, folder_id:str):
return self._patch(f'/rest/config/folders/{folder_id}', {'paused': True})
def scanFolder(self, folder_id:str):
return self._post(f'/rest/db/scan?folder={folder_id}')
class Plugin(PluginInstance, GlobalQueryHandler):
config_key = 'syncthing_api_key'
icon_urls_active = [f"file:{Path(__file__).parent}/syncthing_active.svg"]
icon_urls_inactive = [f"file:{Path(__file__).parent}/syncthing_inactive.svg"]
def __init__(self):
PluginInstance.__init__(self)
GlobalQueryHandler.__init__(self)
self.st = Syncthing(self.readConfig(self.config_key, str) or '')
def defaultTrigger(self):
return 'st '
@property
def api_key(self) -> str:
return self.st.api_key
@api_key.setter
def api_key(self, value: str):
if self.st.api_key != value:
self.st.api_key = value
self.writeConfig(self.config_key, value)
def configWidget(self):
return [
{
'type': 'lineedit',
'property': 'api_key',
'label': 'API key',
'widget_properties': {'tooltip': 'You can find the API key using the web frontend.'}
}
]
def handleTriggerQuery(self, query):
try:
super().handleTriggerQuery(query)
except Exception as e:
query.add(StandardItem(id="err", text="Error", subtext=str(e), iconUrls=self.icon_urls_active))
def handleGlobalQuery(self, query):
config = self.st.config()
devices = dict()
for d in config['devices']:
if not d['name']:
d['name'] = d['deviceID']
d['_shared_folders'] = {}
devices[d['deviceID']] = d
folders = dict()
for f in config['folders']:
if not f['label']:
f['label'] = f['id']
for d in f['devices']:
devices[d['deviceID']]['_shared_folders'][f['id']] = f
folders[f['id']] = f
results = []
matcher = Matcher(query.string)
# create device items
for device_id, d in devices.items():
device_name = d['name']
if match := matcher.match(device_name):
device_folders = ", ".join([f['label'] for f in d['_shared_folders'].values()])
actions = []
if d['paused']:
actions.append(Action("resume", "Resume", lambda did=device_id: self.st.resumeDevice(did)))
else:
actions.append(Action("pause", "Pause", lambda did=device_id: self.st.pauseDevice(did)))
item = StandardItem(
id=device_id,
text=f"{device_name}",
subtext=f"{'PAUSED · ' if d['paused'] else ''}Device · "
f"Shared: {device_folders if device_folders else 'Nothing'}.",
iconUrls=self.icon_urls_inactive if d['paused'] else self.icon_urls_active,
actions=actions
)
results.append(RankItem(item, match))
# create folder items
for folder_id, f in folders.items():
folder_name = f['label']
if match := matcher.match(folder_name):
folders_devices = ", ".join([devices[d['deviceID']]['name'] for d in f['devices']])
actions = []
if f['paused']:
actions.append(Action("resume", "Resume", lambda fid=folder_id: self.st.resumeFolder(fid)))
else:
actions.append(Action("pause", "Pause", lambda fid=folder_id: self.st.pauseFolder(fid)))
actions.append(Action("open", "Open", lambda p=f['path']: openFile(p)))
actions.append(Action("scan", "Scan", lambda fid=folder_id: self.st.scanFolder(fid)))
item = StandardItem(
id=folder_id,
text=folder_name,
subtext=f"{'PAUSED · ' if f['paused'] else ''}Folder · {f['path']} · "
f"Shared with {folders_devices if folders_devices else 'nobody'}.",
iconUrls=self.icon_urls_inactive if f['paused'] else self.icon_urls_active,
actions=actions
)
results.append(RankItem(item, match))
return results