summaryrefslogtreecommitdiffstats
path: root/pykolab/plugins
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2011-03-23 13:19:30 +0000
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2011-03-23 13:19:30 +0000
commitb2c7f60ec0cf67b6510c8126f31e015d316634cb (patch)
tree2ef54fde25d98a5f73e5eba6dabc64a1e417597a /pykolab/plugins
parent5e5441ecd228e2fa71d6406ba6a7f1f94422d389 (diff)
downloadpykolab-b2c7f60ec0cf67b6510c8126f31e015d316634cb.tar.gz
The plugins can now make use of the logging facilities,
Have the plugins be passed keywords and reflect that
Diffstat (limited to 'pykolab/plugins')
-rw-r--r--pykolab/plugins/__init__.py69
-rw-r--r--pykolab/plugins/defaultfolders/__init__.py31
-rw-r--r--pykolab/plugins/dynamicquota/__init__.py45
-rw-r--r--pykolab/plugins/recipientpolicy/__init__.py59
4 files changed, 120 insertions, 84 deletions
diff --git a/pykolab/plugins/__init__.py b/pykolab/plugins/__init__.py
index 116b777..8ef44f8 100644
--- a/pykolab/plugins/__init__.py
+++ b/pykolab/plugins/__init__.py
@@ -20,32 +20,31 @@
import logging
import os
import pdb
-import pykolab
import sys
import traceback
+import pykolab
+
if False:
import pykolab.plugins.defaultfolders
import pykolab.plugins.dynamicquota
import pykolab.plugins.recipientpolicy
-# Translation
from pykolab.translate import _
-class KolabPlugins:
+log = pykolab.getLogger('pykolab.plugins')
+conf = pykolab.getConf()
+
+class KolabPlugins(object):
"""
Detects, loads and interfaces with plugins for different
Kolab components.
"""
- def __init__(self, init=False, conf=None):
+ def __init__(self):
"""
Searches the plugin directory for plugins, and loads
them into a list.
"""
- self.conf = conf
-
- self.log = logging.getLogger('pykolab.plugins')
-
self.plugins = {}
for plugin_path in [ '/usr/share/pykolab/plugins', './pykolab/plugins' ]:
@@ -54,9 +53,9 @@ class KolabPlugins:
if os.path.isdir('%s/%s/' %(plugin_path,plugin,)):
self.plugins[plugin] = False
- self.check_plugins(init=init)
+ self.check_plugins()
- def check_plugins(self, init=False):
+ def check_plugins(self):
"""
Checks all plugins in self.plugins and sets the values to
True (loadable) or False -- not enabled, not installed or
@@ -66,22 +65,22 @@ class KolabPlugins:
try:
exec("from pykolab.plugins import %s" %(plugin))
self.plugins[plugin] = True
- self.load_plugins(plugins=[plugin], init=init)
+ self.load_plugins(plugins=[plugin])
except ImportError, e:
- if not init: print >> sys.stderr, _("ImportError for plugin %s: %s") % (plugin,e)
+ log.error(_("ImportError for plugin %s: %s") % (plugin,e))
traceback.print_exc()
self.plugins[plugin] = False
except RuntimeError, e:
- if not init: print >> sys.stderr, _("RuntimeError for plugin %s: %s") % (plugin,e)
+ log.error( _("RuntimeError for plugin %s: %s") % (plugin,e))
traceback.print_exc()
self.plugins[plugin] = False
except Exception, e:
- if not init: print >> sys.stderr, _("Plugin %s failed to load (%s: %s)") % (plugin, e.__class__, 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=[], init=False):
+ def load_plugins(self, plugins=[]):
"""
Loads plugins specified by a list of plugins or loads them all
"""
@@ -92,7 +91,7 @@ class KolabPlugins:
for plugin in plugins:
if self.plugins[plugin]:
try:
- exec("self.%s = %s.Kolab%s(conf=self.conf)" % (plugin,plugin,plugin.capitalize()))
+ exec("self.%s = %s.Kolab%s()" % (plugin,plugin,plugin.capitalize()))
except:
# TODO: A little better verbosity please!
traceback.print_exc()
@@ -114,17 +113,14 @@ class KolabPlugins:
try:
getattr(self,plugin).set_defaults(defaults)
except TypeError, e:
- print >> sys.stderr, _("Cannot set defaults for plugin %s: %s") % (plugin,e)
+ log.error(_("Cannot set defaults for plugin %s: %s") % (plugin,e))
except RuntimeError, e:
- print >> sys.stderr, _("Cannot set defaults for plugin %s: %s") % (plugin,e)
+ log.error(_("Cannot set defaults for plugin %s: %s") % (plugin,e))
except:
- print >> sys.stderr, _("Cannot set defaults for plugin %s: Unknown Error") % (plugin)
+ log.error(_("Cannot set defaults for plugin %s: Unknown Error") % (plugin))
else:
- if hasattr(self.conf, "log"):
- self.conf.log.debug(_("Not setting defaults for plugin %s: No function 'set_defaults()'") % plugin, level=5)
- else:
- print >> sys.stderr, _("Not setting defaults for plugin %s: No function 'set_defaults()'") % plugin
+ log.debug(_("Not setting defaults for plugin %s: No function 'set_defaults()'") % plugin, level=5)
def set_runtime(self, runtime, plugins=[]):
"""
@@ -143,9 +139,9 @@ class KolabPlugins:
try:
getattr(self,plugin).set_runtime(runtime)
except RuntimeError, e:
- print >> sys.stderr, _("Cannot set runtime for plugin %s: %s") % (plugin,e)
+ log.error(_("Cannot set runtime for plugin %s: %s") % (plugin,e))
else:
- print >> sys.stderr, _("Not setting runtime for plugin %s: No function 'set_runtime()'") % plugin
+ log.debug(_("Not setting runtime for plugin %s: No function 'set_runtime()'") % (plugin), level=5)
def add_options(self, parser, plugins=[]):
"""
@@ -164,14 +160,13 @@ class KolabPlugins:
try:
exec("self.%s.add_options(parser)" % plugin)
except RuntimeError, e:
- print >> sys.stderr, _("Cannot add options for plugin %s: %s") % (plugin,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:
- if hasattr(self.conf, "log"):
- self.conf.log.debug(_("Not adding options for plugin %s: No function 'add_options()'") % plugin, level=5)
- else:
- print >> sys.stderr, _("Not adding options for plugin %s: No function 'add_options()'") % plugin
+ log.debug(_("Not adding options for plugin %s: No function 'add_options()'") % plugin, level=5)
- def check_options(self, conf, plugins=[]):
+ def check_options(self, plugins=[]):
"""
Executes plugin.check_plugins() for all enabled plugins or the list of plugin names specified.
"""
@@ -187,11 +182,11 @@ class KolabPlugins:
if hasattr(getattr(self,plugin),"check_options"):
try:
- exec("self.%s.check_options(conf, conf.cli_options)" % plugin)
+ exec("self.%s.check_options()" % plugin)
except AttributeError, e:
- print >> sys.stderr, _("Cannot check options for plugin %s: %s") % (plugin,e)
+ log.error(_("Cannot check options for plugin %s: %s") % (plugin,e))
else:
- print >> sys.stderr, _("Not checking options for plugin %s: No function 'check_options()'") % plugin
+ log.debug(_("Not checking options for plugin %s: No function 'check_options()'") %(plugin), level=5)
def plugin_check_setting(self, func, option, val, plugins=[]):
"""
@@ -229,11 +224,11 @@ class KolabPlugins:
if hasattr(getattr(self,plugin),hook):
try:
- exec("retval = self.%s.%s(kw=%r, args=%r)" % (plugin,hook,kw,args))
+ exec("retval = self.%s.%s(*args, **kw)" %(plugin,hook))
except TypeError, e:
- print >> sys.stderr, _("Cannot execute hook %s for plugin %s: %s") % (hook,plugin,e)
+ log.error(_("Cannot execute hook %s for plugin %s: %s") %(hook,plugin,e))
except AttributeError, e:
- print >> sys.stderr, _("Cannot execute hook %s for plugin %s: %s") % (hook,plugin,e)
+ log.error(_("Cannot execute hook %s for plugin %s: %s") %(hook,plugin,e))
return retval
diff --git a/pykolab/plugins/defaultfolders/__init__.py b/pykolab/plugins/defaultfolders/__init__.py
index 426b293..91db993 100644
--- a/pykolab/plugins/defaultfolders/__init__.py
+++ b/pykolab/plugins/defaultfolders/__init__.py
@@ -17,28 +17,39 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
-__all__ = [
- 'KolabDefaultfolders'
- ]
+import pykolab
+
+from pykolab.translate import _
+
+conf = pykolab.getConf()
+log = pykolab.getLogger('pykolab.plugins.defaultfolders')
class KolabDefaultfolders(object):
"""
Example plugin to create a set of default folders.
"""
- def __init__(self, conf=None):
- self.conf = conf
+ def __init__(self):
+ pass
+
+ def add_options(self, *args, **kw):
+ pass
- def create_user_folders(self, kw={}, args=()):
+ def create_user_folders(self, *args, **kw):
"""
The arguments passed to the 'create_user_folders' hook:
- - imap connection
- - user folder
+ additional_folders - additional folders to create
+ user_folder - user folder
"""
- (folder, additional_folders) = args
+ if not kw.has_key('additional_folders'):
+ return {}
- exec("additional_folders = %s" %(additional_folders))
+ try:
+ exec("additional_folders = %s" %(kw['additional_folders']))
+ except Exception, e:
+ log.error(_("Could not parse additional_folders"))
+ return {}
return additional_folders
diff --git a/pykolab/plugins/dynamicquota/__init__.py b/pykolab/plugins/dynamicquota/__init__.py
index c91ebfc..c55b899 100644
--- a/pykolab/plugins/dynamicquota/__init__.py
+++ b/pykolab/plugins/dynamicquota/__init__.py
@@ -17,15 +17,25 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
+import pykolab
+
+from pykolab.translate import _
+
+conf = pykolab.getConf()
+log = pykolab.getLogger('pykolab.plugins.dynamicquota')
+
class KolabDynamicquota(object):
"""
Example plugin making quota adjustments given arbitrary conditions.
"""
- def __init__(self, conf=None):
- self.conf = conf
+ def __init__(self):
+ pass
- def set_user_folder_quota(self, kw={}, args=()):
+ def add_options(self, *args, **kw):
+ pass
+
+ def set_user_folder_quota(self, *args, **kw):
"""
The arguments passed to the 'set_user_folder_quota' hook:
@@ -34,16 +44,17 @@ class KolabDynamicquota(object):
- quota (integer, in KB)
"""
- #print args
-
- (used, current_quota, new_quota, default_quota) = args
+ for keyword in [ 'used', 'current_quota', 'new_quota', 'default_quota' ]:
+ if not kw.has_key(keyword):
+ log.warning(_("No keyword %s passed to set_user_folder_quota") %(keyword))
+ return 0
# Escape the user without quota
- if new_quota == 0:
+ if kw['new_quota'] == 0:
# Unless default quota is set
- if default_quota > 0:
- #print "new quota is 0, but default quota > 0, returning default quota"
- return default_quota
+ if kw['default_quota'] > 0:
+ log.info(_("The new quota was set to 0, but default quota > 0, returning default quota"))
+ return kw['default_quota']
#print "new quota is 0, and default quota is no larger then 0, returning 0"
return 0
@@ -53,17 +64,17 @@ class KolabDynamicquota(object):
# - increase the quota by 10% if the currently used storage size
# is over 90%
- if new_quota < int(float(used) * 1.1):
+ if kw['new_quota'] < int(float(kw['used']) * 1.1):
#print "new quota is smaller then 110%% of what is currently used, returning 110%% of used"
- new_quota = int(float(used) * 1.1)
- elif new_quota > int(float(used) * 1.1):
+ new_quota = int(float(kw['used']) * 1.1)
+ elif kw['new_quota'] > int(float(kw['used']) * 1.1):
# TODO: If the current quota in IMAP had been set to 0, but we want to apply quota, and
# 0 is current_quota, 90% of that is still 0...
#print "new quota is larger then 110%% of what is currently used, returning 90%% of current quota"
- new_quota = int(float(current_quota) * 0.9)
+ new_quota = int(float(kw['current_quota']) * 0.9)
- if default_quota > new_quota:
- #print "default quota is more then the calculated new quota"
- return default_quota
+ if kw['default_quota'] > new_quota:
+ log.info(_("The default quota is larger then the calculated new quota, using the default quota"))
+ return kw['default_quota']
return new_quota
diff --git a/pykolab/plugins/recipientpolicy/__init__.py b/pykolab/plugins/recipientpolicy/__init__.py
index fbf0179..5d47067 100644
--- a/pykolab/plugins/recipientpolicy/__init__.py
+++ b/pykolab/plugins/recipientpolicy/__init__.py
@@ -17,8 +17,12 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
+import pykolab
+
+conf = pykolab.getConf()
+log = pykolab.getLogger('pykolab.plugins.recipientpolicy')
+
from pykolab import utils
-from pykolab.auth import Auth
from pykolab.translate import _
class KolabRecipientpolicy(object):
@@ -26,8 +30,11 @@ class KolabRecipientpolicy(object):
Example plugin making quota adjustments given arbitrary conditions.
"""
- def __init__(self, conf=None):
- self.conf = conf
+ def __init__(self):
+ pass
+
+ def add_options(self, *args, **kw):
+ pass
#def mail_domain_space_policy_check(self, kw={}, args=()):
#(mail, alternative_mail, domain_name, domain_root_dn) = args
@@ -35,21 +42,24 @@ class KolabRecipientpolicy(object):
## Your actions go here. For example:
#return (mail, alternative_mail)
- def set_primary_mail(self, kw={}, args=()):
+ def set_primary_mail(self, *args, **kw):
"""
The arguments passed to the 'set_user_attrs_mail' hook:
- - current user attributes
- """
+ primary_mail - the policy
+ user_attrs - the current user attributes
+ primary_domain - the domain to use in the primary mail attribute
+ secondary_domains - the secondary domains that are aliases
- (user_attrs, primary_domain, secondary_domains) = args
+ Return the new primary mail address
+ """
- user_attrs = utils.normalize(user_attrs)
+ user_attrs = utils.normalize(kw['user_attrs'])
if not user_attrs.has_key('domain'):
- user_attrs['domain'] = primary_domain
- elif not user_attrs['domain'] == primary_domain:
- user_attrs['domain'] = primary_domain
+ user_attrs['domain'] = kw['primary_domain']
+ elif not user_attrs['domain'] == kw['primary_domain']:
+ user_attrs['domain'] = kw['primary_domain']
try:
mail = kw['primary_mail'] % user_attrs
@@ -58,30 +68,39 @@ class KolabRecipientpolicy(object):
self.conf.log.warning(_("Attribute substitution for 'mail' failed in Recipient Policy"))
return user_attrs['mail'].lower()
- def set_secondary_mail(self, kw={}, args=()):
+ def set_secondary_mail(self, *args, **kw):
"""
The arguments passed to the 'set_user_attrs_alternative_mail' hook:
- - current user attributes
- """
+ primary_mail - the policy
+ user_attrs - the current user attributes
+ primary_domain - the domain to use in the primary mail attribute
+ secondary_domains - the secondary domains that are aliases
- (user_attrs, primary_domain, secondary_domains) = args
+ Return a list of secondary mail addresses
+ """
- user_attrs = utils.normalize(user_attrs)
+ user_attrs = utils.normalize(kw['user_attrs'])
- user_attrs['standard_domain'] = primary_domain
+ if not user_attrs.has_key('domain'):
+ user_attrs['domain'] = kw['primary_domain']
+ elif not user_attrs['domain'] == kw['primary_domain']:
+ user_attrs['domain'] = kw['primary_domain']
- exec("alternative_mail_routines = %s" % kw['secondary_mail'])
+ try:
+ exec("alternative_mail_routines = %s" % kw['secondary_mail'])
+ except Exception, e:
+ log.error(_("Could not parse the alternative mail routines"))
alternative_mail = []
for routine in alternative_mail_routines.keys():
- for _domain in [ primary_domain ] + secondary_domains:
+ for _domain in [ kw['primary_domain'] ] + kw['secondary_domains']:
user_attrs['domain'] = _domain
try:
exec("retval = '%s'.%s" % (routine,alternative_mail_routines[routine] % user_attrs))
except KeyError, e:
- self.conf.log.warning(_("Attribute substitution for 'alternative_mail' failed in Recipient Policy"))
+ log.warning(_("Attribute substitution for 'alternative_mail' failed in Recipient Policy"))
alternative_mail.append(retval)
return alternative_mail