fastled 1.3.15__py3-none-any.whl → 1.3.16__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/print_filter.py CHANGED
@@ -1,247 +1,247 @@
1
- import re
2
- import zlib
3
- from abc import ABC, abstractmethod
4
- from dataclasses import dataclass
5
- from enum import Enum
6
-
7
-
8
- class PrintFilter(ABC):
9
- """Abstract base class for filtering text output."""
10
-
11
- def __init__(self, echo: bool = True) -> None:
12
- self.echo = echo
13
-
14
- @abstractmethod
15
- def filter(self, text: str) -> str:
16
- """Filter the text according to implementation-specific rules."""
17
- pass
18
-
19
- def print(self, text: str | bytes) -> str:
20
- """Prints the text to the console after filtering."""
21
- if isinstance(text, bytes):
22
- text = text.decode("utf-8")
23
- text = self.filter(text)
24
- if self.echo:
25
- print(text, end="")
26
- return text
27
-
28
-
29
- def _handle_ino_cpp(line: str) -> str:
30
- if ".ino.cpp" in line[0:30]:
31
- # Extract the filename without path and extension
32
- match = re.search(r"src/([^/]+)\.ino\.cpp", line)
33
- if match:
34
- filename = match.group(1)
35
- # Replace with examples/Filename/Filename.ino format
36
- line = line.replace(
37
- f"src/{filename}.ino.cpp", f"examples/{filename}/{filename}.ino"
38
- )
39
- else:
40
- # Fall back to simple extension replacement if regex doesn't match
41
- line = line.replace(".ino.cpp", ".ino")
42
- return line
43
-
44
-
45
- def _handle_fastled_src(line: str) -> str:
46
- return line.replace("fastled/src", "src")
47
-
48
-
49
- class PrintFilterDefault(PrintFilter):
50
- """Provides default filtering for FastLED output."""
51
-
52
- def filter(self, text: str) -> str:
53
- return text
54
-
55
-
56
- class PrintFilterFastled(PrintFilter):
57
- """Provides filtering for FastLED output so that source files match up with local names."""
58
-
59
- def __init__(self, echo: bool = True) -> None:
60
- super().__init__(echo)
61
- self.build_started = False
62
- # self.compile_link_active = False
63
- # self.compile_link_filter:
64
-
65
- def filter(self, text: str) -> str:
66
- lines = text.splitlines()
67
- out: list[str] = []
68
- for line in lines:
69
- ## DEBUG DO NOT SUBMIT
70
- # print(line)
71
- if "# WASM is building" in line:
72
- self.build_started = True
73
- line = _handle_fastled_src(
74
- line
75
- ) # Always convert fastled/src to src for file matchups.
76
- if self.build_started or " error: " in line:
77
- line = _handle_ino_cpp(line)
78
- out.append(line)
79
- text = "\n".join(out)
80
- return text
81
-
82
-
83
- class CompileOrLink(Enum):
84
- COMPILE = "compile"
85
- LINK = "link"
86
-
87
-
88
- @dataclass
89
- class BuildArtifact:
90
- timestamp: float
91
- input_artifact: str | None
92
- output_artifact: str | None
93
- build_flags: str
94
- compile_or_link: CompileOrLink
95
- hash: int
96
-
97
- def flags_pretty(self) -> str:
98
- """
99
- Returns the flags in a pretty format.
100
- This is used for printing the flags to the console.
101
- """
102
- flags = self.build_flags
103
- flags = flags.replace(" -I", "\n-I")
104
- flags = flags.replace(" -D", "\n-D")
105
- flags = flags.replace(" -l", "\n-l")
106
- flags = flags.replace(" -L", "\n-L")
107
- flags = flags.replace(" -o", "\n-o")
108
- flags = flags.replace(" -W", "\n-W")
109
- flags = flags.replace(" -f", "\n-f")
110
- flags = flags.replace(" -g", "\n-g")
111
-
112
- # break into lines and sort
113
- lines = flags.splitlines()
114
- first_line = lines[0]
115
- lines.pop(0) # remove first line
116
- lines = sorted(lines)
117
- # remove duplicates
118
- lines = list(dict.fromkeys(lines))
119
- # remove empty lines
120
- lines = [line for line in lines if line.strip() != ""]
121
- # remove leading and trailing whitespace
122
- lines = [line.strip() for line in lines]
123
- lines = sorted(lines)
124
- lines = [first_line] + lines # add first line back to the beginning
125
- # stringify
126
- flags = "\n".join(lines)
127
- return flags
128
-
129
- def __str__(self) -> str:
130
- return f"{self.brief()} {self.build_flags} {self.compile_or_link} {self.hash}"
131
-
132
- def brief(self) -> str:
133
- return f"{self.timestamp:.2f} {self.output_artifact}"
134
-
135
- def begin_flags(self) -> str:
136
- """
137
- Returns the flags that are used to begin a build.
138
- This is the flags that are used for the first compile or link.
139
- """
140
-
141
- out: str = (
142
- "\n################ NEW COMPILE/LINK FLAG GROUP #####################\n\n"
143
- )
144
- out += f"{self.flags_pretty()}\n"
145
- return out
146
-
147
- def end_flags(self) -> str:
148
- """
149
- Returns the flags that are used to end a build.
150
- This is the flags that are used for the last compile or link.
151
- """
152
- out: str = (
153
- "\n################ END COMPILE/LINK FLAG GROUP #####################\n"
154
- )
155
- return out
156
-
157
- @staticmethod
158
- def parse(input_str: str) -> "BuildArtifact | None":
159
- """
160
- Parse a single build-log line of the form:
161
- "<timestamp> ... <some .cpp or .h file> ... <flags>"
162
-
163
- Returns a BuildArtifact, or None if parsing failed.
164
- """
165
- return _parse(input_str)
166
-
167
-
168
- class TokenFilter(ABC):
169
- @abstractmethod
170
- def extract(self, tokens: list[str]) -> str | None:
171
- """
172
- Scan `tokens`, remove any tokens this filter is responsible for,
173
- and return the extracted string (or None if not found/invalid).
174
- """
175
- ...
176
-
177
-
178
- class TimestampFilter(TokenFilter):
179
- def extract(self, tokens: list[str]) -> str | None:
180
- if not tokens:
181
- return None
182
- candidate = tokens[0]
183
- try:
184
- _ = float(candidate)
185
- return tokens.pop(0)
186
- except ValueError:
187
- return None
188
-
189
-
190
- class InputArtifactFilter(TokenFilter):
191
- def extract(self, tokens: list[str]) -> str | None:
192
- for i, tok in enumerate(tokens):
193
- if tok.endswith(".cpp") or tok.endswith(".h"):
194
- return tokens.pop(i)
195
- return None
196
-
197
-
198
- class OutputArtifactFilter(TokenFilter):
199
- def extract(self, tokens: list[str]) -> str | None:
200
- for i, tok in enumerate(tokens):
201
- if tok == "-o" and i + 1 < len(tokens):
202
- tokens.pop(i) # drop '-o'
203
- return tokens.pop(i) # drop & return artifact
204
- return None
205
-
206
-
207
- class ActionFilter(TokenFilter):
208
- def extract(self, tokens: list[str]) -> str | None:
209
- if "-c" in tokens:
210
- return CompileOrLink.COMPILE.value
211
- return CompileOrLink.LINK.value
212
-
213
-
214
- def _parse(line: str) -> BuildArtifact | None:
215
- tokens = line.strip().split()
216
- if not tokens:
217
- return None
218
-
219
- # instantiate in the order we need them
220
- filters: list[TokenFilter] = [
221
- TimestampFilter(),
222
- InputArtifactFilter(),
223
- OutputArtifactFilter(),
224
- ActionFilter(),
225
- ]
226
-
227
- # apply each filter
228
- raw_ts = filters[0].extract(tokens)
229
- raw_in = filters[1].extract(tokens)
230
- raw_out = filters[2].extract(tokens)
231
- raw_act = filters[3].extract(tokens)
232
-
233
- if raw_ts is None or raw_in is None or raw_act is None:
234
- return None
235
-
236
- # the rest of `tokens` are the flags
237
- flags_str = " ".join(tokens)
238
- h = zlib.adler32(flags_str.encode("utf-8"))
239
-
240
- return BuildArtifact(
241
- timestamp=float(raw_ts),
242
- input_artifact=raw_in,
243
- output_artifact=raw_out,
244
- build_flags=flags_str,
245
- compile_or_link=CompileOrLink(raw_act),
246
- hash=h,
247
- )
1
+ import re
2
+ import zlib
3
+ from abc import ABC, abstractmethod
4
+ from dataclasses import dataclass
5
+ from enum import Enum
6
+
7
+
8
+ class PrintFilter(ABC):
9
+ """Abstract base class for filtering text output."""
10
+
11
+ def __init__(self, echo: bool = True) -> None:
12
+ self.echo = echo
13
+
14
+ @abstractmethod
15
+ def filter(self, text: str) -> str:
16
+ """Filter the text according to implementation-specific rules."""
17
+ pass
18
+
19
+ def print(self, text: str | bytes) -> str:
20
+ """Prints the text to the console after filtering."""
21
+ if isinstance(text, bytes):
22
+ text = text.decode("utf-8")
23
+ text = self.filter(text)
24
+ if self.echo:
25
+ print(text, end="")
26
+ return text
27
+
28
+
29
+ def _handle_ino_cpp(line: str) -> str:
30
+ if ".ino.cpp" in line[0:30]:
31
+ # Extract the filename without path and extension
32
+ match = re.search(r"src/([^/]+)\.ino\.cpp", line)
33
+ if match:
34
+ filename = match.group(1)
35
+ # Replace with examples/Filename/Filename.ino format
36
+ line = line.replace(
37
+ f"src/{filename}.ino.cpp", f"examples/{filename}/{filename}.ino"
38
+ )
39
+ else:
40
+ # Fall back to simple extension replacement if regex doesn't match
41
+ line = line.replace(".ino.cpp", ".ino")
42
+ return line
43
+
44
+
45
+ def _handle_fastled_src(line: str) -> str:
46
+ return line.replace("fastled/src", "src")
47
+
48
+
49
+ class PrintFilterDefault(PrintFilter):
50
+ """Provides default filtering for FastLED output."""
51
+
52
+ def filter(self, text: str) -> str:
53
+ return text
54
+
55
+
56
+ class PrintFilterFastled(PrintFilter):
57
+ """Provides filtering for FastLED output so that source files match up with local names."""
58
+
59
+ def __init__(self, echo: bool = True) -> None:
60
+ super().__init__(echo)
61
+ self.build_started = False
62
+ # self.compile_link_active = False
63
+ # self.compile_link_filter:
64
+
65
+ def filter(self, text: str) -> str:
66
+ lines = text.splitlines()
67
+ out: list[str] = []
68
+ for line in lines:
69
+ ## DEBUG DO NOT SUBMIT
70
+ # print(line)
71
+ if "# WASM is building" in line:
72
+ self.build_started = True
73
+ line = _handle_fastled_src(
74
+ line
75
+ ) # Always convert fastled/src to src for file matchups.
76
+ if self.build_started or " error: " in line:
77
+ line = _handle_ino_cpp(line)
78
+ out.append(line)
79
+ text = "\n".join(out)
80
+ return text
81
+
82
+
83
+ class CompileOrLink(Enum):
84
+ COMPILE = "compile"
85
+ LINK = "link"
86
+
87
+
88
+ @dataclass
89
+ class BuildArtifact:
90
+ timestamp: float
91
+ input_artifact: str | None
92
+ output_artifact: str | None
93
+ build_flags: str
94
+ compile_or_link: CompileOrLink
95
+ hash: int
96
+
97
+ def flags_pretty(self) -> str:
98
+ """
99
+ Returns the flags in a pretty format.
100
+ This is used for printing the flags to the console.
101
+ """
102
+ flags = self.build_flags
103
+ flags = flags.replace(" -I", "\n-I")
104
+ flags = flags.replace(" -D", "\n-D")
105
+ flags = flags.replace(" -l", "\n-l")
106
+ flags = flags.replace(" -L", "\n-L")
107
+ flags = flags.replace(" -o", "\n-o")
108
+ flags = flags.replace(" -W", "\n-W")
109
+ flags = flags.replace(" -f", "\n-f")
110
+ flags = flags.replace(" -g", "\n-g")
111
+
112
+ # break into lines and sort
113
+ lines = flags.splitlines()
114
+ first_line = lines[0]
115
+ lines.pop(0) # remove first line
116
+ lines = sorted(lines)
117
+ # remove duplicates
118
+ lines = list(dict.fromkeys(lines))
119
+ # remove empty lines
120
+ lines = [line for line in lines if line.strip() != ""]
121
+ # remove leading and trailing whitespace
122
+ lines = [line.strip() for line in lines]
123
+ lines = sorted(lines)
124
+ lines = [first_line] + lines # add first line back to the beginning
125
+ # stringify
126
+ flags = "\n".join(lines)
127
+ return flags
128
+
129
+ def __str__(self) -> str:
130
+ return f"{self.brief()} {self.build_flags} {self.compile_or_link} {self.hash}"
131
+
132
+ def brief(self) -> str:
133
+ return f"{self.timestamp:.2f} {self.output_artifact}"
134
+
135
+ def begin_flags(self) -> str:
136
+ """
137
+ Returns the flags that are used to begin a build.
138
+ This is the flags that are used for the first compile or link.
139
+ """
140
+
141
+ out: str = (
142
+ "\n################ NEW COMPILE/LINK FLAG GROUP #####################\n\n"
143
+ )
144
+ out += f"{self.flags_pretty()}\n"
145
+ return out
146
+
147
+ def end_flags(self) -> str:
148
+ """
149
+ Returns the flags that are used to end a build.
150
+ This is the flags that are used for the last compile or link.
151
+ """
152
+ out: str = (
153
+ "\n################ END COMPILE/LINK FLAG GROUP #####################\n"
154
+ )
155
+ return out
156
+
157
+ @staticmethod
158
+ def parse(input_str: str) -> "BuildArtifact | None":
159
+ """
160
+ Parse a single build-log line of the form:
161
+ "<timestamp> ... <some .cpp or .h file> ... <flags>"
162
+
163
+ Returns a BuildArtifact, or None if parsing failed.
164
+ """
165
+ return _parse(input_str)
166
+
167
+
168
+ class TokenFilter(ABC):
169
+ @abstractmethod
170
+ def extract(self, tokens: list[str]) -> str | None:
171
+ """
172
+ Scan `tokens`, remove any tokens this filter is responsible for,
173
+ and return the extracted string (or None if not found/invalid).
174
+ """
175
+ ...
176
+
177
+
178
+ class TimestampFilter(TokenFilter):
179
+ def extract(self, tokens: list[str]) -> str | None:
180
+ if not tokens:
181
+ return None
182
+ candidate = tokens[0]
183
+ try:
184
+ _ = float(candidate)
185
+ return tokens.pop(0)
186
+ except ValueError:
187
+ return None
188
+
189
+
190
+ class InputArtifactFilter(TokenFilter):
191
+ def extract(self, tokens: list[str]) -> str | None:
192
+ for i, tok in enumerate(tokens):
193
+ if tok.endswith(".cpp") or tok.endswith(".h"):
194
+ return tokens.pop(i)
195
+ return None
196
+
197
+
198
+ class OutputArtifactFilter(TokenFilter):
199
+ def extract(self, tokens: list[str]) -> str | None:
200
+ for i, tok in enumerate(tokens):
201
+ if tok == "-o" and i + 1 < len(tokens):
202
+ tokens.pop(i) # drop '-o'
203
+ return tokens.pop(i) # drop & return artifact
204
+ return None
205
+
206
+
207
+ class ActionFilter(TokenFilter):
208
+ def extract(self, tokens: list[str]) -> str | None:
209
+ if "-c" in tokens:
210
+ return CompileOrLink.COMPILE.value
211
+ return CompileOrLink.LINK.value
212
+
213
+
214
+ def _parse(line: str) -> BuildArtifact | None:
215
+ tokens = line.strip().split()
216
+ if not tokens:
217
+ return None
218
+
219
+ # instantiate in the order we need them
220
+ filters: list[TokenFilter] = [
221
+ TimestampFilter(),
222
+ InputArtifactFilter(),
223
+ OutputArtifactFilter(),
224
+ ActionFilter(),
225
+ ]
226
+
227
+ # apply each filter
228
+ raw_ts = filters[0].extract(tokens)
229
+ raw_in = filters[1].extract(tokens)
230
+ raw_out = filters[2].extract(tokens)
231
+ raw_act = filters[3].extract(tokens)
232
+
233
+ if raw_ts is None or raw_in is None or raw_act is None:
234
+ return None
235
+
236
+ # the rest of `tokens` are the flags
237
+ flags_str = " ".join(tokens)
238
+ h = zlib.adler32(flags_str.encode("utf-8"))
239
+
240
+ return BuildArtifact(
241
+ timestamp=float(raw_ts),
242
+ input_artifact=raw_in,
243
+ output_artifact=raw_out,
244
+ build_flags=flags_str,
245
+ compile_or_link=CompileOrLink(raw_act),
246
+ hash=h,
247
+ )