<s> import re , inspect <EOL> from mako . lookup import TemplateLookup <EOL> from mako . template import Template <EOL> class TGPlugin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , extra_vars_func = None , options = None , extension = '<STR_LIT>' ) : <EOL> self . extra_vars_func = extra_vars_func <EOL> self . extension = extension <EOL> if not options : <EOL> options = { } <EOL> lookup_options = { } <EOL> for k , v in options . items ( ) : <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> lookup_options [ k [ <NUM_LIT:5> : ] ] = v <EOL> elif k in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> lookup_options [ k ] = v <EOL> self . lookup = TemplateLookup ( ** lookup_options ) <EOL> self . tmpl_options = { } <EOL> for kw in inspect . getargspec ( Template . __init__ ) [ <NUM_LIT:0> ] : <EOL> if kw in lookup_options : <EOL> self . tmpl_options [ kw ] = lookup_options [ kw ] <EOL> def load_template ( self , templatename , template_string = None ) : <EOL> """<STR_LIT>""" <EOL> if template_string is not None : <EOL> return Template ( template_string , ** self . tmpl_options ) <EOL> if '<STR_LIT:/>' not in templatename : <EOL> templatename = '<STR_LIT:/>' + templatename . replace ( '<STR_LIT:.>' , '<STR_LIT:/>' ) + '<STR_LIT:.>' + self . extension <EOL> return self . lookup . get_template ( templatename ) <EOL> def render ( self , info , format = "<STR_LIT:html>" , fragment = False , template = None ) : <EOL> if isinstance ( template , str ) : <EOL> template = self . load_template ( template ) <EOL> if self . extra_vars_func : <EOL> info . update ( self . extra_vars_func ( ) ) <EOL> return template . render ( ** info ) </s>
