summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/SGenshi.py
blob: 11731ef78a67a4127f863db5544e631a83c1bf0a (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
'''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')

class TemplateFile(Bcfg2.Server.Plugin.FileBacked):
    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)
        
    def GetValue(self, metadata):
        stream = self.template.generate(metadata=metadata)
        data = stream.render('xml')
        return lxml.etree.XML(data)

class SGenshi(Bcfg2.Server.Plugin.Plugin,
              Bcfg2.Server.Plugin.XMLDirectoryBacked):
    __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)
        
    def BuildStructures(self, metadata):
        return [entry.GetValue(metadata) \
                for entry in self.entries.values()]