Commit de5e3984 authored by Martin Matějek's avatar Martin Matějek

Use same directory tree for plugin and its templates

parent c87601b5
......@@ -2,5 +2,4 @@
volatile_dir=/tmp/notify/volatile
persistent_dir=/tmp/notify/persistent
plugin_dir=/tmp/notify/plugins
templates_dir=/tmp/notify/templates
cmd_timeout=10
......@@ -27,7 +27,6 @@ class Api:
config.load_config(conf)
plugin_dir = config.get('settings', 'plugin_dir')
templates_dir = config.get('settings', 'templates_dir')
volatile_dir = config.get('settings', 'volatile_dir')
persistent_dir = config.get('settings', 'persistent_dir')
......@@ -35,10 +34,6 @@ class Api:
logger.error("Missing plugin directory %s", plugin_dir)
raise NotADirectoryError("Missing plugin directory {}".format(plugin_dir))
if not os.path.exists(templates_dir):
logger.error("Missing templates directory %s", templates_dir)
raise NotADirectoryError("Missing templates directory {}".format(templates_dir))
if not os.path.exists(volatile_dir):
logger.error("Missing volatile messages directory %s", volatile_dir)
raise NotADirectoryError("Missing volatile messages directory {}".format(volatile_dir))
......@@ -47,7 +42,7 @@ class Api:
logger.error("Missing persistent messages directory %s", persistent_dir)
raise NotADirectoryError("Missing persistent messages directory {}".format(persistent_dir))
self.plugins = PluginStorage(plugin_dir, templates_dir)
self.plugins = PluginStorage(plugin_dir)
self.notifications = NotificationStorage(volatile_dir, persistent_dir, self.plugins)
def get_notifications(self, media_type='simple', lang='en'):
......
......@@ -14,7 +14,6 @@ class Config:
self.conf.set("settings", "volatile_dir", "/tmp")
self.conf.set("settings", "persistent_dir", "/srv")
self.conf.set("settings", "plugin_dir", "plugins")
self.conf.set("settings", "templates_dir", "templates")
self.conf.set("settings", "cmd_timeout", "10")
def load_config(self, filename):
......
import gettext
import os
import yaml
......@@ -58,7 +59,7 @@ class NotificationSkeleton:
def setup_jinja_env(self):
"""Prepare templates for later use"""
self.jinja_message_template = self.jinja_env.get_template(self.template['src'])
self.jinja_message_template = self.jinja_env.get_template(os.path.join('templates', self.template['src']))
plugin_template = '{}.yml'.format(self.plugin_name)
self.jinja_plugin_template = self.jinja_env.get_template(plugin_template)
......
......@@ -16,9 +16,9 @@ class Plugin:
# 'notifications' section is intentionally omitted for now
# until mandatory and optional attributes check for 'notifications' is implemented
def __init__(self, name, template_dirs, actions, templates, notifications):
def __init__(self, name, template_dir, actions, templates, notifications):
self.name = name
self.template_dirs = template_dirs
self.template_dir = template_dir
self.actions = {}
self.templates = {}
self.notification_types = {}
......@@ -35,7 +35,7 @@ class Plugin:
self.init_jinja_env()
@classmethod
def from_file(cls, filepath, templates_dir):
def from_file(cls, filepath):
try:
with open(filepath, 'r') as f:
data = yaml.safe_load(f)
......@@ -56,11 +56,10 @@ class Plugin:
path = pathlib.Path(filepath)
filename = path.stem
jinja_template_dirs = [
os.path.join(templates_dir, filename),
path.parent,
]
return cls(filename, jinja_template_dirs, **data)
# use list as workaround to this issue https://github.com/pallets/jinja/issues/870
jinja_template_dir = [path.parent]
return cls(filename, jinja_template_dir, **data)
@classmethod
def valid_schema(cls, data):
......@@ -84,7 +83,7 @@ class Plugin:
return True
def init_jinja_env(self):
template_loader = jinja2.FileSystemLoader(self.template_dirs)
template_loader = jinja2.FileSystemLoader(self.template_dir)
self.jinja_env = jinja2.Environment(
loader=template_loader,
autoescape=True,
......
......@@ -14,21 +14,20 @@ class PluginStorage:
"""Storage for plugins"""
META_ATTRS = ['name', 'version', 'timeout', 'severity', 'persistent', 'explicit_dismiss']
def __init__(self, plugin_dir, templates_dir):
# print("Constructing new PluginStorage")
def __init__(self, plugin_dir):
self.plugin_dir = plugin_dir
self.templates_dir = templates_dir
self.plugins = {}
self.load()
def load(self):
"""Load plugins from FS"""
for filepath in glob.glob(os.path.join(self.plugin_dir, '*.yml')):
p = Plugin.from_file(filepath, self.templates_dir)
for filepath in glob.glob(os.path.join(self.plugin_dir, '*', '*.yml')):
logger.debug("reading plugin file '%s'", filepath)
p = Plugin.from_file(filepath)
if p:
logger.debug("Reading plugin '%s'", p.name)
self.plugins[p.name] = p
def get_plugin(self, name):
......
......@@ -3,7 +3,7 @@ width = 150
omit_header = True
mapping_file = babel.cfg
output_file = messages.pot
input_dirs = templates,plugins
input_dirs = plugins
[init_catalog]
input_file = messages.pot
......
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