Re: Does Cheetah count line numbers its written?

James Abbatiello <[email protected]> Wed, 9 Mar 2011 01:03:20 -0500
Newsgroups gmane.comp.python.cheetah
Message-ID <[email protected]>
--===============2828669254914150960==
Content-Type: multipart/alternative; boundary=000e0cd32ff261d740049e067c87

--000e0cd32ff261d740049e067c87
Content-Type: text/plain; charset=ISO-8859-1

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
>
>

--000e0cd32ff261d740049e067c87
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

This is a bit ugly but it may work for you:<br><br>#!/usr/bin/env python<br=
>import Cheetah.Compiler<br>import Cheetah.Template<br><br>class MyAutoMeth=
odCompiler(Cheetah.Compiler.AutoMethodCompiler):<br>=A0=A0=A0 def addPlaceh=
older(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&#39;$LINE&#39;:<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(&#39;&#39;&#39;\<br>This is line $LINE<br=
>Hello, $name!<br>And this is line $LINE&#39;&#39;&#39;, compilerClass=3DMy=
Compiler)<br>
print tclass(searchList=3D[{&quot;name&quot;: &quot;Nate&quot;}])<br><br><b=
r>-- <br>James Abbatiello<br><br><br><div class=3D"gmail_quote">On Tue, Mar=
 8, 2011 at 1:45 PM, Nate Reid <span dir=3D"ltr">&lt;<a href=3D"mailto:gnat=
[email protected]">[email protected]</a>&gt;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex;">



<div>
I&#39;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&#39;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]">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>

--000e0cd32ff261d740049e067c87--


--===============2828669254914150960==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
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
--===============2828669254914150960==
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

--===============2828669254914150960==--