mirror of
https://github.com/andreili/klipper.git
synced 2025-08-24 03:44:06 +02:00
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:
parent
8176ba22aa
commit
be429caba3
@ -102,7 +102,13 @@ class PrinterTemplateEvaluator:
|
|||||||
self.render_timer = reactor.register_timer(self._render, reactor.NOW)
|
self.render_timer = reactor.register_timer(self._render, reactor.NOW)
|
||||||
def _activate_template(self, callback, template, lparams, flush_callback):
|
def _activate_template(self, callback, template, lparams, flush_callback):
|
||||||
if template is not None:
|
if template is not None:
|
||||||
|
# Build a unique id to make it possible to cache duplicate rendering
|
||||||
uid = (template,) + tuple(sorted(lparams.items()))
|
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] = (
|
self.active_templates[callback] = (
|
||||||
uid, template, lparams, flush_callback)
|
uid, template, lparams, flush_callback)
|
||||||
return
|
return
|
||||||
@ -122,17 +128,18 @@ class PrinterTemplateEvaluator:
|
|||||||
context['render'] = render
|
context['render'] = render
|
||||||
# Render all templates
|
# Render all templates
|
||||||
flush_callbacks = {}
|
flush_callbacks = {}
|
||||||
rendered = {}
|
render_cache = {}
|
||||||
template_info = self.active_templates.items()
|
template_info = self.active_templates.items()
|
||||||
for callback, (uid, template, lparams, flush_callback) in template_info:
|
for callback, (uid, template, lparams, flush_callback) in template_info:
|
||||||
text = rendered.get(uid)
|
text = render_cache.get(uid)
|
||||||
if text is None:
|
if text is None:
|
||||||
try:
|
try:
|
||||||
text = template.render(context, **lparams)
|
text = template.render(context, **lparams)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception("display template render error")
|
logging.exception("display template render error")
|
||||||
text = ""
|
text = ""
|
||||||
rendered[uid] = text
|
if uid is not None:
|
||||||
|
render_cache[uid] = text
|
||||||
if flush_callback is not None:
|
if flush_callback is not None:
|
||||||
flush_callbacks[flush_callback] = 1
|
flush_callbacks[flush_callback] = 1
|
||||||
callback(text)
|
callback(text)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user