root/trunk/bcfg2/src/lib/Server/Plugins/TCheetah.py

Revision 5509, 2.5 KB (checked in by solj, 4 weeks ago)

TGenshi/TCheetah: Set type to 'file' for Path entries

Similar to r5475, we need to set the entry type of templated ConfigFiles?
so that they are mapped properly by the POSIXCompat plugin.

Signed-off-by: Sol Jerome <solj@…>

  • Property svn:eol-style set to native
  • Property cvs2svn:cvs-rev set to 1.5
  • Property svn:keywords set to Date Author Id Revision
Line 
1'''This module implements a templating generator based on Cheetah'''
2__revision__ = '$Revision$'
3
4import logging
5import sys
6import traceback
7import Bcfg2.Server.Plugin
8
9logger = logging.getLogger('Bcfg2.Plugins.TCheetah')
10
11try:
12    import Cheetah.Template
13    import Cheetah.Parser
14except:
15    logger.error("TCheetah: Failed to import Cheetah. Is it installed?")
16    raise
17
18
19class TemplateFile:
20    '''Template file creates Cheetah template structures for the loaded file'''
21
22    def __init__(self, name, specific, encoding):
23        self.name = name
24        self.specific = specific
25        self.encoding = encoding
26        self.template = None
27        self.searchlist = dict()
28
29    def handle_event(self, event):
30        '''Handle all fs events for this template'''
31        if event.code2str() == 'deleted':
32            return
33        try:
34            s = {'useStackFrames': False}
35            self.template = Cheetah.Template.Template(open(self.name).read(),
36                                                      compilerSettings=s,
37                                                      searchList=self.searchlist)
38        except Cheetah.Parser.ParseError, perror:
39            logger.error("Cheetah parse error for file %s" % (self.name))
40            logger.error(perror.report())
41
42    def bind_entry(self, entry, metadata):
43        '''Build literal file information'''
44        self.template.metadata = metadata
45        self.searchlist['metadata'] = metadata
46        self.template.path = entry.get('realname', entry.get('name'))
47        self.searchlist['path'] = entry.get('realname', entry.get('name'))
48        self.template.source_path = self.name
49        self.searchlist['source_path'] = self.name
50
51        if entry.tag == 'Path':
52            entry.set('type', 'file')
53        try:
54            if type(self.template) == unicode:
55                entry.text = self.template
56            else:
57                entry.text = unicode(str(self.template), self.encoding)
58        except:
59            (a, b, c) = sys.exc_info()
60            msg = traceback.format_exception(a, b, c, limit=2)[-1][:-1]
61            logger.error(msg)
62            logger.error("TCheetah template error for %s" % self.searchlist['path'])
63            del a, b, c
64            raise Bcfg2.Server.Plugin.PluginExecutionError
65
66
67class TCheetah(Bcfg2.Server.Plugin.GroupSpool):
68    '''The TCheetah generator implements a templating mechanism for configuration files'''
69    name = 'TCheetah'
70    __version__ = '$Id$'
71    __author__ = 'bcfg-dev@mcs.anl.gov'
72    filename_pattern = 'template'
73    es_child_cls = TemplateFile
Note: See TracBrowser for help on using the browser.