Re: Does Cheetah count line numbers its written?
Nate Reid <[email protected]> Fri, 11 Mar 2011 18:29:47 -0800
| Newsgroups | gmane.comp.python.cheetah |
|---|---|
| Message-ID | <[email protected]> |
--===============8719156753305433306==
Content-Type: multipart/alternative;
boundary="_a71b9fae-b255-4da4-a32f-4b6414d49530_"
--_a71b9fae-b255-4da4-a32f-4b6414d49530_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi James=2C
In my work=2C I am using the command line cheetah compiler (e.g. cheetah sc=
ript) so I had to hack a version of the command-line CheetahWrapper by monk=
ey patching in a version that lets me specify a compilerClass to the Templa=
te.compile command. My changes are specifying the compilerClass in the com=
pille command below.
#!/bin/env python
import Cheetah.CheetahWrapper
from Cheetah.CheetahWrapper import CheetahWrapper
from cheetah_compilers import LineNumberCompiler
import os
import shutil
moduleNameRE =3D Cheetah.CheetahWrapper.moduleNameRE
class MyCheetahWrapper(CheetahWrapper):
def __init__(self=2C *args=2C **kwargs):
super(MyCheetahWrapper=2C self).__init__(*args=2C **kwargs)
def _compileOrFillBundle(self=2C b):
C=2C D=2C W =3D self.chatter=2C self.debug=2C self.warn
TemplateClass =3D self._getTemplateClass()
compilerSettings =3D self._getCompilerSettings()
src =3D b.src
dst =3D b.dst
base =3D b.base
basename =3D b.basename
dstDir =3D os.path.dirname(dst)
what =3D self.isCompile and "Compiling" or "Filling"
C("%s %s -> %s^"=2C what=2C src=2C dst) # No trailing newline.
if os.path.exists(dst) and not self.opts.nobackup:
bak =3D b.bak
C(" (backup %s)"=2C bak) # On same line as previous message.
else:
bak =3D None
C("")
if self.isCompile:
if not moduleNameRE.match(basename):
tup =3D basename=2C src
raise Error("""\
%s: base name %s contains invalid characters. It must
be named according to the same rules as Python modules.""" % tup)
pysrc =3D TemplateClass.compile(file=3Dsrc=2C returnAClass=3DFa=
lse=2C
moduleName=3Dbasename=2C
className=3Dbasename=2C
commandlineopts=3Dself.opts=2C
compilerSettings=3DcompilerSettin=
gs=2C
compilerClass=3DLineNumberCompile=
r)
output =3D pysrc
else:
#output =3D str(TemplateClass(file=3Dsrc=2C searchList=3Dself.s=
earchList))
tclass =3D TemplateClass.compile(file=3Dsrc=2C compilerSettings=
=3DcompilerSettings=2C compilerClass=3DLineNumberCompiler)
output =3D str(tclass(searchList=3Dself.searchList))
if bak:
shutil.copyfile(dst=2C 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 =3D open(dst=2C 'w')
f.write(output)
f.close()
if __name__ =3D=3D '__main__':
MyCheetahWrapper().main()
Where LineNumberCompiler is the MyCompiler class you created in the origina=
l e-mail.
The problem is that when the templates are compiled=2C it seems to hardcode=
the $LINE variable directly into the source code=2C so when I actually fil=
l the templates=2C
the $LINE variable no longer corresponds to the actual line in the source c=
ode. 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 replac=
es $LINE with the line number that it was first defined=2C 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=2C 9 Mar 2011 11:23:04 -0800
CC: [email protected]
Subject: Re: [Cheetahtemplate-discuss] Does Cheetah count line numbers its =
written?
Thanks James=2C I'll give this a try and thanks for taking the time to writ=
e this up.
Cheers
-Nate
Date: Wed=2C 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=2C expr=2C filterArgs=2C rawPlaceholder=2C
cacheTokenParts=2C lineCol=2C
silentMode=3DFalse):
if rawPlaceholder =3D=3D u'$LINE':
self.addFilteredChunk(repr(unicode(lineCol[0]))=2C
filterArgs=2C
rawPlaceholder=2C
lineCol=3DlineCol)
else:
super(MyAutoMethodCompiler=2C self).addPlaceholder(
expr=2C filterArgs=2C rawPlaceholder=2C
cacheTokenParts=2C lineCol=2C
silentMode=3DsilentMode)
class MyAutoClassCompiler(Cheetah.Compiler.AutoClassCompiler):
methodCompilerClass =3D MyAutoMethodCompiler
class MyCompiler(Cheetah.Compiler.Compiler):
classCompilerClass =3D MyAutoClassCompiler
tclass =3D Cheetah.Template.Template.compile('''\
This is line $LINE
Hello=2C $name!
And this is line $LINE'''=2C compilerClass=3DMyCompiler)
print tclass(searchList=3D[{"name": "Nate"}])
--=20
James Abbatiello
On Tue=2C Mar 8=2C 2011 at 1:45 PM=2C Nate Reid <[email protected]> wrote=
:
I'm writing a source generator using Cheetah templates for C++ and would li=
ke to add #line directives into the generated source. Without delving deep=
into the Cheetah source code=2C 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 outpu=
t itself for inserting #line directives into the generated source code.
Thanks!
-Nate
=20
---------------------------------------------------------------------------=
---
What You Don't Know About Data Connectivity CAN Hurt You
This paper provides an overview of data connectivity=2C details
its effect on application quality=2C 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
=20
---------------------------------------------------------------------------=
---
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 =
=
--_a71b9fae-b255-4da4-a32f-4b6414d49530_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<style><!--
.hmmessage P
{
margin:0px=3B
padding:0px
}
body.hmmessage
{
font-size: 10pt=3B
font-family:Tahoma
}
--></style>
</head>
<body class=3D'hmmessage'>
Hi James=2C<br>In my work=2C I am using the command line cheetah compiler (=
e.g. cheetah script) so I had to hack a version of the command-line Cheetah=
Wrapper by monkey patching in a version that lets me specify a compilerClas=
s to the Template.compile command. =3B My changes are specifying the co=
mpilerClass in the compille command below.<br><br>#!/bin/env python<br>impo=
rt Cheetah.CheetahWrapper<br>from Cheetah.CheetahWrapper import CheetahWrap=
per<br>from cheetah_compilers import LineNumberCompiler<br>import os<br>imp=
ort shutil<br>moduleNameRE =3D Cheetah.CheetahWrapper.moduleNameRE<br><br>c=
lass MyCheetahWrapper(CheetahWrapper):<br> =3B =3B =3B def __in=
it__(self=2C *args=2C **kwargs):<br> =3B =3B =3B =3B =
=3B =3B =3B super(MyCheetahWrapper=2C self).__init__(*args=2C **kwa=
rgs)<br><br> =3B =3B =3B def _compileOrFillBundle(self=2C b):<b=
r> =3B =3B =3B =3B =3B =3B =3B C=2C D=2C W =3D =
self.chatter=2C self.debug=2C self.warn<br> =3B =3B =3B =3B=
 =3B =3B =3B TemplateClass =3D self._getTemplateClass()<br>&nbs=
p=3B =3B =3B =3B =3B =3B =3B compilerSettings =3D s=
elf._getCompilerSettings()<br> =3B =3B =3B =3B =3B =
=3B =3B src =3D b.src<br> =3B =3B =3B =3B =3B =
=3B =3B dst =3D b.dst<br> =3B =3B =3B =3B =3B =
=3B =3B base =3D b.base<br> =3B =3B =3B =3B =3B&nbs=
p=3B =3B basename =3D b.basename<br> =3B =3B =3B =3B&nb=
sp=3B =3B =3B dstDir =3D os.path.dirname(dst)<br> =3B =3B&n=
bsp=3B =3B =3B =3B =3B what =3D self.isCompile and "Compili=
ng" or "Filling"<br> =3B =3B =3B =3B =3B =3B =
=3B C("%s %s ->=3B %s^"=2C what=2C src=2C dst) # No trailing newline.<br>=
 =3B =3B =3B =3B =3B =3B =3B if os.path.exists(=
dst) and not self.opts.nobackup:<br> =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B bak =3D b.bak<br> =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B C(" (backup %s)"=2C bak) # On same line as previous message.<br>&n=
bsp=3B =3B =3B =3B =3B =3B =3B else:<br> =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B bak =3D None<br> =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B C("")<br> =3B =3B =3B =
=3B =3B =3B =3B if self.isCompile:<br> =3B =3B =3B&=
nbsp=3B =3B =3B =3B =3B =3B =3B =3B if not modu=
leNameRE.match(basename):<br> =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
tup =3D basename=2C src<br> =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
raise Error("""\<br>%s: base name %s contains invalid characters. =3B =
It must<br>be named according to the same rules as Python modules.""" % tup=
)<br> =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B pysrc =3D TemplateClass.compile(file=3Dsrc=2C returnACl=
ass=3DFalse=2C<br> =3B =3B =3B =3B =3B =3B =3B&=
nbsp=3B =3B =3B =3B =3B =3B =3B =3B =3B&nbs=
p=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B moduleName=3Dbasename=2C<b=
r> =3B =3B =3B =3B =3B =3B =3B =3B =3B&=
nbsp=3B =3B =3B =3B =3B =3B =3B =3B =3B&nbs=
p=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B className=3Dbasename=2C<br> =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B commandlineopts=3Dself.opts=2C<br> =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B compilerSettings=3DcompilerSettings=2C<br> =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B compilerClass=3DLineNumberCompiler)<br> =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B output =3D pysrc<b=
r> =3B =3B =3B =3B =3B =3B =3B else:<br> =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B #output =3D str(TemplateClass(file=3Dsrc=2C searchList=3Dself.sear=
chList))<br> =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B tclass =3D TemplateClass.compile(file=3Dsrc=2C =
compilerSettings=3DcompilerSettings=2C compilerClass=3DLineNumberCompiler)<=
br> =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B output =3D str(tclass(searchList=3Dself.searchList))<br><b=
r> =3B =3B =3B =3B =3B =3B =3B if bak:<br> =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B shutil.copyfile(dst=2C bak)<br> =3B =3B =3B =3B&nb=
sp=3B =3B =3B if dstDir and not os.path.exists(dstDir):<br> =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B if self.isCompile:<br> =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
mkdirsWithPyInitFiles(dstDir)<br> =3B =3B =3B =3B =3B&=
nbsp=3B =3B =3B =3B =3B =3B else:<br> =3B =3B&n=
bsp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B os.makedirs(dstDir)<br> =3B =3B =3B=
 =3B =3B =3B =3B if self.opts.stdout:<br> =3B =3B&n=
bsp=3B =3B =3B =3B =3B =3B =3B =3B =3B sys.=
stdout.write(output)<br> =3B =3B =3B =3B =3B =3B&nb=
sp=3B else:<br> =3B =3B =3B =3B =3B =3B =3B&nbs=
p=3B =3B =3B =3B f =3D open(dst=2C 'w')<br> =3B =3B&nbs=
p=3B =3B =3B =3B =3B =3B =3B =3B =3B f.writ=
e(output)<br> =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B f.close()<br><br>if __name__ =3D=3D '__main__':=
<br> =3B =3B =3B MyCheetahWrapper().main()<br><br><br>Where Lin=
eNumberCompiler is the MyCompiler class you created in the original e-mail.=
<br>The problem is that when the templates are compiled=2C it seems to hard=
code the $LINE variable directly into the source code=2C so when I actually=
fill the templates=2C<br>the $LINE variable no longer corresponds to the a=
ctual line in the source code. =3B It also doesn't allow me to use a #d=
ef to define a function that lets me spit out the #line $LINE "filename.cpp=
" directive. =3B E.g. it just replaces $LINE with the line number that =
it was first defined=2C rather than the dynamic one.<br><br>Is there anothe=
r way that Cheetah can actually count the lines it's written while filling =
the template?<br><br>Thanks!<br>-Nate<br><br><hr id=3D"stopSpelling">From: =
[email protected]<br>To: [email protected]<br>Date: Wed=2C 9 Mar 2011 11:2=
3:04 -0800<br>CC: [email protected]<br>Subject:=
Re: [Cheetahtemplate-discuss] Does Cheetah count line numbers its written?=
<br><br>
<meta http-equiv=3D"Content-Type" content=3D"text/html=3B charset=3Dunicode=
">
<meta name=3D"Generator" content=3D"Microsoft SafeHTML">
<style>
.ExternalClass .ecxhmmessage P
{padding:0px=3B}
.ExternalClass body.ecxhmmessage
{font-size:10pt=3Bfont-family:Tahoma=3B}
</style>
Thanks James=2C I'll give this a try and thanks for taking the time to writ=
e this up.<br>Cheers<br>-Nate<br><br><hr id=3D"ecxstopSpelling">Date: Wed=
=2C 9 Mar 2011 01:03:20 -0500<br>Subject: Re: [Cheetahtemplate-discuss] Doe=
s Cheetah count line numbers its written?<br>From: [email protected]<br>To: =
[email protected]<br>CC: [email protected]<br=
><br>This is a bit ugly but it may work for you:<br><br>#!/usr/bin/env pyth=
on<br>import Cheetah.Compiler<br>import Cheetah.Template<br><br>class MyAut=
oMethodCompiler(Cheetah.Compiler.AutoMethodCompiler):<br> =3B =3B&n=
bsp=3B def addPlaceholder(self=2C expr=2C filterArgs=2C rawPlaceholder=2C<b=
r>
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B cacheTokenParts=2C lineCol=2C<br> =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B silentMode=3DFalse):<br> =3B =3B =3B =3B =
=3B =3B =3B if rawPlaceholder =3D=3D u'$LINE':<br> =3B =3B&=
nbsp=3B =3B =3B =3B =3B =3B =3B =3B =3B sel=
f.addFilteredChunk(repr(unicode(lineCol[0]))=2C<br> =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B filterArgs=2C<br>
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B rawPlaceholder=2C<br> =3B =
=3B =3B =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B lineCol=3DlineCol)<br> =3B =3B =3B&=
nbsp=3B =3B =3B =3B else:<br> =3B =3B =3B =3B&n=
bsp=3B =3B =3B =3B =3B =3B =3B super(MyAutoMethodCo=
mpiler=2C self).addPlaceholder(<br> =3B =3B =3B =3B =3B=
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B expr=2C filterArgs=2C rawPlaceholder=2C<br>
 =3B =3B =3B =3B =3B =3B =3B =3B =3B&nb=
sp=3B =3B =3B =3B =3B =3B cacheTokenParts=2C lineCol=2C=
<br> =3B =3B =3B =3B =3B =3B =3B =3B =
=3B =3B =3B =3B =3B =3B =3B silentMode=3DsilentMode=
)<br><br><br>class MyAutoClassCompiler(Cheetah.Compiler.AutoClassCompiler):=
<br> =3B =3B =3B methodCompilerClass =3D MyAutoMethodCompiler<b=
r><br><br>class MyCompiler(Cheetah.Compiler.Compiler):<br>
 =3B =3B =3B classCompilerClass =3D MyAutoClassCompiler<br><br>=
<br>tclass =3D Cheetah.Template.Template.compile('''\<br>This is line $LINE=
<br>Hello=2C $name!<br>And this is line $LINE'''=2C compilerClass=3DMyCompi=
ler)<br>
print tclass(searchList=3D[{"name": "Nate"}])<br><br><br>-- <br>James Abbat=
iello<br><br><br><div class=3D"ecxgmail_quote">On Tue=2C Mar 8=2C 2011 at 1=
:45 PM=2C Nate Reid <span dir=3D"ltr"><=3B<a href=3D"mailto:gnatty7@hotma=
il.com">[email protected]</a>>=3B</span> wrote:<br>
<blockquote class=3D"ecxgmail_quote" style=3D"padding-left: 1ex=3B">
<div>
I'm writing a source generator using Cheetah templates for C++ and would li=
ke to add #line directives into the generated source. =3B Without delvi=
ng deep into the Cheetah source code=2C I was wondering if anyone on the li=
st knew if Cheetah keeps track of how many lines are written so I can easil=
y query the current line number in the template and use that in the templat=
e output itself for inserting #line directives into the generated source co=
de.<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=2C details<br>
its effect on application quality=2C 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> =20
<br>-----------------------------------------------------------------------=
-------
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<br>_____________________________________=
__________
Cheetahtemplate-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss =
</body>
</html>=
--_a71b9fae-b255-4da4-a32f-4b6414d49530_--
--===============8719156753305433306==
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
--===============8719156753305433306==
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
--===============8719156753305433306==--