summaryrefslogtreecommitdiffstats
path: root/pykolab/plugins/__init__.py
blob: 6bfd6271839ebecdb7c72c740086ee6ee9733ea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# -*- coding: utf-8 -*-
# Copyright 2010-2012 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 or, at your option, any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import logging
import os
import pdb
import sys
import traceback

import pykolab

from pykolab.translate import _

log = pykolab.getLogger('pykolab.plugins')
conf = pykolab.getConf()

class KolabPlugins(object):
    """
        Detects, loads and interfaces with plugins for different
        Kolab components.
    """
    def __init__(self):
        """
            Searches the plugin directory for plugins, and loads
            them into a list.
        """
        self.plugins = {}

        for plugin_path in [
                os.path.dirname(__file__),
                '/usr/share/pykolab/plugins',
                './pykolab/plugins'
            ]:

            if os.path.isdir(plugin_path):
                for plugin in os.listdir(plugin_path):
                    if os.path.isdir('%s/%s/' %(plugin_path,plugin,)):
                        self.plugins[plugin] = False

        self.check_plugins()

    def check_plugins(self):
        """
            Checks all plugins in self.plugins and sets the values to
            True (loadable) or False -- not enabled, not installed or
            not loadable.
        """
        for plugin in self.plugins:
            try:
                exec("from pykolab.plugins import %s" %(plugin))
                self.plugins[plugin] = True
                self.load_plugins(plugins=[plugin])
            except ImportError, e:
                log.error(_("ImportError for plugin %s: %s") % (plugin,e))
                traceback.print_exc()
                self.plugins[plugin] = False
            except RuntimeError, e:
                log.error( _("RuntimeError for plugin %s: %s") % (plugin,e))
                traceback.print_exc()
                self.plugins[plugin] = False
            except Exception, e:
                log.error(_("Plugin %s failed to load (%s: %s)") % (plugin, e.__class__, e))
                traceback.print_exc()
            except:
                traceback.print_exc()

    def load_plugins(self, plugins=[]):
        """
            Loads plugins specified by a list of plugins or loads them all
        """

        if len(plugins) < 1:
            plugins = self.plugins.keys()

        for plugin in plugins:
            if self.plugins[plugin]:
                try:
                    exec("self.%s = %s.Kolab%s()" % (plugin,plugin,plugin.capitalize()))
                except:
                    # TODO: A little better verbosity please!
                    traceback.print_exc()

    def set_defaults(self, defaults, plugins=[]):
        """
            Test for a function set_defaults() in all available and loaded plugins and execute plugin.set_defaults()
        """
        if len(plugins) < 1:
            plugins = self.plugins.keys()

        for plugin in plugins:
            if not self.plugins[plugin]:
                continue
            if not hasattr(self,plugin):
                continue

            if hasattr(getattr(self,plugin),"set_defaults"):
                try:
                    getattr(self,plugin).set_defaults(defaults)
                except TypeError, e:
                    log.error(_("Cannot set defaults for plugin %s: %s") % (plugin,e))
                except RuntimeError, e:
                    log.error(_("Cannot set defaults for plugin %s: %s") % (plugin,e))
                except:
                    log.error(_("Cannot set defaults for plugin %s: Unknown Error") % (plugin))

            else:
                log.debug(_("Not setting defaults for plugin %s: No function 'set_defaults()'") % plugin, level=5)

    def set_runtime(self, runtime, plugins=[]):
        """
            Set runtime variables from plugins, like 'i_did_all_this'
        """
        if len(plugins) < 1:
            plugins = self.plugins.keys()

        for plugin in plugins:
            if not self.plugins[plugin]:
                continue
            if not hasattr(self,plugin):
                continue

            if hasattr(getattr(self,plugin),"set_runtime"):
                try:
                    getattr(self,plugin).set_runtime(runtime)
                except RuntimeError, e:
                    log.error(_("Cannot set runtime for plugin %s: %s") % (plugin,e))
            else:
                log.debug(_("Not setting runtime for plugin %s: No function 'set_runtime()'") % (plugin), level=5)

    def add_options(self, parser, plugins=[]):
        """
            Add options specified in a plugin to parser. Takes a list of plugin names or does them all
        """
        if len(plugins) < 1:
            plugins = self.plugins.keys()

        for plugin in plugins:
            if not self.plugins[plugin]:
                continue
            if not hasattr(self,plugin):
                continue

            if hasattr(getattr(self,plugin),"add_options"):
                try:
                    exec("self.%s.add_options(parser)" % plugin)
                except RuntimeError, e:
                    log.error(_("Cannot add options for plugin %s: %s") % (plugin,e))
                except TypeError, e:
                    log.error(_("Cannot add options for plugin %s: %s") % (plugin,e))
            else:
                    log.debug(_("Not adding options for plugin %s: No function 'add_options()'") % plugin, level=5)

    def check_options(self, plugins=[]):
        """
            Executes plugin.check_plugins() for all enabled plugins or the list of plugin names specified.
        """

        if len(plugins) < 1:
            plugins = self.plugins.keys()

        for plugin in plugins:
            if not self.plugins[plugin]:
                continue
            if not hasattr(self,plugin):
                continue

            if hasattr(getattr(self,plugin),"check_options"):
                try:
                    exec("self.%s.check_options()" % plugin)
                except AttributeError, e:
                    log.error(_("Cannot check options for plugin %s: %s") % (plugin,e))
            else:
                log.debug(_("Not checking options for plugin %s: No function 'check_options()'") %(plugin), level=5)

    def plugin_check_setting(self, func, option, val, plugins=[]):
        """
            Checks one setting specified by 'option' against the 'val' it is passed by all plugins or by the list of plugins specified
        """

        if len(plugins) < 1:
            plugins = self.plugins.keys()

        for plugin in plugins:
            if not self.plugins[plugin]:
                continue
            if not hasattr(self,plugin):
                continue

            if hasattr(getattr(self,plugin),"%s_%s" % (func,option)):
                exec("retval = getattr(self,plugin).%s_%s(val)" % (func,option))
                return retval

        return False

    def exec_hook(self, hook, plugins=[], kw={}, args=()):
        """Execute a hook"""

        if len(plugins) < 1:
            plugins = self.plugins.keys()

        for plugin in plugins:
            if not self.plugins[plugin]:
                continue
            if not hasattr(self,plugin):
                continue

            retval = None

            if hasattr(getattr(self,plugin),hook):
                try:
                    log.debug(_("Executing hook %s for plugin %s") %(hook,plugin), level=8)
                    exec("retval = self.%s.%s(*args, **kw)" %(plugin,hook))
                except TypeError, e:
                    log.error(_("Cannot execute hook %s for plugin %s: %s") %(hook,plugin,e))
                except AttributeError, e:
                    log.error(_("Cannot execute hook %s for plugin %s: %s") %(hook,plugin,e))

                return retval

    def return_true_boolean_from_plugins(self, bool, plugins=[]):
        """Given the name of a boolean, walks all specified plugins, or all available plugins, and returns True if a plugin has it set to true"""
        if len(plugins) < 1:
            plugins = self.plugins.keys()

        retval = False

        for plugin in plugins:
            if not self.plugins[plugin]:
                continue
            if not hasattr(self,plugin):
                continue

            if hasattr(getattr(self,plugin),bool):
                try:
                    exec("boolval = self.%s.%s" % (plugin,bool))
                except AttributeError, e:
                    pass
            else:
                boolval = None

            if boolval: retval = True

        return retval