Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions adafruit_templateengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,33 @@ 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) :])
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 < top_skipped_lines:
top_skipped_lines_message = cls._skipped_lines_message(
top_skipped_lines
)
template_before_token = (
f"{top_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:
template_after_token = (
"\n".join(template_after_token.split("\n")[: (lines_around + 1)])
+ f"\n{cls._skipped_lines_message(skipped_lines)}"
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 < bottom_skipped_lines:
bottom_skipped_lines_message = cls._skipped_lines_message(
bottom_skipped_lines
)
template_after_token = (
f"{template_after_token}\n{bottom_skipped_lines_message}"
)

lines_before_line_with_token = template_before_token.rsplit("\n", 1)[0]

line_with_token = (
Expand All @@ -122,7 +136,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,
Expand Down Expand Up @@ -771,7 +785,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(
Expand Down Expand Up @@ -803,19 +817,15 @@ 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
)
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 _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(
Expand All @@ -841,13 +851,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 {})

Expand Down Expand Up @@ -881,19 +891,15 @@ 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
)
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 _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(
Expand All @@ -920,12 +926,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 {})