WIP: Refactor jinja_env + make fallback actually work

parent de5e3984
......@@ -82,7 +82,11 @@ class Notification:
skel_args = json_data['skeleton']
plug = plugin_storage.get_plugin(skel_args['plugin_name'])
skel_args['jinja_env'] = plug.get_jinja_env()
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()
# TODO: Use json schema or another validation method
skel_obj = NotificationSkeleton(**skel_args)
......@@ -136,6 +140,7 @@ class Notification:
output = {}
output['message'] = self.render_template(media_type, lang)
output['actions'] = self.skeleton.translate_actions(lang)
if self.explicit_dismiss:
output['actions']['dismiss'] = '' # default action for all notifications
output['metadata'] = self._serialize_data(self.META_ATTRS)
......
......@@ -8,20 +8,24 @@ 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):
def __init__(self, name, plugin_name, version, template, actions, jinja_env, timeout=None, severity='info', persistent=False, explicit_dismiss=True, fallback=False):
self.name = name
self.plugin_name = plugin_name
self.version = version
self.template = template
self.actions = actions
self.jinja_env = jinja_env
self.timeout = timeout
self.severity = severity
self.persistent = persistent
self.explicit_dismiss = explicit_dismiss
self.setup_jinja_env()
# work-in-progress
if fallback:
self.fallback = fallback
else:
self.jinja_env = jinja_env
self.setup_jinja_env()
self.translations = {}
def get_media_types(self):
......@@ -59,10 +63,9 @@ class NotificationSkeleton:
def setup_jinja_env(self):
"""Prepare templates for later use"""
self.jinja_message_template = self.jinja_env.get_template(os.path.join('templates', self.template['src']))
self.jinja_message_template = self.jinja_env.get_template(os.path.join(self.plugin_name, 'templates', self.template['src']))
plugin_template = '{}.yml'.format(self.plugin_name)
self.jinja_plugin_template = self.jinja_env.get_template(plugin_template)
self.jinja_plugin_template = self.jinja_env.get_template(os.path.join(self.plugin_name, 'plugin.yml'))
def _set_jinja_translation(self, lang):
self.jinja_env.install_gettext_translations(
......
import logging
import os
import pathlib
import jinja2
import yaml
logger = logging.getLogger(__name__)
......@@ -16,9 +14,8 @@ class Plugin:
# 'notifications' section is intentionally omitted for now
# until mandatory and optional attributes check for 'notifications' is implemented
def __init__(self, name, template_dir, actions, templates, notifications):
def __init__(self, name, actions, templates, notifications):
self.name = name
self.template_dir = template_dir
self.actions = {}
self.templates = {}
self.notification_types = {}
......@@ -32,8 +29,6 @@ class Plugin:
for n in notifications:
self.notification_types[n['name']] = n
self.init_jinja_env()
@classmethod
def from_file(cls, filepath):
try:
......@@ -54,12 +49,9 @@ class Plugin:
# i.e. anything not needed for plugin that is present in yaml file
path = pathlib.Path(filepath)
filename = path.stem
# use list as workaround to this issue https://github.com/pallets/jinja/issues/870
jinja_template_dir = [path.parent]
name = path.parent.stem
return cls(filename, jinja_template_dir, **data)
return cls(name, **data)
@classmethod
def valid_schema(cls, data):
......@@ -82,14 +74,6 @@ class Plugin:
return True
def init_jinja_env(self):
template_loader = jinja2.FileSystemLoader(self.template_dir)
self.jinja_env = jinja2.Environment(
loader=template_loader,
autoescape=True,
extensions=['jinja2.ext.i18n']
)
def get_actions(self):
return self.actions
......
import glob
import os
import logging
import pathlib
import jinja2
from functools import lru_cache
......@@ -19,10 +21,11 @@ class PluginStorage:
self.plugins = {}
self.load()
self.init_jinja_env()
def load(self):
"""Load plugins from FS"""
for filepath in glob.glob(os.path.join(self.plugin_dir, '*', '*.yml')):
for filepath in glob.glob(os.path.join(self.plugin_dir, '*', 'plugin.yml')):
logger.debug("reading plugin file '%s'", filepath)
p = Plugin.from_file(filepath)
......@@ -30,9 +33,17 @@ class PluginStorage:
logger.debug("Reading plugin '%s'", p.name)
self.plugins[p.name] = p
def init_jinja_env(self):
template_loader = jinja2.FileSystemLoader(self.plugin_dir)
self.jinja_env = jinja2.Environment(
loader=template_loader,
autoescape=True,
extensions=['jinja2.ext.i18n']
)
def get_plugin(self, name):
"""Return plugin specified by name"""
return self.plugins[name]
return self.plugins.get(name)
def get_all(self):
"""Return all plugins"""
......@@ -91,7 +102,7 @@ class PluginStorage:
if attr in skeleton:
notification_args[attr] = skeleton[attr]
notification_args['jinja_env'] = plugin.get_jinja_env()
notification_args['jinja_env'] = self.jinja_env
return NotificationSkeleton(**notification_args)
......@@ -110,3 +121,6 @@ class PluginStorage:
ret.extend(type_names)
return ret
def get_jinja_env(self):
return self.jinja_env
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