diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2025-03-29 14:33:54 -0400 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2025-04-03 13:38:31 -0400 |
commit | be429caba3eb25b6924c561d494dc74359d818fd (patch) | |
tree | f50650d94579230652df805a0869078c3db98b61 /klippy/extras | |
parent | 8176ba22aa6cca9ad2e470ce927d784281f3f3d7 (diff) | |
download | kutter-be429caba3eb25b6924c561d494dc74359d818fd.tar.gz kutter-be429caba3eb25b6924c561d494dc74359d818fd.tar.xz kutter-be429caba3eb25b6924c561d494dc74359d818fd.zip |
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>
Diffstat (limited to 'klippy/extras')
-rw-r--r-- | klippy/extras/output_pin.py | 13 |
1 files 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) |