mirror of
https://github.com/CumulusNetworks/ifupdown2.git
synced 2024-05-06 15:54:50 +00:00
52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
|
#!/usr/bin/python
|
||
|
|
||
|
import logging
|
||
|
import traceback
|
||
|
from utils import *
|
||
|
|
||
|
class templateEngine():
|
||
|
|
||
|
def __init__(self, template_engine, template_lookuppath=None):
|
||
|
self.logger = logging.getLogger('ifupdown.' +
|
||
|
self.__class__.__name__)
|
||
|
self.tclass = None
|
||
|
self.tclassargs = {}
|
||
|
self.render = self._render_default
|
||
|
if template_engine == 'mako':
|
||
|
try:
|
||
|
self.tclass = utils.importName('mako.template', 'Template')
|
||
|
except Exception, e:
|
||
|
self.logger.warn('unable to load template engine %s (%s)'
|
||
|
%(template_engine, str(e)))
|
||
|
pass
|
||
|
if template_lookuppath:
|
||
|
try:
|
||
|
self.logger.debug('setting template lookuppath to %s'
|
||
|
%template_lookuppath)
|
||
|
lc = utils.importName('mako.lookup', 'TemplateLookup')
|
||
|
self.tclassargs['lookup'] = lc(
|
||
|
directories=template_lookuppath.split(':'))
|
||
|
except Exception, e:
|
||
|
self.logger.warn('unable to set template lookup path' +
|
||
|
' %s (%s)' %(template_lookuppath, str(e)))
|
||
|
pass
|
||
|
self.render = self._render_mako
|
||
|
else:
|
||
|
self.logger.info('skip template processing.., ' +
|
||
|
'template engine not found')
|
||
|
|
||
|
def _render_default(self, textdata):
|
||
|
return textdata
|
||
|
|
||
|
def _render_mako(self, textdata):
|
||
|
""" render textdata passed as argument using mako
|
||
|
|
||
|
Returns rendered textdata """
|
||
|
|
||
|
if not self.tclass:
|
||
|
return textdata
|
||
|
self.logger.info('template processing on interfaces file ...')
|
||
|
t = self.tclass(text=textdata, output_encoding='utf-8',
|
||
|
lookup=self.tclassargs.get('lookup'))
|
||
|
return t.render()
|