output_pin: Make it possible to assign dicts/lists as template parameters

The output_pin template code has a cache to speed up duplicate
rendering of templates.  However, this cache doesn't work if one of
the parameters is a Python list or dictionary.  Just disable the cache
in this case.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2025-03-29 14:33:54 -04:00
parent 8176ba22aa
commit be429caba3

View file

@ -102,7 +102,13 @@ class PrinterTemplateEvaluator:
self.render_timer = reactor.register_timer(self._render, reactor.NOW)
def _activate_template(self, callback, template, lparams, flush_callback):
if template is not None:
# Build a unique id to make it possible to cache duplicate rendering
uid = (template,) + tuple(sorted(lparams.items()))
try:
{}.get(uid)
except TypeError as e:
# lparams is not static, so disable caching
uid = None
self.active_templates[callback] = (
uid, template, lparams, flush_callback)
return
@ -122,17 +128,18 @@ class PrinterTemplateEvaluator:
context['render'] = render
# Render all templates
flush_callbacks = {}
rendered = {}
render_cache = {}
template_info = self.active_templates.items()
for callback, (uid, template, lparams, flush_callback) in template_info:
text = rendered.get(uid)
text = render_cache.get(uid)
if text is None:
try:
text = template.render(context, **lparams)
except Exception as e:
logging.exception("display template render error")
text = ""
rendered[uid] = text
if uid is not None:
render_cache[uid] = text
if flush_callback is not None:
flush_callbacks[flush_callback] = 1
callback(text)