Commit 3d12e8df authored by Martin Matějek's avatar Martin Matějek

WIP safe loading of notification skeletons

parent b91a1b2c
...@@ -45,6 +45,23 @@ class PluginStorage: ...@@ -45,6 +45,23 @@ class PluginStorage:
"""Return all plugins""" """Return all plugins"""
return self.plugins return self.plugins
def valid_id(self, skel_id):
try:
plugin_name, skel_name = skel_id.split('.')
except ValueError:
logger.warning("Malformed skeleton id '%s'", skel_id)
return False
if plugin_name not in self.plugins:
logger.warning("No such plugin '%s'", plugin_name)
return False
if skel_name not in self.plugins[plugin_name].get_notification_types():
logger.warning("No such skeleton: '%s'", skel_name)
return False
return True
def get_skeleton(self, skel_id): def get_skeleton(self, skel_id):
""" """
Return notification skeleton based on id Return notification skeleton based on id
...@@ -54,44 +71,46 @@ class PluginStorage: ...@@ -54,44 +71,46 @@ class PluginStorage:
skeleton object either exists cached or will be added when needed skeleton object either exists cached or will be added when needed
""" """
if not self.valid_id(skel_id):
return None
plugin_name, skel_name = skel_id.split('.') plugin_name, skel_name = skel_id.split('.')
if skel_id not in self.skeletons: if skel_id not in self.skeletons:
if plugin_name in self.plugins: plugin = self.plugins[plugin_name]
notification_types = self.plugins[plugin_name].get_notification_types()
templates = self.plugins[plugin_name].get_templates() notification_types = plugin.get_notification_types()
plugin_actions = plugin.get_actions()
if skel_name in notification_types: templates = plugin.get_templates()
# TODO: refactor/simplify this code
notification_args = {} skeleton = notification_types[skel_name]
notification_args['name'] = notification_types[skel_name]['name']
notification_args['plugin_name'] = plugin_name # TODO: refactor/simplify this code
notification_args['version'] = notification_types[skel_name]['version'] notification_args = {}
notification_args['name'] = skeleton['name']
skel_actions = OrderedDict() notification_args['plugin_name'] = plugin_name
plugin_actions = self.plugins[plugin_name].get_actions() notification_args['version'] = skeleton['version']
for action in notification_types[skel_name]['actions']:
if action in plugin_actions: skel_actions = OrderedDict()
skel_actions[action] = plugin_actions[action] for action in skeleton['actions']:
if action in plugin_actions:
notification_args['actions'] = skel_actions skel_actions[action] = plugin_actions[action]
tmpl_name = notification_types[skel_name]['template'] notification_args['actions'] = skel_actions
template = templates[tmpl_name]
notification_args['template'] = template tmpl_name = skeleton['template']
template = templates[tmpl_name]
for attr in self.META_ATTRS: notification_args['template'] = template
if attr in notification_types[skel_name]:
notification_args[attr] = notification_types[skel_name][attr] for attr in self.META_ATTRS:
if attr in skeleton:
notification_args['template_dir'] = os.path.join(self.templates_dir, plugin_name) notification_args[attr] = skeleton[attr]
self.skeletons[skel_id] = NotificationSkeleton(**notification_args) # cache it
else: notification_args['template_dir'] = os.path.join(self.templates_dir, plugin_name)
logger.warning("No such notification type '%s' in plugin '%s'", skel_name, plugin_name)
return None # cache it
else: self.skeletons[skel_id] = NotificationSkeleton(**notification_args)
logger.warning("No such skeleton: '%s'", skel_id)
return None
return self.skeletons[skel_id] return self.skeletons[skel_id]
......
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