diff options
author | Narayan Desai <desai@mcs.anl.gov> | 2007-11-09 02:16:44 +0000 |
---|---|---|
committer | Narayan Desai <desai@mcs.anl.gov> | 2007-11-09 02:16:44 +0000 |
commit | 21f839c7292f7f6f24c03dadc9c1a1b0c282aafe (patch) | |
tree | 03a1bb375f4a5740c5bde4dd8d548edb8d177bed /src/lib/Server | |
parent | c9286fe92cea4f7be3ed2a5dac1a49c0a7a1a2dd (diff) | |
download | bcfg2-21f839c7292f7f6f24c03dadc9c1a1b0c282aafe.tar.gz bcfg2-21f839c7292f7f6f24c03dadc9c1a1b0c282aafe.tar.bz2 bcfg2-21f839c7292f7f6f24c03dadc9c1a1b0c282aafe.zip |
SGenshi improvements from Sebastien Barthelemy (sorry, bad locale) and improved error handling
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@3944 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib/Server')
-rw-r--r-- | src/lib/Server/Plugins/SGenshi.py | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/src/lib/Server/Plugins/SGenshi.py b/src/lib/Server/Plugins/SGenshi.py index 11731ef78..76d1dd6bf 100644 --- a/src/lib/Server/Plugins/SGenshi.py +++ b/src/lib/Server/Plugins/SGenshi.py @@ -1,37 +1,69 @@ '''This module implements a templating generator based on Genshi''' __revision__ = '$Revision$' -import genshi.template import Bcfg2.Server.Plugin import lxml.etree, logging logger = logging.getLogger('Bcfg2.Plugins.SGenshi') +try: + import genshi.template, genshi.core, genshi.template.base +except: + logger.error("Failed to load Genshi; disabling SGenshi") + raise + +def removecomment(stream): + """A genshi filter that removes comments from the stream.""" + for kind, data, pos in stream: + if kind is genshi.core.COMMENT: + continue + yield kind, data, pos + class TemplateFile(Bcfg2.Server.Plugin.FileBacked): + '''Genshi template file object''' loader = genshi.template.TemplateLoader(auto_reload=True, max_cache_size=64) def HandleEvent(self, _=None): - self.template = self.loader.load(self.name, - cls=genshi.template.MarkupTemplate) + '''Process FAM/GAMIN event''' + try: + self.template = self.loader.load(self.name, + cls=genshi.template.MarkupTemplate) + except genshi.template.base.TemplateSyntaxError, e: + logger.error("SGenshi: Parse failure due to %s" % (e)) def GetValue(self, metadata): - stream = self.template.generate(metadata=metadata) + '''Build actual structure contents''' + if not hasattr(self, 'template'): + logger.error("Template data for %s could not be loaded" % self.name) + raise Bcfg2.Server.Plugin.PluginExecutionError + stream = self.template.generate(metadata=metadata).filter(removecomment) data = stream.render('xml') return lxml.etree.XML(data) class SGenshi(Bcfg2.Server.Plugin.Plugin, Bcfg2.Server.Plugin.XMLDirectoryBacked): + '''SGenshi is a structure plugin that provides direct plugin access to templated structures''' __child__ = TemplateFile __name__ = 'SGenshi' __version__ = '$Id$' def __init__(self, core, datastore): Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore) - Bcfg2.Server.Plugin.XMLDirectoryBacked.__init__(self, - self.data, - self.core.fam) + try: + Bcfg2.Server.Plugin.XMLDirectoryBacked.__init__(self, + self.data, + self.core.fam) + except: + logger.error("Failed to load SGenshi repository; disabling SGenshi") + raise Bcfg2.Server.Plugin.PluginInitError def BuildStructures(self, metadata): - return [entry.GetValue(metadata) \ - for entry in self.entries.values()] + '''Build SGenshi structures''' + ret = [] + for entry in self.entries.values(): + try: + ret.append(entry.GetValue(metadata)) + except Bcfg2.Server.Plugin.PluginExecutionError: + logger.error("SGenshi: Failed to template file %s" % entry.name) + return ret |