2006-11-06 17:05:00 +01:00
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# parent class for all plugins of the CryptoBox
|
|
|
|
#
|
2006-11-30 15:50:28 +01:00
|
|
|
# Copyright 2006 sense.lab e.V.
|
|
|
|
#
|
|
|
|
# This file is part of the CryptoBox.
|
|
|
|
#
|
|
|
|
# The CryptoBox 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; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# The CryptoBox 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with the CryptoBox; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
"""All features should inherit from this class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__revision__ = "$Id"
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import cherrypy
|
|
|
|
|
|
|
|
|
|
|
|
class CryptoBoxPlugin:
|
2006-12-05 13:24:47 +01:00
|
|
|
"""The base class of all features.
|
|
|
|
"""
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
## default capability is "system" - the other supported capability is: "volume"
|
2006-12-05 13:24:47 +01:00
|
|
|
plugin_capabilities = [ "system" ]
|
2006-11-06 17:05:00 +01:00
|
|
|
|
2006-11-08 13:20:30 +01:00
|
|
|
## where should the plugin be visible by default?
|
2006-12-05 13:24:47 +01:00
|
|
|
plugin_visibility = [ "preferences" ]
|
2006-11-08 13:20:30 +01:00
|
|
|
|
2006-11-06 17:05:00 +01:00
|
|
|
## does this plugin require admin authentification?
|
2006-12-05 13:24:47 +01:00
|
|
|
request_auth = False
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
## default rank (0..100) of the plugin in listings (lower value means higher priority)
|
|
|
|
rank = 80
|
|
|
|
|
|
|
|
|
|
|
|
## default icon of this plugin (relative path)
|
2006-12-05 13:24:47 +01:00
|
|
|
default_icon_filename = "plugin_icon.gif"
|
2006-11-06 17:05:00 +01:00
|
|
|
|
2006-11-15 12:47:37 +01:00
|
|
|
## fallback icon file (in the common plugin directory)
|
2006-12-05 13:24:47 +01:00
|
|
|
fallback_icon_filename = "plugin_icon_unknown.gif"
|
2006-11-15 12:47:37 +01:00
|
|
|
|
2006-11-06 17:05:00 +01:00
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def __init__(self, cbox, plugin_dir, site_class=None):
|
2006-11-06 17:05:00 +01:00
|
|
|
self.cbox = cbox
|
|
|
|
self.hdf = {}
|
2006-12-05 13:24:47 +01:00
|
|
|
self.plugin_dir = plugin_dir
|
|
|
|
self.hdf_prefix = "Data.Plugins.%s." % self.get_name()
|
|
|
|
self.site = site_class
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
|
2006-11-08 13:20:30 +01:00
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def do_action(self, **args):
|
|
|
|
"""Override do_action with your plugin code
|
|
|
|
"""
|
|
|
|
raise Exception, \
|
|
|
|
"undefined action handler ('do_action') in plugin '%s'" % self.get_name()
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def get_status(self):
|
|
|
|
"""you should override this, to supply useful state information
|
|
|
|
"""
|
|
|
|
raise Exception, \
|
|
|
|
"undefined state handler ('get_status') in plugin '%s'" % self.get_name()
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def get_name(self):
|
|
|
|
"""the name of the python file (module) should be the name of the plugin
|
|
|
|
"""
|
2006-11-06 17:05:00 +01:00
|
|
|
return self.__module__
|
|
|
|
|
|
|
|
|
2006-12-11 15:16:10 +01:00
|
|
|
def setup(self):
|
|
|
|
"""Any plugin that wants to define bootup actions may override this.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
"""Any plugin that wants to define shutdown actions may override this.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2006-11-06 17:05:00 +01:00
|
|
|
@cherrypy.expose
|
2006-12-05 13:24:47 +01:00
|
|
|
def get_icon(self, image=None, **kargs):
|
2006-11-06 17:05:00 +01:00
|
|
|
"""return the image data of the icon of the plugin
|
|
|
|
|
|
|
|
the parameter 'image' may be used for alternative image locations (relative
|
|
|
|
to the directory of the plugin)
|
2006-12-05 13:24:47 +01:00
|
|
|
'**kargs' is necessary, as a 'weblang' attribute may be specified (and ignored)
|
|
|
|
"""
|
|
|
|
import re
|
2006-12-11 15:16:10 +01:00
|
|
|
if (image is None) or (not re.match(r'[\w\-\.]*$', image)):
|
2006-12-05 13:24:47 +01:00
|
|
|
plugin_icon_file = os.path.join(self.plugin_dir, self.default_icon_filename)
|
2006-11-06 17:05:00 +01:00
|
|
|
else:
|
2006-12-05 13:24:47 +01:00
|
|
|
plugin_icon_file = os.path.join(self.plugin_dir, image)
|
2006-11-06 17:05:00 +01:00
|
|
|
if not os.access(plugin_icon_file, os.R_OK):
|
2006-12-05 13:24:47 +01:00
|
|
|
plugin_icon_file = os.path.join(
|
|
|
|
self.cbox.prefs["Locations"]["PluginDir"], self.fallback_icon_filename)
|
2006-11-06 17:05:00 +01:00
|
|
|
return cherrypy.lib.cptools.serveFile(plugin_icon_file)
|
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def get_template_filename(self, template_name):
|
2006-11-06 17:05:00 +01:00
|
|
|
"""return the filename of the template, if it is part of this plugin
|
|
|
|
|
|
|
|
use this function to check, if the plugin provides the specified template
|
|
|
|
"""
|
2006-12-05 13:24:47 +01:00
|
|
|
result_file = os.path.join(self.plugin_dir, template_name + ".cs")
|
2006-11-06 17:05:00 +01:00
|
|
|
if os.access(result_file, os.R_OK) and os.path.isfile(result_file):
|
|
|
|
return result_file
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def get_language_data(self):
|
|
|
|
"""Retrieve the language data of the feature.
|
|
|
|
|
|
|
|
Typically this is the content of the language.hdf file as a HDF object.
|
|
|
|
"""
|
2006-11-28 15:24:53 +01:00
|
|
|
import neo_cgi, neo_util
|
2006-11-06 17:05:00 +01:00
|
|
|
lang_hdf = neo_util.HDF()
|
2006-12-05 13:24:47 +01:00
|
|
|
lang_file = os.path.join(self.plugin_dir, 'language.hdf')
|
2006-11-28 15:24:53 +01:00
|
|
|
try:
|
2006-12-05 13:24:47 +01:00
|
|
|
lang_hdf.readFile(lang_file)
|
|
|
|
except (neo_util.Error, neo_util.ParseError):
|
|
|
|
self.cbox.log.error("failed to load language file (%s) of plugin (%s):" % \
|
|
|
|
(lang_file, self.get_name()))
|
2006-11-28 15:24:53 +01:00
|
|
|
return lang_hdf
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def load_dataset(self, hdf):
|
|
|
|
"""Add the local values of the feature to the hdf dataset.
|
|
|
|
"""
|
2006-11-06 17:05:00 +01:00
|
|
|
for (key, value) in self.hdf.items():
|
|
|
|
hdf.setValue(key, str(value))
|
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def is_auth_required(self):
|
2006-11-06 17:05:00 +01:00
|
|
|
"""check if this plugin requires authentication
|
|
|
|
first step: check plugin configuration
|
2006-12-05 13:24:47 +01:00
|
|
|
second step: check default value of plugin
|
|
|
|
"""
|
2006-11-06 17:05:00 +01:00
|
|
|
try:
|
2006-12-05 13:24:47 +01:00
|
|
|
if self.cbox.prefs.plugin_conf[self.get_name()]["requestAuth"] is None:
|
|
|
|
return self.request_auth
|
|
|
|
if self.cbox.prefs.plugin_conf[self.get_name()]["requestAuth"]:
|
2006-11-06 17:05:00 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except KeyError:
|
2006-12-05 13:24:47 +01:00
|
|
|
return self.request_auth
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def is_enabled(self):
|
2006-11-06 17:05:00 +01:00
|
|
|
"""check if this plugin is enabled
|
|
|
|
first step: check plugin configuration
|
2006-12-05 13:24:47 +01:00
|
|
|
second step: check default value of plugin
|
|
|
|
"""
|
|
|
|
fallback = bool(self.plugin_visibility)
|
2006-11-06 17:05:00 +01:00
|
|
|
try:
|
2006-12-05 13:24:47 +01:00
|
|
|
if self.cbox.prefs.plugin_conf[self.get_name()]["visibility"] is None:
|
2006-11-08 13:20:30 +01:00
|
|
|
return fallback
|
2006-12-05 13:24:47 +01:00
|
|
|
return bool(self.cbox.prefs.plugin_conf[self.get_name()]["visibility"])
|
2006-11-06 17:05:00 +01:00
|
|
|
except KeyError:
|
2006-11-08 13:20:30 +01:00
|
|
|
return fallback
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def get_rank(self):
|
2006-11-06 17:05:00 +01:00
|
|
|
"""check the rank of this plugin
|
|
|
|
first step: check plugin configuration
|
2006-12-05 13:24:47 +01:00
|
|
|
second step: check default value of plugin
|
|
|
|
"""
|
2006-11-06 17:05:00 +01:00
|
|
|
try:
|
2006-12-05 13:24:47 +01:00
|
|
|
if self.cbox.prefs.plugin_conf[self.get_name()]["rank"] is None:
|
2006-11-06 17:05:00 +01:00
|
|
|
return self.rank
|
2006-12-05 13:24:47 +01:00
|
|
|
return int(self.cbox.prefs.plugin_conf[self.get_name()]["rank"])
|
2006-11-22 16:21:21 +01:00
|
|
|
except (KeyError, TypeError):
|
2006-11-06 17:05:00 +01:00
|
|
|
return self.rank
|
2006-11-08 13:20:30 +01:00
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def get_visibility(self):
|
|
|
|
"""Check which visibility flags of the feature are set.
|
|
|
|
"""
|
2006-11-08 13:20:30 +01:00
|
|
|
try:
|
2006-12-05 13:24:47 +01:00
|
|
|
if self.cbox.prefs.plugin_conf[self.get_name()]["visibility"] is None:
|
|
|
|
return self.plugin_visibility[:]
|
|
|
|
return self.cbox.prefs.plugin_conf[self.get_name()]["visibility"]
|
2006-11-08 13:20:30 +01:00
|
|
|
except KeyError:
|
2006-12-05 13:24:47 +01:00
|
|
|
return self.plugin_visibility
|
2006-11-06 17:05:00 +01:00
|
|
|
|
|
|
|
|
2006-12-06 17:25:52 +01:00
|
|
|
def reset(self):
|
|
|
|
"""Reinitialize the plugin.
|
|
|
|
|
|
|
|
This function should be called before every run
|
|
|
|
"""
|
|
|
|
self.hdf = {}
|
|
|
|
|
|
|
|
|
2006-12-05 13:24:47 +01:00
|
|
|
def get_test_class(self):
|
|
|
|
"""Return the unittest class of the feature.
|
|
|
|
"""
|
2006-11-06 17:05:00 +01:00
|
|
|
import imp
|
2006-12-05 13:24:47 +01:00
|
|
|
pl_file = os.path.join(self.plugin_dir, "unittests.py")
|
2006-11-06 17:05:00 +01:00
|
|
|
if os.access(pl_file, os.R_OK) and os.path.isfile(pl_file):
|
|
|
|
try:
|
2006-12-05 13:24:47 +01:00
|
|
|
return imp.load_source("unittests_%s" % self.get_name(), pl_file).unittests
|
2006-11-06 17:05:00 +01:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
try:
|
2006-12-05 13:24:47 +01:00
|
|
|
self.cbox.log.info("could not load unittests for plugin: %s" % \
|
|
|
|
self.get_name())
|
2006-11-06 17:05:00 +01:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|