fastled 1.3.19__py3-none-any.whl → 1.3.21__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/open_browser.py CHANGED
@@ -1,137 +1,137 @@
1
- import atexit
2
- import random
3
- import sys
4
- import time
5
- import weakref
6
- from multiprocessing import Process
7
- from pathlib import Path
8
-
9
- from fastled.server_flask import run_flask_in_thread
10
-
11
- DEFAULT_PORT = 8089 # different than live version.
12
- PYTHON_EXE = sys.executable
13
-
14
- # Use a weak reference set to track processes without preventing garbage collection
15
- _WEAK_CLEANUP_SET = weakref.WeakSet()
16
-
17
-
18
- def add_cleanup(proc: Process) -> None:
19
- """Add a process to the cleanup list using weak references"""
20
- _WEAK_CLEANUP_SET.add(proc)
21
-
22
- # Register a cleanup function that checks if the process is still alive
23
- def cleanup_if_alive():
24
- if proc.is_alive():
25
- try:
26
- proc.terminate()
27
- proc.join(timeout=1.0)
28
- if proc.is_alive():
29
- proc.kill()
30
- except Exception:
31
- pass
32
-
33
- atexit.register(cleanup_if_alive)
34
-
35
-
36
- def is_port_free(port: int) -> bool:
37
- """Check if a port is free"""
38
- import httpx
39
-
40
- try:
41
- response = httpx.get(f"http://localhost:{port}", timeout=1)
42
- response.raise_for_status()
43
- return False
44
- except (httpx.HTTPError, httpx.ConnectError):
45
- return True
46
-
47
-
48
- def find_free_port(start_port: int) -> int:
49
- """Find a free port starting at start_port"""
50
- for port in range(start_port, start_port + 100, 2):
51
- if is_port_free(port):
52
- print(f"Found free port: {port}")
53
- return port
54
- else:
55
- print(f"Port {port} is in use, finding next")
56
- raise ValueError("Could not find a free port")
57
-
58
-
59
- def wait_for_server(port: int, timeout: int = 10) -> None:
60
- """Wait for the server to start."""
61
- from httpx import get
62
-
63
- future_time = time.time() + timeout
64
- while future_time > time.time():
65
- try:
66
- url = f"http://localhost:{port}"
67
- # print(f"Waiting for server to start at {url}")
68
- response = get(url, timeout=1)
69
- if response.status_code == 200:
70
- return
71
- except Exception:
72
- continue
73
- raise TimeoutError("Could not connect to server")
74
-
75
-
76
- def spawn_http_server(
77
- fastled_js: Path,
78
- compile_server_port: int,
79
- port: int | None = None,
80
- open_browser: bool = True,
81
- ) -> Process:
82
-
83
- if port is not None and not is_port_free(port):
84
- raise ValueError(f"Port {port} was specified but in use")
85
- if port is None:
86
- offset = random.randint(0, 100)
87
- port = find_free_port(DEFAULT_PORT + offset)
88
-
89
- # port: int,
90
- # cwd: Path,
91
- # compile_server_port: int,
92
- # certfile: Path | None = None,
93
- # keyfile: Path | None = None,
94
-
95
- proc = Process(
96
- target=run_flask_in_thread,
97
- args=(port, fastled_js, compile_server_port),
98
- daemon=True,
99
- )
100
- add_cleanup(proc)
101
- proc.start()
102
-
103
- # Add to cleanup set with weak reference
104
- add_cleanup(proc)
105
-
106
- wait_for_server(port)
107
- if open_browser:
108
- print(f"Opening browser to http://localhost:{port}")
109
- import webbrowser
110
-
111
- webbrowser.open(
112
- url=f"http://localhost:{port}",
113
- new=1,
114
- autoraise=True,
115
- )
116
- return proc
117
-
118
-
119
- if __name__ == "__main__":
120
- import argparse
121
-
122
- parser = argparse.ArgumentParser(
123
- description="Open a browser to the fastled_js directory"
124
- )
125
- parser.add_argument(
126
- "fastled_js", type=Path, help="Path to the fastled_js directory"
127
- )
128
- parser.add_argument(
129
- "--port",
130
- type=int,
131
- default=DEFAULT_PORT,
132
- help=f"Port to run the server on (default: {DEFAULT_PORT})",
133
- )
134
- args = parser.parse_args()
135
-
136
- proc = spawn_http_server(args.fastled_js, args.port, open_browser=True)
137
- proc.join()
1
+ import atexit
2
+ import random
3
+ import sys
4
+ import time
5
+ import weakref
6
+ from multiprocessing import Process
7
+ from pathlib import Path
8
+
9
+ from fastled.server_flask import run_flask_in_thread
10
+
11
+ DEFAULT_PORT = 8089 # different than live version.
12
+ PYTHON_EXE = sys.executable
13
+
14
+ # Use a weak reference set to track processes without preventing garbage collection
15
+ _WEAK_CLEANUP_SET = weakref.WeakSet()
16
+
17
+
18
+ def add_cleanup(proc: Process) -> None:
19
+ """Add a process to the cleanup list using weak references"""
20
+ _WEAK_CLEANUP_SET.add(proc)
21
+
22
+ # Register a cleanup function that checks if the process is still alive
23
+ def cleanup_if_alive():
24
+ if proc.is_alive():
25
+ try:
26
+ proc.terminate()
27
+ proc.join(timeout=1.0)
28
+ if proc.is_alive():
29
+ proc.kill()
30
+ except Exception:
31
+ pass
32
+
33
+ atexit.register(cleanup_if_alive)
34
+
35
+
36
+ def is_port_free(port: int) -> bool:
37
+ """Check if a port is free"""
38
+ import httpx
39
+
40
+ try:
41
+ response = httpx.get(f"http://localhost:{port}", timeout=1)
42
+ response.raise_for_status()
43
+ return False
44
+ except (httpx.HTTPError, httpx.ConnectError):
45
+ return True
46
+
47
+
48
+ def find_free_port(start_port: int) -> int:
49
+ """Find a free port starting at start_port"""
50
+ for port in range(start_port, start_port + 100, 2):
51
+ if is_port_free(port):
52
+ print(f"Found free port: {port}")
53
+ return port
54
+ else:
55
+ print(f"Port {port} is in use, finding next")
56
+ raise ValueError("Could not find a free port")
57
+
58
+
59
+ def wait_for_server(port: int, timeout: int = 10) -> None:
60
+ """Wait for the server to start."""
61
+ from httpx import get
62
+
63
+ future_time = time.time() + timeout
64
+ while future_time > time.time():
65
+ try:
66
+ url = f"http://localhost:{port}"
67
+ # print(f"Waiting for server to start at {url}")
68
+ response = get(url, timeout=1)
69
+ if response.status_code == 200:
70
+ return
71
+ except Exception:
72
+ continue
73
+ raise TimeoutError("Could not connect to server")
74
+
75
+
76
+ def spawn_http_server(
77
+ fastled_js: Path,
78
+ compile_server_port: int,
79
+ port: int | None = None,
80
+ open_browser: bool = True,
81
+ ) -> Process:
82
+
83
+ if port is not None and not is_port_free(port):
84
+ raise ValueError(f"Port {port} was specified but in use")
85
+ if port is None:
86
+ offset = random.randint(0, 100)
87
+ port = find_free_port(DEFAULT_PORT + offset)
88
+
89
+ # port: int,
90
+ # cwd: Path,
91
+ # compile_server_port: int,
92
+ # certfile: Path | None = None,
93
+ # keyfile: Path | None = None,
94
+
95
+ proc = Process(
96
+ target=run_flask_in_thread,
97
+ args=(port, fastled_js, compile_server_port),
98
+ daemon=True,
99
+ )
100
+ add_cleanup(proc)
101
+ proc.start()
102
+
103
+ # Add to cleanup set with weak reference
104
+ add_cleanup(proc)
105
+
106
+ wait_for_server(port)
107
+ if open_browser:
108
+ print(f"Opening browser to http://localhost:{port}")
109
+ import webbrowser
110
+
111
+ webbrowser.open(
112
+ url=f"http://localhost:{port}",
113
+ new=1,
114
+ autoraise=True,
115
+ )
116
+ return proc
117
+
118
+
119
+ if __name__ == "__main__":
120
+ import argparse
121
+
122
+ parser = argparse.ArgumentParser(
123
+ description="Open a browser to the fastled_js directory"
124
+ )
125
+ parser.add_argument(
126
+ "fastled_js", type=Path, help="Path to the fastled_js directory"
127
+ )
128
+ parser.add_argument(
129
+ "--port",
130
+ type=int,
131
+ default=DEFAULT_PORT,
132
+ help=f"Port to run the server on (default: {DEFAULT_PORT})",
133
+ )
134
+ args = parser.parse_args()
135
+
136
+ proc = spawn_http_server(args.fastled_js, args.port, open_browser=True)
137
+ proc.join()