notificationskeleton.py 3.03 KB
Newer Older
1 2
import gettext

3
import jinja2
4
import yaml
5 6


7
class NotificationSkeleton:
8
    ATTRS = ['name', 'plugin_name', 'version', 'template', 'actions', 'template_dir', 'timeout', 'severity', 'persistent', 'explicit_dismiss']
9
    DEFAULT_ATTRS = ['timeout', 'severity', 'persistent', 'explicit_dismiss']
10

11
    def __init__(self, name, plugin_name, version, template, actions, template_dir, timeout=None, severity='info', persistent=False, explicit_dismiss=True):
12
        self.name = name
13
        self.plugin_name = plugin_name
14
        self.version = version
15 16
        self.template = template
        self.actions = actions
17 18
        self.template_dir = template_dir

19 20 21
        self.timeout = timeout
        self.severity = severity
        self.persistent = persistent
22
        self.explicit_dismiss = explicit_dismiss
23

24
        self.init_jinja_env()
25
        self.translations = {}
26

27 28 29
    def get_media_types(self):
        return self.template['supported_media']

30
    def serialize(self):
31 32 33 34
        json_data = {}

        for attr in self.ATTRS:
            json_data[attr] = getattr(self, attr)
35

36
        return json_data
37

38
    def get_skeleton_defaults(self):
39 40 41 42
        defaults = {}

        for attr in self.DEFAULT_ATTRS:
            defaults[attr] = getattr(self, attr)
43 44 45

        return defaults

46
    def get_action_cmd(self, name):
Martin Matějek's avatar
Martin Matějek committed
47
        if name in self.actions:
48 49 50
            return self.actions[name]['command']

        return None
Martin Matějek's avatar
Martin Matějek committed
51

52
    def translate_actions(self, lang):
53 54
        self._set_jinja_translation(lang)

55 56
        actions = {}

57
        parsed = yaml.safe_load(self.jinja_plugin_template.render())
58
        for a in self.actions:
59 60 61
            for pa in parsed['actions']:
                if a == pa['name']:
                    actions[a] = pa['title']
62 63 64

        return actions

65 66 67 68 69
    def init_jinja_env(self):
        """
        Init jinja environment

        Prepare template for later use
70
        For now it will be initiated when creating new skeleton instance
71
        """
72 73 74
        template_loader = jinja2.FileSystemLoader(self.template_dir)
        self.jinja_env = jinja2.Environment(
            loader=template_loader,
75
            autoescape=True,
76 77
            extensions=['jinja2.ext.i18n']
        )
78

79
        self.jinja_message_template = self.jinja_env.get_template(self.template['src'])
80

81
        plugin_template = '{}.yml'.format(self.plugin_name)
82
        self.jinja_plugin_template = self.jinja_env.get_template(plugin_template)
83

84 85
    def _set_jinja_translation(self, lang):
        self.jinja_env.install_gettext_translations(
86
            gettext.translation("notifylib-{}".format(self.plugin_name), localedir='/usr/share/locale', languages=[lang], fallback=True)
87 88
        )

89
    def render(self, data, media_type, lang):
90
        """Render using jinja in given language"""
91
        self._set_jinja_translation(lang)
92
        output = self.jinja_message_template.render(media=media_type, **data)
93 94

        return output
95

96 97
    def __str__(self):
        out = "{\n"
98 99 100 101

        for attr in self.ATTRS:
            out += "\t{}: {}\n".format(attr, getattr(self, attr))

102 103 104
        out += "}\n"

        return out