From be429caba3eb25b6924c561d494dc74359d818fd Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 29 Mar 2025 14:33:54 -0400 Subject: [PATCH] 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 --- klippy/extras/output_pin.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/klippy/extras/output_pin.py b/klippy/extras/output_pin.py index dc9959ce..4e79f2ad 100644 --- a/klippy/extras/output_pin.py +++ b/klippy/extras/output_pin.py @@ -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)