Re: Does Cheetah count line numbers its written?
James Abbatiello <[email protected]> Wed, 23 Mar 2011 23:22:49 -0400
| Newsgroups | gmane.comp.python.cheetah |
|---|---|
| Message-ID | <[email protected]> |
--===============8597299208491947675== Content-Type: multipart/alternative; boundary=000e0cd2544eef95ea049f31fdc7 --000e0cd2544eef95ea049f31fdc7 Content-Type: text/plain; charset=ISO-8859-1 Hi Nate, I guess I'm not quite clear on what you're trying to do. If you don't want the line number from the original template then what line number do you want? Could you provide some small theoretical example of your desired input and what you'd expect it to produce? -- James Abbatiello On Fri, Mar 11, 2011 at 9:29 PM, Nate Reid <[email protected]> wrote: > Hi James, > In my work, I am using the command line cheetah compiler (e.g. cheetah > script) so I had to hack a version of the command-line CheetahWrapper by > monkey patching in a version that lets me specify a compilerClass to the > Template.compile command. My changes are specifying the compilerClass in > the compille command below. > > #!/bin/env python > import Cheetah.CheetahWrapper > from Cheetah.CheetahWrapper import CheetahWrapper > from cheetah_compilers import LineNumberCompiler > import os > import shutil > moduleNameRE = Cheetah.CheetahWrapper.moduleNameRE > > class MyCheetahWrapper(CheetahWrapper): > def __init__(self, *args, **kwargs): > super(MyCheetahWrapper, self).__init__(*args, **kwargs) > > def _compileOrFillBundle(self, b): > C, D, W = self.chatter, self.debug, self.warn > TemplateClass = self._getTemplateClass() > compilerSettings = self._getCompilerSettings() > src = b.src > dst = b.dst > base = b.base > basename = b.basename > dstDir = os.path.dirname(dst) > what = self.isCompile and "Compiling" or "Filling" > C("%s %s -> %s^", what, src, dst) # No trailing newline. > if os.path.exists(dst) and not self.opts.nobackup: > bak = b.bak > C(" (backup %s)", bak) # On same line as previous message. > else: > bak = None > C("") > if self.isCompile: > if not moduleNameRE.match(basename): > tup = basename, src > raise Error("""\ > %s: base name %s contains invalid characters. It must > be named according to the same rules as Python modules.""" % tup) > pysrc = TemplateClass.compile(file=src, returnAClass=False, > moduleName=basename, > className=basename, > commandlineopts=self.opts, > > compilerSettings=compilerSettings, > compilerClass=LineNumberCompiler) > output = pysrc > else: > #output = str(TemplateClass(file=src, > searchList=self.searchList)) > tclass = TemplateClass.compile(file=src, > compilerSettings=compilerSettings, compilerClass=LineNumberCompiler) > output = str(tclass(searchList=self.searchList)) > > if bak: > shutil.copyfile(dst, bak) > if dstDir and not os.path.exists(dstDir): > if self.isCompile: > mkdirsWithPyInitFiles(dstDir) > else: > os.makedirs(dstDir) > if self.opts.stdout: > sys.stdout.write(output) > else: > f = open(dst, 'w') > f.write(output) > f.close() > > if __name__ == '__main__': > MyCheetahWrapper().main() > > > Where LineNumberCompiler is the MyCompiler class you created in the > original e-mail. > The problem is that when the templates are compiled, it seems to hardcode > the $LINE variable directly into the source code, so when I actually fill > the templates, > the $LINE variable no longer corresponds to the actual line in the source > code. It also doesn't allow me to use a #def to define a function that lets > me spit out the #line $LINE "filename.cpp" directive. E.g. it just replaces > $LINE with the line number that it was first defined, rather than the > dynamic one. > > Is there another way that Cheetah can actually count the lines it's written > while filling the template? > > Thanks! > -Nate > > ------------------------------ > From: [email protected] > To: [email protected] > Date: Wed, 9 Mar 2011 11:23:04 -0800 > > CC: [email protected] > Subject: Re: [Cheetahtemplate-discuss] Does Cheetah count line numbers its > written? > > Thanks James, I'll give this a try and thanks for taking the time to write > this up. > Cheers > -Nate > > ------------------------------ > Date: Wed, 9 Mar 2011 01:03:20 -0500 > Subject: Re: [Cheetahtemplate-discuss] Does Cheetah count line numbers its > written? > From: [email protected] > To: [email protected] > CC: [email protected] > > This is a bit ugly but it may work for you: > > #!/usr/bin/env python > import Cheetah.Compiler > import Cheetah.Template > > class MyAutoMethodCompiler(Cheetah.Compiler.AutoMethodCompiler): > def addPlaceholder(self, expr, filterArgs, rawPlaceholder, > cacheTokenParts, lineCol, > silentMode=False): > if rawPlaceholder == u'$LINE': > self.addFilteredChunk(repr(unicode(lineCol[0])), > filterArgs, > rawPlaceholder, > lineCol=lineCol) > else: > super(MyAutoMethodCompiler, self).addPlaceholder( > expr, filterArgs, rawPlaceholder, > cacheTokenParts, lineCol, > silentMode=silentMode) > > > class MyAutoClassCompiler(Cheetah.Compiler.AutoClassCompiler): > methodCompilerClass = MyAutoMethodCompiler > > > class MyCompiler(Cheetah.Compiler.Compiler): > classCompilerClass = MyAutoClassCompiler > > > tclass = Cheetah.Template.Template.compile('''\ > This is line $LINE > Hello, $name! > And this is line $LINE''', compilerClass=MyCompiler) > print tclass(searchList=[{"name": "Nate"}]) > > > -- > James Abbatiello > > > On Tue, Mar 8, 2011 at 1:45 PM, Nate Reid <[email protected]> wrote: > > I'm writing a source generator using Cheetah templates for C++ and would > like to add #line directives into the generated source. Without delving > deep into the Cheetah source code, I was wondering if anyone on the list > knew if Cheetah keeps track of how many lines are written so I can easily > query the current line number in the template and use that in the template > output itself for inserting #line directives into the generated source code. > Thanks! > -Nate > > > ------------------------------------------------------------------------------ > What You Don't Know About Data Connectivity CAN Hurt You > This paper provides an overview of data connectivity, details > its effect on application quality, and explores various alternative > solutions. http://p.sf.net/sfu/progress-d2d > _______________________________________________ > Cheetahtemplate-discuss mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss > > > > ------------------------------------------------------------------------------ > Colocation vs. Managed Hosting A question and answer guide to determining > the best fit for your organization - today and in the future. > http://p.sf.net/sfu/internap-sfd2d > _______________________________________________ Cheetahtemplate-discuss > mailing list [email protected] > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss > > > ------------------------------------------------------------------------------ > Colocation vs. Managed Hosting > A question and answer guide to determining the best fit > for your organization - today and in the future. > http://p.sf.net/sfu/internap-sfd2d > _______________________________________________ > Cheetahtemplate-discuss mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss > > --000e0cd2544eef95ea049f31fdc7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi Nate,<br><br>I guess I'm not quite clear on what you're trying t= o do.=A0 If you don't want the line number from the original template t= hen what line number do you want?=A0 Could you provide some small theoretic= al example of your desired input and what you'd expect it to produce?<b= r> <br>-- <br>James Abbatiello<br><br><br><div class=3D"gmail_quote">On Fri, M= ar 11, 2011 at 9:29 PM, Nate Reid <span dir=3D"ltr"><<a href=3D"mailto:g= [email protected]">[email protected]</a>></span> wrote:<br><blockquot= e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol= id;padding-left:1ex;"> <div> Hi James,<br>In my work, I am using the command line cheetah compiler (e.g.= cheetah script) so I had to hack a version of the command-line CheetahWrap= per by monkey patching in a version that lets me specify a compilerClass to= the Template.compile command.=A0 My changes are specifying the compilerCla= ss in the compille command below.<br> <br>#!/bin/env python<br>import Cheetah.CheetahWrapper<br>from Cheetah.Chee= tahWrapper import CheetahWrapper<br>from cheetah_compilers import LineNumbe= rCompiler<br>import os<br>import shutil<br>moduleNameRE =3D Cheetah.Cheetah= Wrapper.moduleNameRE<br> <br>class MyCheetahWrapper(CheetahWrapper):<br>=A0=A0=A0 def __init__(self,= *args, **kwargs):<br>=A0=A0=A0=A0=A0=A0=A0 super(MyCheetahWrapper, self)._= _init__(*args, **kwargs)<br><br>=A0=A0=A0 def _compileOrFillBundle(self, b)= :<br>=A0=A0=A0=A0=A0=A0=A0 C, D, W =3D self.chatter, self.debug, self.warn<= br> =A0=A0=A0=A0=A0=A0=A0 TemplateClass =3D self._getTemplateClass()<br>=A0=A0= =A0=A0=A0=A0=A0 compilerSettings =3D self._getCompilerSettings()<br>=A0=A0= =A0=A0=A0=A0=A0 src =3D b.src<br>=A0=A0=A0=A0=A0=A0=A0 dst =3D b.dst<br>=A0= =A0=A0=A0=A0=A0=A0 base =3D b.base<br>=A0=A0=A0=A0=A0=A0=A0 basename =3D b.= basename<br>=A0=A0=A0=A0=A0=A0=A0 dstDir =3D os.path.dirname(dst)<br> =A0=A0=A0=A0=A0=A0=A0 what =3D self.isCompile and "Compiling" or = "Filling"<br>=A0=A0=A0=A0=A0=A0=A0 C("%s %s -> %s^",= what, src, dst) # No trailing newline.<br>=A0=A0=A0=A0=A0=A0=A0 if os.path= .exists(dst) and not self.opts.nobackup:<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 bak =3D b.bak<br>=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0 C(" (backup %s)", bak) # On same line as previous messa= ge.<br>=A0=A0=A0=A0=A0=A0=A0 else:<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 bak= =3D None<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 C("")<br>=A0=A0=A0= =A0=A0=A0=A0 if self.isCompile:<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if not= moduleNameRE.match(basename):<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 tup =3D basename, src<br>=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 raise Error("""\<= br>%s: base name %s contains invalid characters.=A0 It must<br>be named acc= ording to the same rules as Python modules.""" % tup)<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 pysrc =3D TemplateClass.compile(file=3Dsr= c, returnAClass=3DFalse,<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0 moduleName=3Dbasename,<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0 className=3Dbasename,<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0 commandlineopts=3Dself.opts,<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 compilerSettings=3Dcompile= rSettings,<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 compilerCla= ss=3DLineNumberCompiler)<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 output =3D py= src<br>=A0=A0=A0=A0=A0=A0=A0 else:<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 #ou= tput =3D str(TemplateClass(file=3Dsrc, searchList=3Dself.searchList))<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 tclass =3D TemplateClass.compile(file=3Ds= rc, compilerSettings=3DcompilerSettings, compilerClass=3DLineNumberCompiler= )<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 output =3D str(tclass(searchList=3Ds= elf.searchList))<br><br>=A0=A0=A0=A0=A0=A0=A0 if bak:<br>=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0 shutil.copyfile(dst, bak)<br> =A0=A0=A0=A0=A0=A0=A0 if dstDir and not os.path.exists(dstDir):<br>=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 if self.isCompile:<br>=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 mkdirsWithPyInitFiles(dstDir)<br>=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0 else:<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 os.m= akedirs(dstDir)<br>=A0=A0=A0=A0=A0=A0=A0 if self.opts.stdout:<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 sys.stdout.write(output)<br>=A0=A0=A0=A0= =A0=A0=A0 else:<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 f =3D open(dst, 'w= ')<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 f.write(output)<br>=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 f.close()<br><br>if __name__ =3D=3D '__main__'= ;:<br>=A0=A0=A0 MyCheetahWrapper().main()<br> <br><br>Where LineNumberCompiler is the MyCompiler class you created in the= original e-mail.<br>The problem is that when the templates are compiled, i= t seems to hardcode the $LINE variable directly into the source code, so wh= en I actually fill the templates,<br> the $LINE variable no longer corresponds to the actual line in the source c= ode.=A0 It also doesn't allow me to use a #def to define a function tha= t lets me spit out the #line $LINE "filename.cpp" directive.=A0 E= .g. it just replaces $LINE with the line number that it was first defined, = rather than the dynamic one.<br> <br>Is there another way that Cheetah can actually count the lines it's= written while filling the template?<br><br>Thanks!<br>-Nate<br><br><hr>Fro= m: <a href=3D"mailto:[email protected]" target=3D"_blank">gnatty7@hotmail= .com</a><br> To: <a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]<= /a><br>Date: Wed, 9 Mar 2011 11:23:04 -0800<div class=3D"im"><br>CC: <a hre= f=3D"mailto:[email protected]" target=3D"_blank= ">[email protected]</a><br> </div><div><div></div><div class=3D"h5">Subject: Re: [Cheetahtemplate-discu= ss] Does Cheetah count line numbers its written?<br><br> Thanks James, I'll give this a try and thanks for taking the time to wr= ite this up.<br>Cheers<br>-Nate<br><br><hr>Date: Wed, 9 Mar 2011 01:03:20 -= 0500<br>Subject: Re: [Cheetahtemplate-discuss] Does Cheetah count line numb= ers its written?<br> From: <a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]= m</a><br>To: <a href=3D"mailto:[email protected]" target=3D"_blank">gnatt= [email protected]</a><br>CC: <a href=3D"mailto:[email protected]= ourceforge.net" target=3D"_blank">[email protected]= .net</a><br> <br>This is a bit ugly but it may work for you:<br><br>#!/usr/bin/env pytho= n<br>import Cheetah.Compiler<br>import Cheetah.Template<br><br>class MyAuto= MethodCompiler(Cheetah.Compiler.AutoMethodCompiler):<br>=A0=A0=A0 def addPl= aceholder(self, expr, filterArgs, rawPlaceholder,<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 cacheTok= enParts, lineCol,<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 silentMode=3DFalse):<br>=A0=A0=A0=A0=A0=A0=A0 if rawPlaceholde= r =3D=3D u'$LINE':<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 self.addFil= teredChunk(repr(unicode(lineCol[0])),<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 filterAr= gs,<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0 rawPlaceholder,<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 lineC= ol=3DlineCol)<br>=A0=A0=A0=A0=A0=A0=A0 else:<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 super(MyAutoMethodCompiler, self).addPlaceholder(<br>=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 expr, filterArgs, rawPlaceholder,<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 cacheTokenParts, lineCol,<br>= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 silentMode=3DsilentMode)<br><= br><br>class MyAutoClassCompiler(Cheetah.Compiler.AutoClassCompiler):<br>= =A0=A0=A0 methodCompilerClass =3D MyAutoMethodCompiler<br><br><br>class MyC= ompiler(Cheetah.Compiler.Compiler):<br> =A0=A0=A0 classCompilerClass =3D MyAutoClassCompiler<br><br><br>tclass =3D = Cheetah.Template.Template.compile('''\<br>This is line $LINE<br= >Hello, $name!<br>And this is line $LINE''', compilerClass=3DMy= Compiler)<br> print tclass(searchList=3D[{"name": "Nate"}])<br><br><b= r>-- <br>James Abbatiello<br><br><br><div>On Tue, Mar 8, 2011 at 1:45 PM, N= ate Reid <span dir=3D"ltr"><<a href=3D"mailto:[email protected]" targe= t=3D"_blank">[email protected]</a>></span> wrote:<br> <blockquote style=3D"padding-left:1ex"> <div> I'm writing a source generator using Cheetah templates for C++ and woul= d like to add #line directives into the generated source.=A0 Without delvin= g deep into the Cheetah source code, I was wondering if anyone on the list = knew if Cheetah keeps track of how many lines are written so I can easily q= uery the current line number in the template and use that in the template o= utput itself for inserting #line directives into the generated source code.= <br> Thanks!<br>-Nate<br> </div> <br>-----------------------------------------------------------------------= -------<br> What You Don't Know About Data Connectivity CAN Hurt You<br> This paper provides an overview of data connectivity, details<br> its effect on application quality, and explores various alternative<br> solutions. <a href=3D"http://p.sf.net/sfu/progress-d2d" target=3D"_blank">h= ttp://p.sf.net/sfu/progress-d2d</a><br>____________________________________= ___________<br> Cheetahtemplate-discuss mailing list<br> <a href=3D"mailto:[email protected]" target=3D"= _blank">[email protected]</a><br> <a href=3D"https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-dis= cuss" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/cheeta= htemplate-discuss</a><br> <br></blockquote></div><br> =20 <br></div></div>-----------------------------------------------------------= ------------------- Colocation vs. Managed Hosting A question and answer guide to determining the best fit for your organization - today and in the future. <a href=3D"http://p.sf.net/sfu/internap-sfd2d" target=3D"_blank">http://p.s= f.net/sfu/internap-sfd2d</a><br>___________________________________________= ____ Cheetahtemplate-discuss mailing list <a href=3D"mailto:[email protected]" target=3D"= _blank">[email protected]</a> <a href=3D"https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-dis= cuss" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/cheeta= htemplate-discuss</a> </div> <br>-----------------------------------------------------------------------= -------<br> Colocation vs. Managed Hosting<br> A question and answer guide to determining the best fit<br> for your organization - today and in the future.<br> <a href=3D"http://p.sf.net/sfu/internap-sfd2d" target=3D"_blank">http://p.s= f.net/sfu/internap-sfd2d</a><br>___________________________________________= ____<br> Cheetahtemplate-discuss mailing list<br> <a href=3D"mailto:[email protected]">Cheetahtem= [email protected]</a><br> <a href=3D"https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-dis= cuss" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/cheeta= htemplate-discuss</a><br> <br></blockquote></div><br> --000e0cd2544eef95ea049f31fdc7-- --===============8597299208491947675== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ Enable your software for Intel(R) Active Management Technology to meet the growing manageability and security demands of your customers. Businesses are taking advantage of Intel(R) vPro (TM) technology - will your software be a part of the solution? Download the Intel(R) Manageability Checker today! http://p.sf.net/sfu/intel-dev2devmar --===============8597299208491947675== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Cheetahtemplate-discuss mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss --===============8597299208491947675==--