-
Notifications
You must be signed in to change notification settings - Fork 75
/
label.py
62 lines (56 loc) · 2 KB
/
label.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
"""
Demo of a single-file app extending Specter functionality.
This app allows to set a label for the device.
"""
from app import BaseApp, AppError
from gui.screens import Prompt
from io import BytesIO
# Should be called App if you use a single file
class App(BaseApp):
"""Allows to set a label for the device."""
name = "label"
prefixes = [b"getlabel", b"setlabel"]
async def process_host_command(self, stream, show_screen):
"""
If command with one of the prefixes is received
it will be passed to this method.
Should return a tuple:
- stream (file, BytesIO etc)
- meta object with title and note
"""
# reads prefix from the stream (until first space)
prefix = self.get_prefix(stream)
if prefix == b"getlabel":
label = self.get_label()
obj = {"title": "Device's label is: %s" % label}
stream = BytesIO(label.encode())
return stream, obj
elif prefix == b"setlabel":
label = stream.read().strip().decode("ascii")
if not label:
raise AppError("Device label cannot be empty")
scr = Prompt(
"\n\nSet device label to: %s\n" % label,
"Current device label: %s" % self.get_label(),
)
res = await show_screen(scr)
if res is False:
return None
self.set_label(label)
obj = {"title": "New device label: %s" % label}
return BytesIO(label.encode()), obj
else:
raise AppError("Invalid command")
def get_label(self):
try:
with open(self.path + "/label") as f:
label = f.read()
return label
except Exception:
return "Specter-DIY"
def set_label(self, label):
try:
with open(self.path + "/label", "w") as f:
f.write(label)
except Exception:
return AppError("Failed to save new label")