From d251bdc9e64a3eb8d313df33b6f0eb659933ba53 Mon Sep 17 00:00:00 2001 From: michalpokusa <72110769+michalpokusa@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:11:11 +0000 Subject: [PATCH 1/5] Fix: TypeError with chunk_size=None in rendering iter functions --- adafruit_templateengine.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/adafruit_templateengine.py b/adafruit_templateengine.py index bf34f44..ed913d0 100644 --- a/adafruit_templateengine.py +++ b/adafruit_templateengine.py @@ -804,18 +804,14 @@ def render_string_iter( key = hash(template_string) if cache and key in _CACHE: - return _yield_as_sized_chunks( - _CACHE[key].render_iter(context or {}, chunk_size), chunk_size - ) + return _CACHE[key].render_iter(context or {}, chunk_size=chunk_size) template = Template(template_string) if cache: _CACHE[key] = template - return _yield_as_sized_chunks( - template.render_iter(context or {}), chunk_size=chunk_size - ) + return template.render_iter(context or {}, chunk_size=chunk_size) def render_string( @@ -882,18 +878,14 @@ def render_template_iter( key = hash(template_path) if cache and key in _CACHE: - return _yield_as_sized_chunks( - _CACHE[key].render_iter(context or {}, chunk_size), chunk_size - ) + return _CACHE[key].render_iter(context or {}, chunk_size=chunk_size) template = FileTemplate(template_path) if cache: _CACHE[key] = template - return _yield_as_sized_chunks( - template.render_iter(context or {}, chunk_size=chunk_size), chunk_size - ) + return template.render_iter(context or {}, chunk_size=chunk_size) def render_template( From f6ce97bf77c7c52d151964febf23a95568cd30ff Mon Sep 17 00:00:00 2001 From: michalpokusa <72110769+michalpokusa@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:20:28 +0000 Subject: [PATCH 2/5] Renamed _CACHE to CACHED_TEMPLATES --- adafruit_templateengine.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/adafruit_templateengine.py b/adafruit_templateengine.py index ed913d0..335cb12 100644 --- a/adafruit_templateengine.py +++ b/adafruit_templateengine.py @@ -771,7 +771,7 @@ def __init__(self, template_path: str) -> None: super().__init__(template_string) -_CACHE: "dict[int, Template| FileTemplate]" = {} +CACHED_TEMPLATES: "dict[int, Template| FileTemplate]" = {} def render_string_iter( @@ -803,13 +803,13 @@ def render_string_iter( """ key = hash(template_string) - if cache and key in _CACHE: - return _CACHE[key].render_iter(context or {}, chunk_size=chunk_size) + if cache and key in CACHED_TEMPLATES: + return CACHED_TEMPLATES[key].render_iter(context or {}, chunk_size=chunk_size) template = Template(template_string) if cache: - _CACHE[key] = template + CACHED_TEMPLATES[key] = template return template.render_iter(context or {}, chunk_size=chunk_size) @@ -837,13 +837,13 @@ def render_string( """ key = hash(template_string) - if cache and key in _CACHE: - return _CACHE[key].render(context or {}) + if cache and key in CACHED_TEMPLATES: + return CACHED_TEMPLATES[key].render(context or {}) template = Template(template_string) if cache: - _CACHE[key] = template + CACHED_TEMPLATES[key] = template return template.render(context or {}) @@ -877,13 +877,13 @@ def render_template_iter( """ key = hash(template_path) - if cache and key in _CACHE: - return _CACHE[key].render_iter(context or {}, chunk_size=chunk_size) + if cache and key in CACHED_TEMPLATES: + return CACHED_TEMPLATES[key].render_iter(context or {}, chunk_size=chunk_size) template = FileTemplate(template_path) if cache: - _CACHE[key] = template + CACHED_TEMPLATES[key] = template return template.render_iter(context or {}, chunk_size=chunk_size) @@ -912,12 +912,12 @@ def render_template( key = hash(template_path) - if cache and key in _CACHE: - return _CACHE[key].render(context or {}) + if cache and key in CACHED_TEMPLATES: + return CACHED_TEMPLATES[key].render(context or {}) template = FileTemplate(template_path) if cache: - _CACHE[key] = template + CACHED_TEMPLATES[key] = template return template.render(context or {}) From 6c8d5585760d92b3d110ebcf9def778f8165901a Mon Sep 17 00:00:00 2001 From: michalpokusa <72110769+michalpokusa@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:44:33 +0000 Subject: [PATCH 3/5] Fix: Negative number of skipped lines were displayed --- adafruit_templateengine.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/adafruit_templateengine.py b/adafruit_templateengine.py index 335cb12..4e6cb0d 100644 --- a/adafruit_templateengine.py +++ b/adafruit_templateengine.py @@ -94,18 +94,22 @@ def _underline_token_in_template( template_before_token = token.template[: token.start_position] if skipped_lines := template_before_token.count("\n") - lines_around: - template_before_token = ( - f"{cls._skipped_lines_message(skipped_lines)}\n" - + "\n".join(template_before_token.split("\n")[-(lines_around + 1) :]) + template_before_token = "\n".join( + template_before_token.split("\n")[-(lines_around + 1) :] ) + if 0 < skipped_lines: + template_before_token = f"{cls._skipped_lines_message(skipped_lines)}\n{template_before_token}" + template_after_token = token.template[token.end_position :] if skipped_lines := template_after_token.count("\n") - lines_around: - template_after_token = ( - "\n".join(template_after_token.split("\n")[: (lines_around + 1)]) - + f"\n{cls._skipped_lines_message(skipped_lines)}" + template_after_token = "\n".join( + template_after_token.split("\n")[: (lines_around + 1)] ) + if 0 < skipped_lines: + template_after_token += f"\n{cls._skipped_lines_message(skipped_lines)}" + lines_before_line_with_token = template_before_token.rsplit("\n", 1)[0] line_with_token = ( @@ -122,7 +126,7 @@ def _underline_token_in_template( lines_after_line_with_token = template_after_token.split("\n", 1)[-1] - return "\n".join( + return "\n" + "\n".join( [ lines_before_line_with_token, line_with_token, From b8782a27e151ab7d2879f5cf9e98efdf2d5041a0 Mon Sep 17 00:00:00 2001 From: michalpokusa <72110769+michalpokusa@users.noreply.github.com> Date: Wed, 30 Oct 2024 22:38:41 +0000 Subject: [PATCH 4/5] Minor refactor and rename of variables to pass pylint --- adafruit_templateengine.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/adafruit_templateengine.py b/adafruit_templateengine.py index 4e6cb0d..270b914 100644 --- a/adafruit_templateengine.py +++ b/adafruit_templateengine.py @@ -93,22 +93,32 @@ def _underline_token_in_template( """ template_before_token = token.template[: token.start_position] - if skipped_lines := template_before_token.count("\n") - lines_around: + if top_skipped_lines := template_before_token.count("\n") - lines_around: template_before_token = "\n".join( template_before_token.split("\n")[-(lines_around + 1) :] ) - if 0 < skipped_lines: - template_before_token = f"{cls._skipped_lines_message(skipped_lines)}\n{template_before_token}" + if 0 < top_skipped_lines: + before_skipped_lines_message = cls._skipped_lines_message( + top_skipped_lines + ) + template_before_token = ( + f"{before_skipped_lines_message}\n{template_before_token}" + ) template_after_token = token.template[token.end_position :] - if skipped_lines := template_after_token.count("\n") - lines_around: + if bottom_skipped_lines := template_after_token.count("\n") - lines_around: template_after_token = "\n".join( template_after_token.split("\n")[: (lines_around + 1)] ) - if 0 < skipped_lines: - template_after_token += f"\n{cls._skipped_lines_message(skipped_lines)}" + if 0 < bottom_skipped_lines: + after_skipped_lines_message = cls._skipped_lines_message( + bottom_skipped_lines + ) + template_after_token = ( + f"{template_after_token}\n{after_skipped_lines_message}" + ) lines_before_line_with_token = template_before_token.rsplit("\n", 1)[0] From 94a54c787f0004aec6a428f0ba7b926d2aedd6e3 Mon Sep 17 00:00:00 2001 From: michalpokusa <72110769+michalpokusa@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:14:20 +0000 Subject: [PATCH 5/5] Refactored variable names to be consistent --- adafruit_templateengine.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_templateengine.py b/adafruit_templateengine.py index 270b914..90a3eaf 100644 --- a/adafruit_templateengine.py +++ b/adafruit_templateengine.py @@ -99,11 +99,11 @@ def _underline_token_in_template( ) if 0 < top_skipped_lines: - before_skipped_lines_message = cls._skipped_lines_message( + top_skipped_lines_message = cls._skipped_lines_message( top_skipped_lines ) template_before_token = ( - f"{before_skipped_lines_message}\n{template_before_token}" + f"{top_skipped_lines_message}\n{template_before_token}" ) template_after_token = token.template[token.end_position :] @@ -113,11 +113,11 @@ def _underline_token_in_template( ) if 0 < bottom_skipped_lines: - after_skipped_lines_message = cls._skipped_lines_message( + bottom_skipped_lines_message = cls._skipped_lines_message( bottom_skipped_lines ) template_after_token = ( - f"{template_after_token}\n{after_skipped_lines_message}" + f"{template_after_token}\n{bottom_skipped_lines_message}" ) lines_before_line_with_token = template_before_token.rsplit("\n", 1)[0]