mirror of
https://github.com/CumulusNetworks/ifupdown2.git
synced 2024-05-06 15:54:50 +00:00
move ifupdown2/* .
ifupdown2 code was one level deeper because ifupdown2 initially had ifupdown2 and ifupdown2-addons as two separate packages. Since they were combined into one package, it makes sense to move all combined code under the top level directory Signed-off-by: Roopa Prabhu <roopa@cumulusnetworks.com>
This commit is contained in:
59
ifupdown/template.py
Normal file
59
ifupdown/template.py
Normal file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright 2014 Cumulus Networks, Inc. All rights reserved.
|
||||
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
|
||||
#
|
||||
# template --
|
||||
# helper class to render templates
|
||||
#
|
||||
|
||||
import logging
|
||||
import traceback
|
||||
from utils import *
|
||||
|
||||
class templateEngine():
|
||||
""" provides template rendering methods """
|
||||
|
||||
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()
|
Reference in New Issue
Block a user