00001 """
00002 Template filler ...
00003
00004 Pipe in the source ...
00005 define the class to parse the source and the template to fill in the options
00006
00007 cat $DYBDBIROOT/spec/GSimPmtSpec.spec | filltmpl.py --template="$DYBDBIROOT/templates/T.h" --parser="DybDbiPre.Parser" --debug
00008 cat $DYBDBIROOT/spec/GSimPmtSpec.spec | filltmpl.py --debug
00009
00010 """
00011
00012 def load_cls(spec):
00013 spec = spec.split('.')
00014 modl = __import__( '.'.join(spec[:-1]) , fromlist=[spec[-1]] )
00015 cls = getattr( modl , spec[-1] )
00016 return cls
00017
00018
00019 def dj_fill( src, _ ):
00020 opts, args = _
00021 try:
00022 parser_cls = load_cls(opts.parser)
00023 except:
00024 raise Exception("failed to load parser class with spec %s" % opts.parser )
00025 parser = parser_cls()
00026 ctx = parser( src )
00027
00028 extras = getattr( ctx , 'extras' , {} )
00029
00030 if opts.debug:
00031 print "FillTmpl debug.. "
00032 print locals()
00033
00034 import django
00035 import django.template.loader
00036 from django.template import Context, Template
00037
00038 import os
00039 dbg = os.environ.has_key('VERBOSE')
00040 tmpl = os.path.expandvars( opts.template )
00041 tdir = os.path.dirname( tmpl )
00042 django.conf.settings.configure(DEBUG=dbg, TEMPLATE_DEBUG=dbg, TEMPLATE_STRING_IF_INVALID="filltmpl-missing-noodles", TEMPLATE_DIRS=(tdir,) )
00043 t = Template( file(tmpl).read() )
00044 template_object_name = extras.pop('template_object_name', 't' )
00045 c = Context( { template_object_name:ctx } )
00046 c.update( extras )
00047 return t.render( c )
00048
00049 def main():
00050 import sys
00051 from optparse import OptionParser
00052 opr = OptionParser()
00053 opr.add_option("-p", "--parser", help="python dotted spec of callable parser class" )
00054 opr.add_option("-t", "--template", help="path to template file" )
00055 opr.add_option("-d", "--debug" , action="store_true", help="provide debug output" )
00056 opr.set_defaults( debug=False, ctxobj=False, parser="DybDbiPre.Parser", template="$DYBDBIROOT/templates/T.h" )
00057 print dj_fill( sys.stdin, opr.parse_args() )
00058
00059
00060 if __name__=='__main__':
00061 main()
00062
00063
00064
00065
00066