| 1 | '''This module implements a templating generator based on Cheetah''' |
|---|
| 2 | __revision__ = '$Revision$' |
|---|
| 3 | |
|---|
| 4 | import logging |
|---|
| 5 | import sys |
|---|
| 6 | import traceback |
|---|
| 7 | import Bcfg2.Server.Plugin |
|---|
| 8 | |
|---|
| 9 | logger = logging.getLogger('Bcfg2.Plugins.TCheetah') |
|---|
| 10 | |
|---|
| 11 | try: |
|---|
| 12 | import Cheetah.Template |
|---|
| 13 | import Cheetah.Parser |
|---|
| 14 | except: |
|---|
| 15 | logger.error("TCheetah: Failed to import Cheetah. Is it installed?") |
|---|
| 16 | raise |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | class 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 | |
|---|
| 67 | class 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 |
|---|