language namespace for plugins separated

plugin interface changed ("prepareForm" removed)
plugins do not raise exceptions anymore
first part of the partitioning plugin
device-specific stuff moved to CryptoBoxTools
This commit is contained in:
lars 2006-09-14 12:33:01 +00:00
parent f2a7ceb61c
commit de3280806f
26 changed files with 622 additions and 309 deletions

View file

@ -1,9 +1,21 @@
import CryptoBox
import WebInterfaceDataset
import re
from Plugins import PluginManager
import Plugins
from CryptoBoxExceptions import *
class WebInterfacePlugins:
def __init__(self, log, plugins, handler_func):
for plname in plugins.allPlugins():
log.info("Plugin '%s' loaded" % plname)
## this should be the "easiest" way to expose all plugins as URLs
setattr(self, plname, handler_func(plname))
setattr(getattr(self, plname), "exposed", True)
class WebInterfaceSites:
'''
url2func = {'index':'show_status','doc':'show_doc','logs':'show_log'}
@ -16,17 +28,10 @@ class WebInterfaceSites:
self.log = logging.getLogger("CryptoBox")
self.prefs = self.cbox.prefs
self.__resetDataset()
self.plugins = PluginManager(self.prefs["Locations"]["PluginDir"])
self.__exposePlugins()
self.pluginList = Plugins.PluginManager(self.prefs["Locations"]["PluginDir"])
self.plugins = WebInterfacePlugins(self.log, self.pluginList, self.return_plugin_action)
self.plugins.index = self.system
def __exposePlugins(self):
for plname in self.plugins.allPlugins():
self.log.info("Plugin '%s' loaded" % plname)
## this should be the "easiest" way to expose all modules as URLs
setattr(self, "module_" + plname, self.return_module_action(plname))
setattr(getattr(self, "module_" + plname), "exposed", True)
def __resetDataset(self):
self.dataset = WebInterfaceDataset.WebInterfaceDataset(self.cbox, self.prefs)
@ -134,8 +139,8 @@ class WebInterfaceSites:
try:
container.setName(volume_name)
# TODO: specify the possible exceptions
except Exception:
self.log.warn("failed to rename the volume '%s' to '%s'" % (device, volume_name))
except Exception, errMsg:
self.log.warn("failed to rename the volume '%s' to '%s: %s'" % (device, volume_name, errMsg))
self.dataset["Data.Warning"] = "SetVolumeNameFailed"
else:
self.log.info("successfully renamed volume '%s' to '%s'" % (device, volume_name))
@ -311,7 +316,7 @@ class WebInterfaceSites:
return self.__render("show_volume")
def return_module_action(self, module):
def return_plugin_action(self, plugin_name):
def handler(**args):
self.__resetDataset()
try:
@ -319,15 +324,9 @@ class WebInterfaceSites:
del args["weblang"]
except KeyError:
pass
plugin = self.plugins.getPlugin(module)
try:
nextTemplate = plugin.doAction(self.cbox, **args)
except CBPluginActionError, errMsg:
self.log.debug(errMsg)
self.dataset["Data.Warning"] = errMsg
nextTemplate = "empty"
plugin.prepareForm(self.dataset, self.cbox)
return self.__render(nextTemplate, module)
plugin = self.pluginList.getPlugin(plugin_name)
nextTemplate = plugin.doAction(self.dataset, self.cbox, **args)
return self.__render(nextTemplate, plugin_name)
return handler
@ -418,7 +417,7 @@ class WebInterfaceSites:
return False
def __render(self, template, module=None):
def __render(self, template, plugin=None):
'''renders from clearsilver templates and returns the resulting html
Gets a dictionary with all settings, nessessary for rendering.
@ -430,16 +429,16 @@ class WebInterfaceSites:
try:
import neo_cgi, neo_util, neo_cs
except ImportError:
errorMsg = "Could not import clearsilver modules. Try 'apt-get install python-clearsilver'."
errorMsg = "Could not import clearsilver module. Try 'apt-get install python-clearsilver'."
self.log.error(errorMsg)
sys.stderr.write(errorMsg)
raise ImportError, errorMsg
module_cs_file = False
if module:
module_cs_file = self.plugins.getTemplateFileName(module, template)
plugin_cs_file = False
if plugin:
plugin_cs_file = self.pluginList.getTemplateFileName(plugin, template)
default_cs_file = os.path.join(self.prefs["Locations"]["TemplateDir"], template + ".cs")
self.dataset["Data.TemplateFile"] = module_cs_file or default_cs_file
self.dataset["Data.TemplateFile"] = plugin_cs_file or default_cs_file
self.log.info("rendering site: " + template)
cs_path = os.path.join(self.prefs["Locations"]["TemplateDir"], "main.cs")
@ -454,7 +453,7 @@ class WebInterfaceSites:
return "Couldn't read language file: %s" % hdf_path
## add the current state of the plugins to the hdf dataset
self.dataset.setPluginState(self.plugins)
self.dataset.setPluginState(self.pluginList)
hdf = neo_util.HDF()
hdf.readFile(hdf_path)
@ -462,7 +461,7 @@ class WebInterfaceSites:
for key in self.dataset.keys():
hdf.setValue(key,str(self.dataset[key]))
## load languaga data of plugins
self.plugins.loadLanguageData(hdf, lang=self.dataset["Settings.Language"])
self.pluginList.loadLanguageData(hdf, lang=self.dataset["Settings.Language"])
cs = neo_cs.CS(hdf)
cs.parseFile(cs_path)
return cs.render()