fixup! fixup! WIP: Refactor jinja_env + make fallback actually work

parent c913b3df
import gettext
import logging
import os
import yaml
from jinja2 import Template, TemplateNotFound
from jinja2 import TemplateNotFound
logger = logging.getLogger(__name__)
class NotificationSkeleton:
......@@ -21,7 +24,6 @@ class NotificationSkeleton:
self.persistent = persistent
self.explicit_dismiss = explicit_dismiss
# work-in-progress
self.fallback = False
self.jinja_env = jinja_env
self.setup_jinja_env()
......@@ -56,27 +58,22 @@ class NotificationSkeleton:
def translate_actions(self, lang):
self._set_jinja_translation(lang)
# work-in-progress!
if self.fallback:
acts = {}
for action, content in self.actions.items():
tpl = self.jinja_env.from_string(content['title'])
content['title'] = tpl.render()
acts[action] = content
parsed = {'actions': acts}
print(parsed)
translated = []
for action in self.actions.values():
tpl = self.jinja_env.from_string(action['title'])
action['title'] = tpl.render()
translated.append(action)
parsed = {'actions': translated}
else:
print(self.jinja_plugin_template.render())
parsed = yaml.safe_load(self.jinja_plugin_template.render())
res = {
return {
pa['name']: pa['title'] for pa in parsed['actions'] if pa['name'] in self.actions
}
print(res)
return res
def setup_jinja_env(self):
"""Prepare templates for later use"""
try:
......@@ -85,7 +82,8 @@ class NotificationSkeleton:
self.jinja_plugin_template = self.jinja_env.get_template(os.path.join(self.plugin_name, 'plugin.yml'))
except TemplateNotFound as e:
self.fallback = True
print("Template {} not found".format(e))
logger.warning("Template file '%s' not found", e)
logger.debug("Using fallback instead")
def _set_jinja_translation(self, lang):
self.jinja_env.install_gettext_translations(
......
import glob
import os
import logging
import pathlib
import jinja2
from functools import lru_cache
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment