Repository navigation
Expand file tree
/
Copy pathbutton_themes.py
More file actions
210 lines (187 loc) · 7 KB
/
Copy pathbutton_themes.py
File metadata and controls
210 lines (187 loc) · 7 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""Presentation-only button designs stored with the bar's private runtime data."""
import base64
import binascii
import json
import os
from pathlib import Path
import re
import struct
import tempfile
import threading
MAX_REQUEST = 160 * 1024
MAX_ICON_BYTES = 96 * 1024
FONTS = {"system", "rounded", "comic", "mono", "serif", "condensed", "cyber"}
TEXT_CASES = {
"original",
"uppercase",
"lowercase",
"capitalize",
"camel",
"pascal",
"snake",
"kebab",
}
ICONS = {"none", "image", "key", "star", "bolt", "heart", "smile"}
def fields(value, names):
if not isinstance(value, dict) or set(value) != set(names.split()):
raise ValueError("This button design has missing or unknown settings.")
return value
def number(value, low, high):
if type(value) not in (int, float) or not low <= value <= high:
raise ValueError("A button setting is outside its allowed range.")
return value
def choice(value, choices):
if not isinstance(value, str) or value not in choices:
raise ValueError("Unknown button style.")
return value
def color(value):
if not isinstance(value, str) or not re.fullmatch(r"#[0-9a-fA-F]{6}", value):
raise ValueError("Use a six-digit hex colour, such as #6088B0.")
return value.lower()
def gradient(value):
fields(value, "angle stops")
stops = value["stops"]
if not isinstance(stops, list) or not 2 <= len(stops) <= 6:
raise ValueError("A gradient needs between two and six colour stops.")
result = []
for stop in stops:
fields(stop, "color position")
result.append(
{
"color": color(stop["color"]),
"position": number(stop["position"], 0, 100),
}
)
return {"angle": number(value["angle"], 0, 360), "stops": result}
def icon_image(value):
if not isinstance(value, str):
raise ValueError("Choose an image for your icon.")
if not value:
return ""
prefix = "data:image/png;base64,"
if not value.startswith(prefix) or len(value) > MAX_ICON_BYTES * 4 // 3 + len(
prefix
):
raise ValueError("Icon images must be small PNG images.")
try:
raw = base64.b64decode(value[len(prefix) :], validate=True)
except (ValueError, binascii.Error) as error:
raise ValueError("This icon image could not be read.") from error
if (
len(raw) < 45
or not raw.startswith(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")
or not raw.endswith(b"\x00\x00\x00\x00IEND\xaeB`\x82")
):
raise ValueError("This icon image could not be read.")
width, height = struct.unpack(">II", raw[16:24])
if not 1 <= width <= 144 or not 1 <= height <= 144:
raise ValueError("Icon images must be resized to at most 144 pixels.")
return value
def label_backdrop(value):
if value is None:
return None
fields(value, "color opacity padding radius")
return {
"color": color(value["color"]),
"opacity": number(value["opacity"], 0, 100),
"padding": number(value["padding"], 0, 12),
"radius": number(value["radius"], 0, 20),
}
def validate_theme(value):
fields(value, "version background text icon")
if (
not isinstance(value["version"], int)
or isinstance(value["version"], bool)
or value["version"] != 1
):
raise ValueError("Unknown button design version.")
background = fields(value["background"], "kind color gradient speed intensity")
# Existing version-1 designs predate configurable label backdrops and casing.
text = value["text"]
if isinstance(text, dict):
text = {"backdrop": None, "case": "original", **text}
text = fields(text, "color gradient size font weight angle rotation backdrop case")
icon = fields(value["icon"], "kind image size rotation")
image = icon_image(icon["image"])
kind = choice(icon["kind"], ICONS)
if kind == "image" and not image:
raise ValueError("Choose an image for your icon.")
return {
"version": 1,
"background": {
"kind": choice(
background["kind"],
{
"solid",
"gradient",
"glitter",
"plasma",
"shader-lights",
"shader-snow",
"shader-cubes",
"shader-synthwave",
"shader-wonderful",
"shader-heartfelt",
"shader-star-nest",
"shader-clouds",
"shader-universe",
"shader-matrix",
},
),
"color": color(background["color"]),
"gradient": gradient(background["gradient"]),
"speed": number(background["speed"], 2, 60),
"intensity": number(background["intensity"], 0, 100),
},
"text": {
"color": color(text["color"]),
"gradient": (
gradient(text["gradient"]) if text["gradient"] is not None else None
),
"size": number(text["size"], 10, 48),
"font": choice(text["font"], FONTS),
"case": choice(text["case"], TEXT_CASES),
"weight": number(text["weight"], 400, 900),
"angle": number(text["angle"], 0, 360),
"rotation": number(text["rotation"], 0, 60),
"backdrop": label_backdrop(text["backdrop"]),
},
"icon": {
"kind": kind,
"image": image if kind == "image" else "",
"size": number(icon["size"], 20, 64),
"rotation": number(icon["rotation"], 0, 60),
},
}
class ButtonThemes:
write_lock = threading.Lock()
def __init__(self, path="data/revbank.buttons.json"):
self.path = Path(path)
def all(self):
try:
saved = json.loads(self.path.read_text(encoding="utf-8"))
except FileNotFoundError:
return {}
except (UnicodeError, json.JSONDecodeError) as error:
raise ValueError("Saved button designs could not be read.") from error
if not isinstance(saved, dict):
raise ValueError("Saved button designs could not be read.")
return {name: validate_theme(theme) for name, theme in saved.items()}
def save(self, user, theme):
theme = validate_theme(theme)
with self.write_lock:
saved = self.all()
saved[user] = theme
self.path.parent.mkdir(parents=True, exist_ok=True)
descriptor, temporary = tempfile.mkstemp(
prefix=self.path.name + ".", dir=self.path.parent, text=True
)
try:
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
json.dump(saved, output, ensure_ascii=True)
output.write("\n")
os.replace(temporary, self.path)
except Exception:
Path(temporary).unlink(missing_ok=True)
raise
return theme