Skip to content

Commit be78fc3

Browse files
authored
Merge pull request #5 from michalpokusa/fixes-and-refactor
Fixes for render functions `chunk_size` parameter and negative skipped linesin error message
2 parents 9130d5d + 94a54c7 commit be78fc3

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

adafruit_templateengine.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,33 @@ def _underline_token_in_template(
9393
"""
9494

9595
template_before_token = token.template[: token.start_position]
96-
if skipped_lines := template_before_token.count("\n") - lines_around:
97-
template_before_token = (
98-
f"{cls._skipped_lines_message(skipped_lines)}\n"
99-
+ "\n".join(template_before_token.split("\n")[-(lines_around + 1) :])
96+
if top_skipped_lines := template_before_token.count("\n") - lines_around:
97+
template_before_token = "\n".join(
98+
template_before_token.split("\n")[-(lines_around + 1) :]
10099
)
101100

101+
if 0 < top_skipped_lines:
102+
top_skipped_lines_message = cls._skipped_lines_message(
103+
top_skipped_lines
104+
)
105+
template_before_token = (
106+
f"{top_skipped_lines_message}\n{template_before_token}"
107+
)
108+
102109
template_after_token = token.template[token.end_position :]
103-
if skipped_lines := template_after_token.count("\n") - lines_around:
104-
template_after_token = (
105-
"\n".join(template_after_token.split("\n")[: (lines_around + 1)])
106-
+ f"\n{cls._skipped_lines_message(skipped_lines)}"
110+
if bottom_skipped_lines := template_after_token.count("\n") - lines_around:
111+
template_after_token = "\n".join(
112+
template_after_token.split("\n")[: (lines_around + 1)]
107113
)
108114

115+
if 0 < bottom_skipped_lines:
116+
bottom_skipped_lines_message = cls._skipped_lines_message(
117+
bottom_skipped_lines
118+
)
119+
template_after_token = (
120+
f"{template_after_token}\n{bottom_skipped_lines_message}"
121+
)
122+
109123
lines_before_line_with_token = template_before_token.rsplit("\n", 1)[0]
110124

111125
line_with_token = (
@@ -122,7 +136,7 @@ def _underline_token_in_template(
122136

123137
lines_after_line_with_token = template_after_token.split("\n", 1)[-1]
124138

125-
return "\n".join(
139+
return "\n" + "\n".join(
126140
[
127141
lines_before_line_with_token,
128142
line_with_token,
@@ -771,7 +785,7 @@ def __init__(self, template_path: str) -> None:
771785
super().__init__(template_string)
772786

773787

774-
_CACHE: "dict[int, Template| FileTemplate]" = {}
788+
CACHED_TEMPLATES: "dict[int, Template| FileTemplate]" = {}
775789

776790

777791
def render_string_iter(
@@ -803,19 +817,15 @@ def render_string_iter(
803817
"""
804818
key = hash(template_string)
805819

806-
if cache and key in _CACHE:
807-
return _yield_as_sized_chunks(
808-
_CACHE[key].render_iter(context or {}, chunk_size), chunk_size
809-
)
820+
if cache and key in CACHED_TEMPLATES:
821+
return CACHED_TEMPLATES[key].render_iter(context or {}, chunk_size=chunk_size)
810822

811823
template = Template(template_string)
812824

813825
if cache:
814-
_CACHE[key] = template
826+
CACHED_TEMPLATES[key] = template
815827

816-
return _yield_as_sized_chunks(
817-
template.render_iter(context or {}), chunk_size=chunk_size
818-
)
828+
return template.render_iter(context or {}, chunk_size=chunk_size)
819829

820830

821831
def render_string(
@@ -841,13 +851,13 @@ def render_string(
841851
"""
842852
key = hash(template_string)
843853

844-
if cache and key in _CACHE:
845-
return _CACHE[key].render(context or {})
854+
if cache and key in CACHED_TEMPLATES:
855+
return CACHED_TEMPLATES[key].render(context or {})
846856

847857
template = Template(template_string)
848858

849859
if cache:
850-
_CACHE[key] = template
860+
CACHED_TEMPLATES[key] = template
851861

852862
return template.render(context or {})
853863

@@ -881,19 +891,15 @@ def render_template_iter(
881891
"""
882892
key = hash(template_path)
883893

884-
if cache and key in _CACHE:
885-
return _yield_as_sized_chunks(
886-
_CACHE[key].render_iter(context or {}, chunk_size), chunk_size
887-
)
894+
if cache and key in CACHED_TEMPLATES:
895+
return CACHED_TEMPLATES[key].render_iter(context or {}, chunk_size=chunk_size)
888896

889897
template = FileTemplate(template_path)
890898

891899
if cache:
892-
_CACHE[key] = template
900+
CACHED_TEMPLATES[key] = template
893901

894-
return _yield_as_sized_chunks(
895-
template.render_iter(context or {}, chunk_size=chunk_size), chunk_size
896-
)
902+
return template.render_iter(context or {}, chunk_size=chunk_size)
897903

898904

899905
def render_template(
@@ -920,12 +926,12 @@ def render_template(
920926

921927
key = hash(template_path)
922928

923-
if cache and key in _CACHE:
924-
return _CACHE[key].render(context or {})
929+
if cache and key in CACHED_TEMPLATES:
930+
return CACHED_TEMPLATES[key].render(context or {})
925931

926932
template = FileTemplate(template_path)
927933

928934
if cache:
929-
_CACHE[key] = template
935+
CACHED_TEMPLATES[key] = template
930936

931937
return template.render(context or {})

0 commit comments

Comments
 (0)