fastled 1.2.23__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fastled/types.py ADDED
@@ -0,0 +1,61 @@
1
+ import argparse
2
+ from dataclasses import dataclass
3
+ from enum import Enum
4
+ from typing import Any
5
+
6
+
7
+ @dataclass
8
+ class CompileResult:
9
+ success: bool
10
+ stdout: str
11
+ hash_value: str | None
12
+ zip_bytes: bytes
13
+
14
+ def __bool__(self) -> bool:
15
+ return self.success
16
+
17
+ def to_dict(self) -> dict[str, Any]:
18
+ return self.__dict__.copy()
19
+
20
+
21
+ class CompileServerError(Exception):
22
+ """Error class for failing to instantiate CompileServer."""
23
+
24
+ pass
25
+
26
+
27
+ class BuildMode(Enum):
28
+ DEBUG = "DEBUG"
29
+ QUICK = "QUICK"
30
+ RELEASE = "RELEASE"
31
+
32
+ @classmethod
33
+ def from_string(cls, mode_str: str) -> "BuildMode":
34
+ try:
35
+ return cls[mode_str.upper()]
36
+ except KeyError:
37
+ valid_modes = [mode.name for mode in cls]
38
+ raise ValueError(f"BUILD_MODE must be one of {valid_modes}, got {mode_str}")
39
+
40
+ @staticmethod
41
+ def from_args(args: argparse.Namespace) -> "BuildMode":
42
+ if args.debug:
43
+ return BuildMode.DEBUG
44
+ elif args.release:
45
+ return BuildMode.RELEASE
46
+ else:
47
+ return BuildMode.QUICK
48
+
49
+
50
+ class Platform(Enum):
51
+ WASM = "WASM"
52
+
53
+ @classmethod
54
+ def from_string(cls, platform_str: str) -> "Platform":
55
+ try:
56
+ return cls[platform_str.upper()]
57
+ except KeyError:
58
+ valid_modes = [mode.name for mode in cls]
59
+ raise ValueError(
60
+ f"Platform must be one of {valid_modes}, got {platform_str}"
61
+ )
fastled/util.py ADDED
@@ -0,0 +1,10 @@
1
+ import hashlib
2
+ from pathlib import Path
3
+
4
+
5
+ def hash_file(file_path: Path) -> str:
6
+ hasher = hashlib.sha256()
7
+ with open(file_path, "rb") as f:
8
+ for chunk in iter(lambda: f.read(4096), b""):
9
+ hasher.update(chunk)
10
+ return hasher.hexdigest()
fastled/web_compile.py ADDED
@@ -0,0 +1,285 @@
1
+ import _thread
2
+ import io
3
+ import json
4
+ import os
5
+ import shutil
6
+ import tempfile
7
+ import zipfile
8
+ from concurrent.futures import Future, ThreadPoolExecutor, as_completed
9
+ from dataclasses import dataclass
10
+ from pathlib import Path
11
+
12
+ import httpx
13
+
14
+ from fastled.settings import SERVER_PORT
15
+ from fastled.sketch import get_sketch_files
16
+ from fastled.types import BuildMode, CompileResult
17
+ from fastled.util import hash_file
18
+
19
+ DEFAULT_HOST = "https://fastled.onrender.com"
20
+ ENDPOINT_COMPILED_WASM = "compile/wasm"
21
+ _TIMEOUT = 60 * 4 # 2 mins timeout
22
+ _AUTH_TOKEN = "oBOT5jbsO4ztgrpNsQwlmFLIKB"
23
+ ENABLE_EMBEDDED_DATA = True
24
+ _EXECUTOR = ThreadPoolExecutor(max_workers=8)
25
+
26
+
27
+ @dataclass
28
+ class ConnectionResult:
29
+ host: str
30
+ success: bool
31
+ ipv4: bool
32
+
33
+
34
+ def _sanitize_host(host: str) -> str:
35
+ if host.startswith("http"):
36
+ return host
37
+ is_local_host = "localhost" in host or "127.0.0.1" in host or "0.0.0.0" in host
38
+ use_https = not is_local_host
39
+ if use_https:
40
+ return host if host.startswith("https://") else f"https://{host}"
41
+ return host if host.startswith("http://") else f"http://{host}"
42
+
43
+
44
+ def _test_connection(host: str, use_ipv4: bool) -> ConnectionResult:
45
+ # Function static cache
46
+ host = _sanitize_host(host)
47
+ transport = httpx.HTTPTransport(local_address="0.0.0.0") if use_ipv4 else None
48
+ try:
49
+ with httpx.Client(
50
+ timeout=_TIMEOUT,
51
+ transport=transport,
52
+ ) as test_client:
53
+ test_response = test_client.get(
54
+ f"{host}/healthz", timeout=3, follow_redirects=True
55
+ )
56
+ result = ConnectionResult(host, test_response.status_code == 200, use_ipv4)
57
+ except KeyboardInterrupt:
58
+ _thread.interrupt_main()
59
+
60
+ except TimeoutError:
61
+ result = ConnectionResult(host, False, use_ipv4)
62
+ except Exception:
63
+ result = ConnectionResult(host, False, use_ipv4)
64
+ return result
65
+
66
+
67
+ def _file_info(file_path: Path) -> str:
68
+ hash_txt = hash_file(file_path)
69
+ file_size = file_path.stat().st_size
70
+ json_str = json.dumps({"hash": hash_txt, "size": file_size})
71
+ return json_str
72
+
73
+
74
+ @dataclass
75
+ class ZipResult:
76
+ zip_bytes: bytes
77
+ zip_embedded_bytes: bytes | None
78
+ success: bool
79
+ error: str | None
80
+
81
+
82
+ def zip_files(directory: Path, build_mode: BuildMode) -> ZipResult | Exception:
83
+ print("Zipping files...")
84
+ try:
85
+ files = get_sketch_files(directory)
86
+ if not files:
87
+ raise FileNotFoundError(f"No files found in {directory}")
88
+ for f in files:
89
+ print(f"Adding file: {f}")
90
+ # Create in-memory zip file
91
+ has_embedded_zip = False
92
+ zip_embedded_buffer = io.BytesIO()
93
+ zip_buffer = io.BytesIO()
94
+ with zipfile.ZipFile(
95
+ zip_embedded_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
96
+ ) as emebedded_zip_file:
97
+ with zipfile.ZipFile(
98
+ zip_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
99
+ ) as zip_file:
100
+ for file_path in files:
101
+ relative_path = file_path.relative_to(directory)
102
+ achive_path = str(Path("wasm") / relative_path)
103
+ if str(relative_path).startswith("data") and ENABLE_EMBEDDED_DATA:
104
+ _file_info_str = _file_info(file_path)
105
+ zip_file.writestr(
106
+ achive_path + ".embedded.json", _file_info_str
107
+ )
108
+ emebedded_zip_file.write(file_path, relative_path)
109
+ has_embedded_zip = True
110
+ else:
111
+ zip_file.write(file_path, achive_path)
112
+ # write build mode into the file as build.txt so that sketches are fingerprinted
113
+ # based on the build mode. Otherwise the same sketch with different build modes
114
+ # will have the same fingerprint.
115
+ zip_file.writestr(
116
+ str(Path("wasm") / "build_mode.txt"), build_mode.value
117
+ )
118
+ result = ZipResult(
119
+ zip_bytes=zip_buffer.getvalue(),
120
+ zip_embedded_bytes=(
121
+ zip_embedded_buffer.getvalue() if has_embedded_zip else None
122
+ ),
123
+ success=True,
124
+ error=None,
125
+ )
126
+ return result
127
+ except Exception as e:
128
+ return e
129
+
130
+
131
+ def find_good_connection(
132
+ urls: list[str], filter_out_bad=True, use_ipv6: bool = True
133
+ ) -> ConnectionResult | None:
134
+ futures: list[Future] = []
135
+ for url in urls:
136
+
137
+ f = _EXECUTOR.submit(_test_connection, url, use_ipv4=True)
138
+ futures.append(f)
139
+ if use_ipv6 and "localhost" not in url:
140
+ f_v6 = _EXECUTOR.submit(_test_connection, url, use_ipv4=False)
141
+ futures.append(f_v6)
142
+
143
+ try:
144
+ # Return first successful result
145
+ for future in as_completed(futures):
146
+ result: ConnectionResult = future.result()
147
+ if result.success or not filter_out_bad:
148
+ return result
149
+ finally:
150
+ # Cancel any remaining futures
151
+ for future in futures:
152
+ future.cancel()
153
+ return None
154
+
155
+
156
+ def web_compile(
157
+ directory: Path | str,
158
+ host: str | None = None,
159
+ auth_token: str | None = None,
160
+ build_mode: BuildMode | None = None,
161
+ profile: bool = False,
162
+ ) -> CompileResult:
163
+ if isinstance(directory, str):
164
+ directory = Path(directory)
165
+ host = _sanitize_host(host or DEFAULT_HOST)
166
+ build_mode = build_mode or BuildMode.QUICK
167
+ print("Compiling on", host)
168
+ auth_token = auth_token or _AUTH_TOKEN
169
+ if not directory.exists():
170
+ raise FileNotFoundError(f"Directory not found: {directory}")
171
+ zip_result = zip_files(directory, build_mode=build_mode)
172
+ if isinstance(zip_result, Exception):
173
+ return CompileResult(
174
+ success=False, stdout=str(zip_result), hash_value=None, zip_bytes=b""
175
+ )
176
+ zip_bytes = zip_result.zip_bytes
177
+ archive_size = len(zip_bytes)
178
+ print(f"Web compiling on {host}...")
179
+ try:
180
+ host = _sanitize_host(host)
181
+ urls = [host]
182
+ domain = host.split("://")[-1]
183
+ if ":" not in domain:
184
+ urls.append(f"{host}:{SERVER_PORT}")
185
+
186
+ connection_result = find_good_connection(urls)
187
+ if connection_result is None:
188
+ print("Connection failed to all endpoints")
189
+ return CompileResult(
190
+ success=False,
191
+ stdout="Connection failed",
192
+ hash_value=None,
193
+ zip_bytes=b"",
194
+ )
195
+
196
+ ipv4_stmt = "IPv4" if connection_result.ipv4 else "IPv6"
197
+ transport = (
198
+ httpx.HTTPTransport(local_address="0.0.0.0")
199
+ if connection_result.ipv4
200
+ else None
201
+ )
202
+ with httpx.Client(
203
+ transport=transport,
204
+ timeout=_TIMEOUT,
205
+ ) as client:
206
+ headers = {
207
+ "accept": "application/json",
208
+ "authorization": auth_token,
209
+ "build": (
210
+ build_mode.value.lower()
211
+ if build_mode
212
+ else BuildMode.QUICK.value.lower()
213
+ ),
214
+ "profile": "true" if profile else "false",
215
+ }
216
+
217
+ url = f"{connection_result.host}/{ENDPOINT_COMPILED_WASM}"
218
+ print(f"Compiling on {url} via {ipv4_stmt}. Zip size: {archive_size} bytes")
219
+ files = {"file": ("wasm.zip", zip_bytes, "application/x-zip-compressed")}
220
+ response = client.post(
221
+ url,
222
+ follow_redirects=True,
223
+ files=files,
224
+ headers=headers,
225
+ timeout=_TIMEOUT,
226
+ )
227
+
228
+ if response.status_code != 200:
229
+ json_response = response.json()
230
+ detail = json_response.get("detail", "Could not compile")
231
+ return CompileResult(
232
+ success=False, stdout=detail, hash_value=None, zip_bytes=b""
233
+ )
234
+
235
+ print(f"Response status code: {response}")
236
+ # Create a temporary directory to extract the zip
237
+ with tempfile.TemporaryDirectory() as extract_dir:
238
+ extract_path = Path(extract_dir)
239
+
240
+ # Write the response content to a temporary zip file
241
+ temp_zip = extract_path / "response.zip"
242
+ temp_zip.write_bytes(response.content)
243
+
244
+ # Extract the zip
245
+ shutil.unpack_archive(temp_zip, extract_path, "zip")
246
+
247
+ if zip_result.zip_embedded_bytes:
248
+ # extract the embedded bytes, which were not sent to the server
249
+ temp_zip.write_bytes(zip_result.zip_embedded_bytes)
250
+ shutil.unpack_archive(temp_zip, extract_path, "zip")
251
+
252
+ # we don't need the temp zip anymore
253
+ temp_zip.unlink()
254
+
255
+ # Read stdout from out.txt if it exists
256
+ stdout_file = extract_path / "out.txt"
257
+ hash_file = extract_path / "hash.txt"
258
+ stdout = stdout_file.read_text() if stdout_file.exists() else ""
259
+ hash_value = hash_file.read_text() if hash_file.exists() else None
260
+
261
+ # now rezip the extracted files since we added the embedded json files
262
+ out_buffer = io.BytesIO()
263
+ with zipfile.ZipFile(
264
+ out_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
265
+ ) as out_zip:
266
+ for root, _, _files in os.walk(extract_path):
267
+ for file in _files:
268
+ file_path = Path(root) / file
269
+ relative_path = file_path.relative_to(extract_path)
270
+ out_zip.write(file_path, relative_path)
271
+
272
+ return CompileResult(
273
+ success=True,
274
+ stdout=stdout,
275
+ hash_value=hash_value,
276
+ zip_bytes=out_buffer.getvalue(),
277
+ )
278
+ except KeyboardInterrupt:
279
+ print("Keyboard interrupt")
280
+ raise
281
+ except httpx.HTTPError as e:
282
+ print(f"Error: {e}")
283
+ return CompileResult(
284
+ success=False, stdout=str(e), hash_value=None, zip_bytes=b""
285
+ )
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 zackees
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.