Skip to content

Commit b79c35e

Browse files
committed
Reverted fixes and removed previous ones due to CP 9.0.0 fixing the issue on re module level
1 parent a5750b2 commit b79c35e

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

adafruit_templateengine.py

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -156,43 +156,38 @@ def safe_markdown(value: Any) -> str:
156156
_PRECOMPILED_BLOCK_PATTERN = re.compile(r"{% block \w+? %}")
157157
_PRECOMPILED_INCLUDE_PATTERN = re.compile(r"{% include '.+?' %}|{% include \".+?\" %}")
158158
_PRECOMPILED_HASH_COMMENT_PATTERN = re.compile(r"{# .+? #}")
159-
# Workaround for bug in re module https://github.com/adafruit/circuitpython/issues/8525
160159
_PRECOMPILED_BLOCK_COMMENT_PATTERN = re.compile(
161-
# TODO: Use r"{% comment ('.*?' |\".*?\" )?%}[\s\S]*?{% endcomment %}" without flags when fixed
162-
r"{% comment ('.*?' |\".*?\" )?%}.*?{% endcomment %}",
163-
16, # re.DOTALL flag
160+
r"{% comment ('.*?' |\".*?\" )?%}[\s\S]*?{% endcomment %}"
164161
)
165162
_PRECOMPILED_TOKEN_PATTERN = re.compile(r"{{ .+? }}|{% .+? %}")
166163

167164

168-
def _find_next_extends(template: bytes) -> "re.Match[bytes]":
165+
def _find_next_extends(template: str):
169166
return _PRECOMPILED_EXTENDS_PATTERN.search(template)
170167

171168

172-
def _find_next_block(template: bytes) -> "re.Match[bytes]":
169+
def _find_next_block(template: str):
173170
return _PRECOMPILED_BLOCK_PATTERN.search(template)
174171

175172

176-
def _find_next_include(template: bytes) -> "re.Match[bytes]":
173+
def _find_next_include(template: str):
177174
return _PRECOMPILED_INCLUDE_PATTERN.search(template)
178175

179176

180-
def _find_named_endblock(template: bytes, name: bytes) -> "re.Match[bytes]":
181-
return re.search(
182-
r"{% endblock ".encode("utf-8") + name + r" %}".encode("utf-8"), template
183-
)
177+
def _find_named_endblock(template: str, name: str):
178+
return re.search(r"{% endblock " + name + r" %}", template)
184179

185180

186-
def _exists_and_is_file(path: str) -> bool:
181+
def _exists_and_is_file(path: str):
187182
try:
188183
return (os.stat(path)[0] & 0b_11110000_00000000) == 0b_10000000_00000000
189184
except OSError:
190185
return False
191186

192187

193-
def _resolve_includes(template: bytes) -> bytes:
188+
def _resolve_includes(template: str):
194189
while (include_match := _find_next_include(template)) is not None:
195-
template_path = include_match.group(0)[12:-4].decode("utf-8")
190+
template_path = include_match.group(0)[12:-4]
196191

197192
# TODO: Restrict include to specific directory
198193

@@ -203,19 +198,19 @@ def _resolve_includes(template: bytes) -> bytes:
203198
with open(template_path, "rt", encoding="utf-8") as template_file:
204199
template = (
205200
template[: include_match.start()]
206-
+ template_file.read().encode("utf-8")
201+
+ template_file.read()
207202
+ template[include_match.end() :]
208203
)
209204
return template
210205

211206

212-
def _check_for_unsupported_nested_blocks(template: bytes):
207+
def _check_for_unsupported_nested_blocks(template: str):
213208
if _find_next_block(template) is not None:
214209
raise ValueError("Nested blocks are not supported")
215210

216211

217-
def _resolve_includes_blocks_and_extends(template: bytes) -> bytes:
218-
block_replacements: "dict[bytes, bytes]" = {}
212+
def _resolve_includes_blocks_and_extends(template: str):
213+
block_replacements: "dict[str, str]" = {}
219214

220215
# Processing nested child templates
221216
while (extends_match := _find_next_extends(template)) is not None:
@@ -225,7 +220,7 @@ def _resolve_includes_blocks_and_extends(template: bytes) -> bytes:
225220
with open(
226221
extended_template_name, "rt", encoding="utf-8"
227222
) as extended_template_file:
228-
extended_template = extended_template_file.read().encode("utf-8")
223+
extended_template = extended_template_file.read()
229224

230225
# Removed the extend tag
231226
template = template[extends_match.end() :]
@@ -248,7 +243,7 @@ def _resolve_includes_blocks_and_extends(template: bytes) -> bytes:
248243

249244
if block_name in block_replacements:
250245
block_replacements[block_name] = block_replacements[block_name].replace(
251-
r"{{ block.super }}".encode("utf-8"), block_content
246+
r"{{ block.super }}", block_content
252247
)
253248
else:
254249
block_replacements.setdefault(block_name, block_content)
@@ -265,16 +260,14 @@ def _resolve_includes_blocks_and_extends(template: bytes) -> bytes:
265260
return _replace_blocks_with_replacements(template, block_replacements)
266261

267262

268-
def _replace_blocks_with_replacements(
269-
template: bytes, replacements: "dict[bytes, bytes]"
270-
) -> bytes:
263+
def _replace_blocks_with_replacements(template: str, replacements: "dict[str, str]"):
271264
# Replace blocks in top-level template
272265
while (block_match := _find_next_block(template)) is not None:
273266
block_name = block_match.group(0)[9:-3]
274267

275268
# Self-closing block tag without default content
276269
if (endblock_match := _find_named_endblock(template, block_name)) is None:
277-
replacement = replacements.get(block_name, "".encode("utf-8"))
270+
replacement = replacements.get(block_name, "")
278271

279272
template = (
280273
template[: block_match.start()]
@@ -299,7 +292,7 @@ def _replace_blocks_with_replacements(
299292
# Replace default content with replacement
300293
else:
301294
replacement = replacements[block_name].replace(
302-
r"{{ block.super }}".encode("utf-8"), block_content
295+
r"{{ block.super }}", block_content
303296
)
304297

305298
template = (
@@ -311,15 +304,15 @@ def _replace_blocks_with_replacements(
311304
return template
312305

313306

314-
def _find_next_hash_comment(template: bytes) -> "re.Match[bytes]":
307+
def _find_next_hash_comment(template: str):
315308
return _PRECOMPILED_HASH_COMMENT_PATTERN.search(template)
316309

317310

318-
def _find_next_block_comment(template: bytes) -> "re.Match[bytes]":
311+
def _find_next_block_comment(template: str):
319312
return _PRECOMPILED_BLOCK_COMMENT_PATTERN.search(template)
320313

321314

322-
def _remove_comments(template: bytes) -> bytes:
315+
def _remove_comments(template: str):
323316
# Remove hash comments: {# ... #}
324317
while (comment_match := _find_next_hash_comment(template)) is not None:
325318
template = template[: comment_match.start()] + template[comment_match.end() :]
@@ -331,7 +324,7 @@ def _remove_comments(template: bytes) -> bytes:
331324
return template
332325

333326

334-
def _find_next_token(template: bytes) -> "re.Match[bytes]":
327+
def _find_next_token(template: str):
335328
return _PRECOMPILED_TOKEN_PATTERN.search(template)
336329

337330

@@ -343,10 +336,6 @@ def _create_template_function( # pylint: disable=,too-many-locals,too-many-bran
343336
context_name: str = "context",
344337
dry_run: bool = False,
345338
) -> "Generator[str] | str":
346-
# Workaround for bug in re module https://github.com/adafruit/circuitpython/issues/6860
347-
# TODO: Remove .encode() and .decode() when bug is fixed
348-
template: bytes = template.encode("utf-8")
349-
350339
# Resolve includes, blocks and extends
351340
template = _resolve_includes_blocks_and_extends(template)
352341

@@ -363,10 +352,10 @@ def _create_template_function( # pylint: disable=,too-many-locals,too-many-bran
363352

364353
# Resolve tokens
365354
while (token_match := _find_next_token(template)) is not None:
366-
token: str = token_match.group(0).decode("utf-8")
355+
token = token_match.group(0)
367356

368357
# Add the text before the token
369-
if text_before_token := template[: token_match.start()].decode("utf-8"):
358+
if text_before_token := template[: token_match.start()]:
370359
function_string += (
371360
indent * indentation_level + f"yield {repr(text_before_token)}\n"
372361
)
@@ -457,9 +446,7 @@ def _create_template_function( # pylint: disable=,too-many-locals,too-many-bran
457446

458447
# Add the text after the last token (if any)
459448
if template:
460-
function_string += (
461-
indent * indentation_level + f"yield {repr(template.decode('utf-8'))}\n" #
462-
)
449+
function_string += indent * indentation_level + f"yield {repr(template)}\n"
463450

464451
# If dry run, return the template function string
465452
if dry_run:

0 commit comments

Comments
 (0)