fastled 1.2.68__py3-none-any.whl → 1.2.70__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/server_flask.py CHANGED
@@ -1,152 +1,152 @@
1
- import argparse
2
- from multiprocessing import Process
3
- from pathlib import Path
4
-
5
- from livereload import Server
6
-
7
-
8
- def _run_flask_server(
9
- fastled_js: Path,
10
- port: int,
11
- certfile: Path | None = None,
12
- keyfile: Path | None = None,
13
- ) -> None:
14
- """Run Flask server with live reload in a subprocess
15
-
16
- Args:
17
- fastled_js: Path to the fastled_js directory
18
- port: Port to run the server on
19
- certfile: Path to the SSL certificate file
20
- keyfile: Path to the SSL key file
21
- """
22
- try:
23
- from flask import Flask, send_from_directory
24
-
25
- app = Flask(__name__)
26
-
27
- # Must be a full path or flask will fail to find the file.
28
- fastled_js = fastled_js.resolve()
29
-
30
- @app.route("/")
31
- def serve_index():
32
- return send_from_directory(fastled_js, "index.html")
33
-
34
- @app.route("/<path:path>")
35
- def serve_files(path):
36
- response = send_from_directory(fastled_js, path)
37
- # Some servers don't set the Content-Type header for a bunch of files.
38
- if path.endswith(".js"):
39
- response.headers["Content-Type"] = "application/javascript"
40
- if path.endswith(".css"):
41
- response.headers["Content-Type"] = "text/css"
42
- if path.endswith(".wasm"):
43
- response.headers["Content-Type"] = "application/wasm"
44
- if path.endswith(".json"):
45
- response.headers["Content-Type"] = "application/json"
46
- if path.endswith(".png"):
47
- response.headers["Content-Type"] = "image/png"
48
- if path.endswith(".jpg"):
49
- response.headers["Content-Type"] = "image/jpeg"
50
- if path.endswith(".jpeg"):
51
- response.headers["Content-Type"] = "image/jpeg"
52
- if path.endswith(".gif"):
53
- response.headers["Content-Type"] = "image/gif"
54
- if path.endswith(".svg"):
55
- response.headers["Content-Type"] = "image/svg+xml"
56
- if path.endswith(".ico"):
57
- response.headers["Content-Type"] = "image/x-icon"
58
- if path.endswith(".html"):
59
- response.headers["Content-Type"] = "text/html"
60
-
61
- # now also add headers to force no caching
62
- response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
63
- response.headers["Pragma"] = "no-cache"
64
- response.headers["Expires"] = "0"
65
- return response
66
-
67
- server = Server(app.wsgi_app)
68
- # Watch index.html for changes
69
- server.watch(str(fastled_js / "index.html"))
70
- # server.watch(str(fastled_js / "index.js"))
71
- # server.watch(str(fastled_js / "index.css"))
72
- # Start the server
73
- server.serve(port=port, debug=True)
74
- except KeyboardInterrupt:
75
- import _thread
76
-
77
- _thread.interrupt_main()
78
- except Exception as e:
79
- print(f"Failed to run Flask server: {e}")
80
- import _thread
81
-
82
- _thread.interrupt_main()
83
-
84
-
85
- def run(
86
- port: int, cwd: Path, certfile: Path | None = None, keyfile: Path | None = None
87
- ) -> None:
88
- """Run the Flask server."""
89
- try:
90
- _run_flask_server(cwd, port, certfile, keyfile)
91
- import warnings
92
-
93
- warnings.warn("Flask server has stopped")
94
- except KeyboardInterrupt:
95
- import _thread
96
-
97
- _thread.interrupt_main()
98
- pass
99
-
100
-
101
- def parse_args() -> argparse.Namespace:
102
- """Parse the command line arguments."""
103
- parser = argparse.ArgumentParser(
104
- description="Open a browser to the fastled_js directory"
105
- )
106
- parser.add_argument(
107
- "fastled_js", type=Path, help="Path to the fastled_js directory"
108
- )
109
- parser.add_argument(
110
- "--port",
111
- "-p",
112
- type=int,
113
- required=True,
114
- help="Port to run the server on (default: %(default)s)",
115
- )
116
- parser.add_argument(
117
- "--certfile",
118
- type=Path,
119
- help="Path to the SSL certificate file for HTTPS",
120
- )
121
- parser.add_argument(
122
- "--keyfile",
123
- type=Path,
124
- help="Path to the SSL key file for HTTPS",
125
- )
126
- return parser.parse_args()
127
-
128
-
129
- def run_flask_server_process(
130
- port: int,
131
- cwd: Path | None = None,
132
- certfile: Path | None = None,
133
- keyfile: Path | None = None,
134
- ) -> Process:
135
- """Run the Flask server in a separate process."""
136
- cwd = cwd or Path(".")
137
- process = Process(
138
- target=run,
139
- args=(port, cwd, certfile, keyfile),
140
- )
141
- process.start()
142
- return process
143
-
144
-
145
- def main() -> None:
146
- """Main function."""
147
- args = parse_args()
148
- run(args.port, args.fastled_js, args.certfile, args.keyfile)
149
-
150
-
151
- if __name__ == "__main__":
152
- main()
1
+ import argparse
2
+ from multiprocessing import Process
3
+ from pathlib import Path
4
+
5
+ from livereload import Server
6
+
7
+
8
+ def _run_flask_server(
9
+ fastled_js: Path,
10
+ port: int,
11
+ certfile: Path | None = None,
12
+ keyfile: Path | None = None,
13
+ ) -> None:
14
+ """Run Flask server with live reload in a subprocess
15
+
16
+ Args:
17
+ fastled_js: Path to the fastled_js directory
18
+ port: Port to run the server on
19
+ certfile: Path to the SSL certificate file
20
+ keyfile: Path to the SSL key file
21
+ """
22
+ try:
23
+ from flask import Flask, send_from_directory
24
+
25
+ app = Flask(__name__)
26
+
27
+ # Must be a full path or flask will fail to find the file.
28
+ fastled_js = fastled_js.resolve()
29
+
30
+ @app.route("/")
31
+ def serve_index():
32
+ return send_from_directory(fastled_js, "index.html")
33
+
34
+ @app.route("/<path:path>")
35
+ def serve_files(path):
36
+ response = send_from_directory(fastled_js, path)
37
+ # Some servers don't set the Content-Type header for a bunch of files.
38
+ if path.endswith(".js"):
39
+ response.headers["Content-Type"] = "application/javascript"
40
+ if path.endswith(".css"):
41
+ response.headers["Content-Type"] = "text/css"
42
+ if path.endswith(".wasm"):
43
+ response.headers["Content-Type"] = "application/wasm"
44
+ if path.endswith(".json"):
45
+ response.headers["Content-Type"] = "application/json"
46
+ if path.endswith(".png"):
47
+ response.headers["Content-Type"] = "image/png"
48
+ if path.endswith(".jpg"):
49
+ response.headers["Content-Type"] = "image/jpeg"
50
+ if path.endswith(".jpeg"):
51
+ response.headers["Content-Type"] = "image/jpeg"
52
+ if path.endswith(".gif"):
53
+ response.headers["Content-Type"] = "image/gif"
54
+ if path.endswith(".svg"):
55
+ response.headers["Content-Type"] = "image/svg+xml"
56
+ if path.endswith(".ico"):
57
+ response.headers["Content-Type"] = "image/x-icon"
58
+ if path.endswith(".html"):
59
+ response.headers["Content-Type"] = "text/html"
60
+
61
+ # now also add headers to force no caching
62
+ response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
63
+ response.headers["Pragma"] = "no-cache"
64
+ response.headers["Expires"] = "0"
65
+ return response
66
+
67
+ server = Server(app.wsgi_app)
68
+ # Watch index.html for changes
69
+ server.watch(str(fastled_js / "index.html"))
70
+ # server.watch(str(fastled_js / "index.js"))
71
+ # server.watch(str(fastled_js / "index.css"))
72
+ # Start the server
73
+ server.serve(port=port, debug=True)
74
+ except KeyboardInterrupt:
75
+ import _thread
76
+
77
+ _thread.interrupt_main()
78
+ except Exception as e:
79
+ print(f"Failed to run Flask server: {e}")
80
+ import _thread
81
+
82
+ _thread.interrupt_main()
83
+
84
+
85
+ def run(
86
+ port: int, cwd: Path, certfile: Path | None = None, keyfile: Path | None = None
87
+ ) -> None:
88
+ """Run the Flask server."""
89
+ try:
90
+ _run_flask_server(cwd, port, certfile, keyfile)
91
+ import warnings
92
+
93
+ warnings.warn("Flask server has stopped")
94
+ except KeyboardInterrupt:
95
+ import _thread
96
+
97
+ _thread.interrupt_main()
98
+ pass
99
+
100
+
101
+ def parse_args() -> argparse.Namespace:
102
+ """Parse the command line arguments."""
103
+ parser = argparse.ArgumentParser(
104
+ description="Open a browser to the fastled_js directory"
105
+ )
106
+ parser.add_argument(
107
+ "fastled_js", type=Path, help="Path to the fastled_js directory"
108
+ )
109
+ parser.add_argument(
110
+ "--port",
111
+ "-p",
112
+ type=int,
113
+ required=True,
114
+ help="Port to run the server on (default: %(default)s)",
115
+ )
116
+ parser.add_argument(
117
+ "--certfile",
118
+ type=Path,
119
+ help="Path to the SSL certificate file for HTTPS",
120
+ )
121
+ parser.add_argument(
122
+ "--keyfile",
123
+ type=Path,
124
+ help="Path to the SSL key file for HTTPS",
125
+ )
126
+ return parser.parse_args()
127
+
128
+
129
+ def run_flask_server_process(
130
+ port: int,
131
+ cwd: Path | None = None,
132
+ certfile: Path | None = None,
133
+ keyfile: Path | None = None,
134
+ ) -> Process:
135
+ """Run the Flask server in a separate process."""
136
+ cwd = cwd or Path(".")
137
+ process = Process(
138
+ target=run,
139
+ args=(port, cwd, certfile, keyfile),
140
+ )
141
+ process.start()
142
+ return process
143
+
144
+
145
+ def main() -> None:
146
+ """Main function."""
147
+ args = parse_args()
148
+ run(args.port, args.fastled_js, args.certfile, args.keyfile)
149
+
150
+
151
+ if __name__ == "__main__":
152
+ main()