Re: Using a variable inside another variable name

Oliver Nicholas <[email protected]> Mon, 19 Sep 2011 10:07:17 -0700
Newsgroups gmane.comp.python.cheetah
Message-ID <CAMo5GenH+1HPpKHzeMU97fYXbom+U9O6uHZ3iQnBOo8dXLwZrg@mail.gmail.com>
--===============7590685220975790815==
Content-Type: multipart/alternative; boundary=90e6ba6135b43a33eb04ad4e61f7

--90e6ba6135b43a33eb04ad4e61f7
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

$getVar("sso_host_%s" % $myLanguage)

On Mon, Sep 19, 2011 at 10:04 AM, R=E9mi ALVADO <[email protected]> wro=
te:

> Hi,
>
> I'm new with Cheetah and I'm trying to use it to build a deployment tool
> and replace my old configuration files with brand new cheetah templates.
> I'm working for a company which edit websites all over the world so lots =
of
> our configuration keys are overriden by country.
> With Cheetah, I'm storing my configuration into a YAML data storage files
> and I use a Python script combined with some Bash scripts to find, fill a=
nd
> replace all *.cheetah files.
> Please find below an example of my test configuration file, my YAML data
> storage and the expected result file
>
> --------------- CONF FILE -------------------
> prod:
>   sso:
>     hosts:
> #for myLanguage in $cultures
>       sso_$myLanguage:
>         host:   $eval("sso_host_" + myLanguage)
>         port:   $eval("sso_port_" + myLanguage)
> #end for
>
> --------------- YAML DATA STORAGE -------------------
> # Global
> cultures:
>   - de
>   - en
>   - fr
>
> # DE
> sso_host_de:         sso-de.example.com
> sso_port_de:         8081
> [...]
>
> # EN
> sso_host_en:         sso-en.example.com
> sso_port_en:         8082
> [...]
>
> # FR
> sso_host_fr:         sso-fr.example.com
> sso_port_fr:         8083
> [...]
>
> --------------- YAML DATA STORAGE -------------------
> prod:
>   sso:
>     hosts:
>       sso_de:
>         host:   sso-de.example.com
>         port:   8081
>       sso_en:
>         host:   sso-en.example.com
>         port:   8082
>       sso_fr:
>         host:   sso-fr.example.com
>         port:   8083
>
> -------------------------------------
>
> I know using "eval" is not a good idea (eval =3D evil :) ) but I've tried
> several other  solutions ("$sso_public_host_$myLanguage",
> "$(sso_public_host_$myLanguage)", ...) and none of them work.
> I've also tried to change my data storage file to have something like :
> de:
>    sso_host_public: ...
> but I've got the exact same issue with
> $self[$myLanguage]['sso_host_public'] and I don't like this representatio=
n
> since it's less natural than a list of <key,value> tuples.
>
> Do you have any idea how I can use a variable inside another variable nam=
e
> ? It seems to be a bit odd but it would be very convenient in a productio=
n
> environnement to have a very simple data storage file with only <key,valu=
e>
> tuples : easy to read and easy to share with a simple copy/paste into
> release note on a mail, a twiki, ...
>
> Thanks and congratulations this templating engine : despite this little
> issue, it's really great and I might consider using it on another project=
s,
> more web oriented :)
>
> R=E9mi
>
>
> -------------------------------------------------------------------------=
-----
> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
> Learn about the latest advances in developing for the
> BlackBerry&reg; mobile platform with sessions, labs & more.
> See new tools and technologies. Register for BlackBerry&reg; DevCon today=
!
> http://p.sf.net/sfu/rim-devcon-copy1
> _______________________________________________
> Cheetahtemplate-discuss mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
>
>


--=20
oliver phillip nicholas
*computer whisperer*
http://bigo.yelp.com/

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

$getVar(&quot;sso_host_%s&quot; % $myLanguage)<br><br><div class=3D"gmail_q=
uote">On Mon, Sep 19, 2011 at 10:04 AM, R=E9mi ALVADO <span dir=3D"ltr">&lt=
;<a href=3D"mailto:[email protected]">[email protected]</a>&gt;</sp=
an> wrote:<br>

<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex;">Hi,<div><br></div><div>I&#39;m new with Che=
etah and I&#39;m trying to use it to build a deployment tool and replace my=
 old configuration files with brand new cheetah templates.=A0</div>

<div>I&#39;m working for a company which edit websites all over the world s=
o lots of our configuration keys are overriden by country.</div>

<div>With Cheetah, I&#39;m storing my configuration into a YAML data storag=
e files and I use a Python script combined with some Bash scripts to find, =
fill and replace all *.cheetah files.=A0</div><div>Please find below an exa=
mple of my test configuration file, my YAML data storage and the expected r=
esult file</div>



<div><br></div><div><font face=3D"&#39;courier new&#39;, monospace">-------=
-------- CONF FILE -------------------</font></div><div><div><font face=3D"=
&#39;courier new&#39;, monospace">prod:</font></div>

<div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0sso:</font></div=
><div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0hosts:</fon=
t></div><div><font face=3D"&#39;courier new&#39;, monospace">#for myLanguag=
e in $cultures</font></div>



<div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0 =A0sso_$myL=
anguage:</font></div><div><font face=3D"&#39;courier new&#39;, monospace">=
=A0=A0 =A0 =A0 =A0host: =A0 $eval(&quot;sso_host_&quot; + myLanguage)</font=
></div>

<div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0 =A0 =A0</fo=
nt><span style=3D"font-family:&#39;courier new&#39;, monospace">port: =A0 $=
eval(&quot;sso_port_&quot; + myLanguage)</span></div>

<div><font face=3D"&#39;courier new&#39;, monospace">#end for</font></div><=
/div><div><font face=3D"&#39;courier new&#39;, monospace"><br></font></div>=
<div><font face=3D"&#39;courier new&#39;, monospace">--------------- YAML D=
ATA STORAGE -------------------</font></div>



<div><div><font face=3D"&#39;courier new&#39;, monospace"># Global=A0</font=
></div><div><font face=3D"&#39;courier new&#39;, monospace">cultures:</font=
></div><div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0- de</fon=
t></div>



<div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0- en</font></div=
><div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0- fr</font></di=
v><div><font face=3D"&#39;courier new&#39;, monospace"><br>

</font></div><div><font face=3D"&#39;courier new&#39;, monospace"># DE</fon=
t></div><div><font face=3D"&#39;courier new&#39;, monospace">sso_host_de: =
=A0 =A0 =A0 =A0 <a href=3D"http://sso-de.example.com" target=3D"_blank">sso=
-de.example.com</a></font></div>



</div><div><font face=3D"&#39;courier new&#39;, monospace">sso_port_de: =A0=
 =A0 =A0 =A0 8081</font></div><div><font face=3D"&#39;courier new&#39;, mon=
ospace">[...]</font></div><div><font face=3D"&#39;courier new&#39;, monospa=
ce"><br>

</font></div><div><div><font face=3D"&#39;courier new&#39;, monospace"># EN=
</font></div><div><font face=3D"&#39;courier new&#39;, monospace">sso_host_=
en: =A0 =A0 =A0 =A0 <a href=3D"http://sso-en.example.com" target=3D"_blank"=
>sso-en.example.com</a></font></div>



</div><div><font face=3D"&#39;courier new&#39;, monospace">sso_port_en: =A0=
 =A0 =A0 =A0 8082</font></div><div><font face=3D"&#39;courier new&#39;, mon=
ospace">[...]</font></div><div><font face=3D"&#39;courier new&#39;, monospa=
ce"><br>

</font></div><div><div><font face=3D"&#39;courier new&#39;, monospace"># FR=
</font></div><div><font face=3D"&#39;courier new&#39;, monospace">sso_host_=
fr: =A0 =A0 =A0 =A0 <a href=3D"http://sso-fr.example.com" target=3D"_blank"=
>sso-fr.example.com</a></font></div>



</div><div><font face=3D"&#39;courier new&#39;, monospace">sso_port_fr: =A0=
 =A0 =A0 =A0 8083</font></div><div><font face=3D"&#39;courier new&#39;, mon=
ospace">[...]</font></div><div><font face=3D"&#39;courier new&#39;, monospa=
ce"><br>

</font></div><div><font face=3D"&#39;courier new&#39;, monospace">---------=
------ YAML DATA STORAGE -------------------</font></div><div><font face=3D=
"&#39;courier new&#39;, monospace"><span style=3D"font-family:arial"><div>

<font face=3D"&#39;courier new&#39;, monospace">prod:</font></div><div><fon=
t face=3D"&#39;courier new&#39;, monospace">=A0=A0sso:</font></div><div><fo=
nt face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0hosts:</font></div>

<div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0 =A0sso_de:<=
/font></div><div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0=
 =A0 =A0host: =A0=A0</font><span style=3D"font-family:&#39;courier new&#39;=
, monospace"><a href=3D"http://sso-de.example.com" target=3D"_blank">sso-de=
.example.com</a></span></div>



<div><span style=3D"font-family:&#39;courier new&#39;, monospace">=A0=A0 =
=A0 =A0 =A0port: =A0 8081</span></div><div><span style=3D"font-family:&#39;=
courier new&#39;, monospace"><span style=3D"font-family:arial"><div>

<font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0 =A0sso_en:</font=
></div><div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0 =A0 =
=A0host: =A0=A0</font><span style=3D"font-family:&#39;courier new&#39;, mon=
ospace"><a href=3D"http://sso-en.example.com" target=3D"_blank">sso-en.exam=
ple.com</a></span></div>



<div><span style=3D"font-family:&#39;courier new&#39;, monospace">=A0=A0 =
=A0 =A0 =A0port: =A0 8082</span></div><div><span style=3D"font-family:&#39;=
courier new&#39;, monospace"><span style=3D"font-family:arial"><div>

<font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0 =A0sso_fr:</font=
></div><div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0 =A0 =A0 =
=A0host: =A0=A0</font><span style=3D"font-family:&#39;courier new&#39;, mon=
ospace"><a href=3D"http://sso-fr.example.com" target=3D"_blank">sso-fr.exam=
ple.com</a></span></div>



<div><span style=3D"font-family:&#39;courier new&#39;, monospace">=A0=A0 =
=A0 =A0 =A0port: =A0 8083</span></div><div><span style=3D"font-family:&#39;=
courier new&#39;, monospace"><br>

</span></div><div><span style=3D"font-family:&#39;courier new&#39;, monospa=
ce">-------------------------------------</span></div></span></span></div><=
/span></span></div></span></font></div><div>
<br>
</div><div>I know using &quot;eval&quot; is not a good idea (eval =3D evil =
:) ) but I&#39;ve tried several other =A0solutions (&quot;$sso_public_host_=
$myLanguage&quot;, &quot;$(sso_public_host_$myLanguage)&quot;, ...) and non=
e of them work.</div>



<div>I&#39;ve also tried to change my data storage file to have something l=
ike :</div><div><font face=3D"&#39;courier new&#39;, monospace">de:=A0</fon=
t></div><div><font face=3D"&#39;courier new&#39;, monospace">=A0=A0=A0sso_h=
ost_public: ...</font></div>



<div>but I&#39;ve got the exact same issue with <font face=3D"&#39;courier =
new&#39;, monospace">$self[$myLanguage][&#39;sso_host_public&#39;]</font> a=
nd I don&#39;t like this representation since it&#39;s less natural than a =
list of &lt;key,value&gt; tuples.</div>



<div><br></div><div>Do you have any idea how I can use a variable inside an=
other variable name ? It seems to be a bit odd but it would be very conveni=
ent in a production environnement to have a very simple data storage file w=
ith only &lt;key,value&gt; tuples : easy to read and easy to share with a s=
imple copy/paste into release note on a mail, a twiki, ...</div>



<div><br></div><div>Thanks and congratulations this templating engine : des=
pite this little issue, it&#39;s really great and I might consider using it=
 on another projects, more web oriented :)</div><div><br></div><font color=
=3D"#888888"><div>

R=E9mi</div>

</font><br>----------------------------------------------------------------=
--------------<br>
BlackBerry&amp;reg; DevCon Americas, Oct. 18-20, San Francisco, CA<br>
Learn about the latest advances in developing for the<br>
BlackBerry&amp;reg; mobile platform with sessions, labs &amp; more.<br>
See new tools and technologies. Register for BlackBerry&amp;reg; DevCon tod=
ay!<br>
<a href=3D"http://p.sf.net/sfu/rim-devcon-copy1" target=3D"_blank">http://p=
.sf.net/sfu/rim-devcon-copy1</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><br clear=3D"all"><div><br></div>-- <br>oliver p=
hillip nicholas<br><i>computer whisperer</i><br><a href=3D"http://bigo.yelp=
.com/" target=3D"_blank">http://bigo.yelp.com/</a><br><br>

--90e6ba6135b43a33eb04ad4e61f7--


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

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the 
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
--===============7590685220975790815==
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

--===============7590685220975790815==--