fixup! WIP: Refactor jinja_env + make fallback actually work

parent 471be1c0
......@@ -3,7 +3,7 @@ import logging
import uuid
from datetime import datetime
from jinja2 import TemplateError
from jinja2 import TemplateError, TemplateNotFound
from types import SimpleNamespace
from . import api_version
......@@ -84,7 +84,6 @@ class Notification:
if not plug:
logger.warning("Plugin '%s' not available - check your instalation", skel_args['plugin_name'])
skel_args['fallback'] = True
skel_args['jinja_env'] = plugin_storage.get_jinja_env()
......
......@@ -3,12 +3,14 @@ import os
import yaml
from jinja2 import Template, TemplateNotFound
class NotificationSkeleton:
ATTRS = ['name', 'plugin_name', 'version', 'template', 'actions', 'timeout', 'severity', 'persistent', 'explicit_dismiss']
DEFAULT_ATTRS = ['timeout', 'severity', 'persistent', 'explicit_dismiss']
def __init__(self, name, plugin_name, version, template, actions, jinja_env, timeout=None, severity='info', persistent=False, explicit_dismiss=True, fallback=False):
def __init__(self, name, plugin_name, version, template, actions, jinja_env, timeout=None, severity='info', persistent=False, explicit_dismiss=True):
self.name = name
self.plugin_name = plugin_name
self.version = version
......@@ -20,11 +22,9 @@ class NotificationSkeleton:
self.explicit_dismiss = explicit_dismiss
# work-in-progress
if fallback:
self.fallback = fallback
else:
self.jinja_env = jinja_env
self.setup_jinja_env()
self.fallback = False
self.jinja_env = jinja_env
self.setup_jinja_env()
self.translations = {}
......@@ -56,16 +56,36 @@ class NotificationSkeleton:
def translate_actions(self, lang):
self._set_jinja_translation(lang)
parsed = yaml.safe_load(self.jinja_plugin_template.render())
return {
# 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)
else:
print(self.jinja_plugin_template.render())
parsed = yaml.safe_load(self.jinja_plugin_template.render())
res = {
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"""
self.jinja_message_template = self.jinja_env.get_template(os.path.join(self.plugin_name, 'templates', self.template['src']))
try:
self.jinja_message_template = self.jinja_env.get_template(os.path.join(self.plugin_name, 'templates', self.template['src']))
self.jinja_plugin_template = self.jinja_env.get_template(os.path.join(self.plugin_name, 'plugin.yml'))
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))
def _set_jinja_translation(self, lang):
self.jinja_env.install_gettext_translations(
......
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