Using a variable inside another variable name

RĂ©mi ALVADO <[email protected]> Mon, 19 Sep 2011 19:04:58 +0200
Newsgroups gmane.comp.python.cheetah
Message-ID <CA++mwdquCnn-yyJFzxva-aHKPRBnaEaymFKdneGUJqELT-TsXw@mail.gmail.com>
--===============7087458474317223140==
Content-Type: multipart/alternative; boundary=bcaec5196973f0c52804ad4e58e4

--bcaec5196973f0c52804ad4e58e4
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi,

I'm new with Cheetah and I'm trying to use it to build a deployment tool an=
d
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 and
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
representation 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 name =
?
It seems to be a bit odd but it would be very convenient in a production
environnement to have a very simple data storage file with only <key,value>
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 projects,
more web oriented :)

R=C3=A9mi

--bcaec5196973f0c52804ad4e58e4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi,<div><br></div><div>I&#39;m new with Cheetah and I&#39;m trying to use i=
t to build a deployment tool and replace my old configuration files with br=
and new cheetah templates.=C2=A0</div><div>I&#39;m working for a company wh=
ich edit websites all over the world so 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.=C2=A0</div><div>Please find below an =
example of my test configuration file, my YAML data storage and the expecte=
d result file</div>

<div><br></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier n=
ew&#39;, monospace">--------------- CONF FILE -------------------</font></d=
iv><div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;=
, monospace">prod:</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=C2=A0=C2=A0sso:</font></div><div><font class=3D"Apple-style-span" fac=
e=3D"&#39;courier new&#39;, monospace">=C2=A0=C2=A0 =C2=A0hosts:</font></di=
v><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mono=
space">#for myLanguage in $cultures</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=C2=A0=C2=A0 =C2=A0 =C2=A0sso_$myLanguage:</font></div><div><font clas=
s=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=C2=A0=C2=
=A0 =C2=A0 =C2=A0 =C2=A0host: =C2=A0 $eval(&quot;sso_host_&quot; + myLangua=
ge)</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0</font><span class=3D"Apple-style-spa=
n" style=3D"font-family: &#39;courier new&#39;, monospace; ">port: =C2=A0 $=
eval(&quot;sso_port_&quot; + myLanguage)</span></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">#end for</font></div></div><div><font class=3D"Apple-style-span" face=
=3D"&#39;courier new&#39;, monospace"><br></font></div><div><font class=3D"=
Apple-style-span" face=3D"&#39;courier new&#39;, monospace">---------------=
 YAML DATA STORAGE -------------------</font></div>

<div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m=
onospace"># Global=C2=A0</font></div><div><font class=3D"Apple-style-span" =
face=3D"&#39;courier new&#39;, monospace">cultures:</font></div><div><font =
class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=C2=A0=
=C2=A0- de</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=C2=A0=C2=A0- en</font></div><div><font class=3D"Apple-style-span" fac=
e=3D"&#39;courier new&#39;, monospace">=C2=A0=C2=A0- fr</font></div><div><f=
ont class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace"><b=
r>

</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new=
&#39;, monospace"># DE</font></div><div><font class=3D"Apple-style-span" fa=
ce=3D"&#39;courier new&#39;, monospace">sso_host_de: =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 <a href=3D"http://sso-de.example.com">sso-de.example.com</a></font><=
/div>

</div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, =
monospace">sso_port_de: =C2=A0 =C2=A0 =C2=A0 =C2=A0 8081</font></div><div><=
font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">[=
...]</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier=
 new&#39;, monospace"><br>

</font></div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courie=
r new&#39;, monospace"># EN</font></div><div><font class=3D"Apple-style-spa=
n" face=3D"&#39;courier new&#39;, monospace">sso_host_en: =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 <a href=3D"http://sso-en.example.com">sso-en.example.com</a></fo=
nt></div>

</div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, =
monospace">sso_port_en: =C2=A0 =C2=A0 =C2=A0 =C2=A0 8082</font></div><div><=
font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">[=
...]</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier=
 new&#39;, monospace"><br>

</font></div><div><div><font class=3D"Apple-style-span" face=3D"&#39;courie=
r new&#39;, monospace"># FR</font></div><div><font class=3D"Apple-style-spa=
n" face=3D"&#39;courier new&#39;, monospace">sso_host_fr: =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 <a href=3D"http://sso-fr.example.com">sso-fr.example.com</a></fo=
nt></div>

</div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, =
monospace">sso_port_fr: =C2=A0 =C2=A0 =C2=A0 =C2=A0 8083</font></div><div><=
font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">[=
...]</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier=
 new&#39;, monospace"><br>

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

<font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=
prod:</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courie=
r new&#39;, monospace">=C2=A0=C2=A0sso:</font></div><div><font class=3D"App=
le-style-span" face=3D"&#39;courier new&#39;, monospace">=C2=A0=C2=A0 =C2=
=A0hosts:</font></div>

<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=C2=A0=C2=A0 =C2=A0 =C2=A0sso_de:</font></div><div><font class=3D"Appl=
e-style-span" face=3D"&#39;courier new&#39;, monospace">=C2=A0=C2=A0 =C2=A0=
 =C2=A0 =C2=A0host: =C2=A0=C2=A0</font><span class=3D"Apple-style-span" sty=
le=3D"font-family: &#39;courier new&#39;, monospace; "><a href=3D"http://ss=
o-de.example.com">sso-de.example.com</a></span></div>

<div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne=
w&#39;, monospace; ">=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0port: =C2=A0 8081</sp=
an></div><div><span class=3D"Apple-style-span" style=3D"font-family: &#39;c=
ourier new&#39;, monospace; "><span class=3D"Apple-style-span" style=3D"fon=
t-family: arial; "><div>

<font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=
=C2=A0=C2=A0 =C2=A0 =C2=A0sso_en:</font></div><div><font class=3D"Apple-sty=
le-span" face=3D"&#39;courier new&#39;, monospace">=C2=A0=C2=A0 =C2=A0 =C2=
=A0 =C2=A0host: =C2=A0=C2=A0</font><span class=3D"Apple-style-span" style=
=3D"font-family: &#39;courier new&#39;, monospace; "><a href=3D"http://sso-=
en.example.com">sso-en.example.com</a></span></div>

<div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne=
w&#39;, monospace; ">=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0port: =C2=A0 8082</sp=
an></div><div><span class=3D"Apple-style-span" style=3D"font-family: &#39;c=
ourier new&#39;, monospace; "><span class=3D"Apple-style-span" style=3D"fon=
t-family: arial; "><div>

<font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=
=C2=A0=C2=A0 =C2=A0 =C2=A0sso_fr:</font></div><div><font class=3D"Apple-sty=
le-span" face=3D"&#39;courier new&#39;, monospace">=C2=A0=C2=A0 =C2=A0 =C2=
=A0 =C2=A0host: =C2=A0=C2=A0</font><span class=3D"Apple-style-span" style=
=3D"font-family: &#39;courier new&#39;, monospace; "><a href=3D"http://sso-=
fr.example.com">sso-fr.example.com</a></span></div>

<div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne=
w&#39;, monospace; ">=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0port: =C2=A0 8083</sp=
an></div><div><span class=3D"Apple-style-span" style=3D"font-family: &#39;c=
ourier new&#39;, monospace; "><br>

</span></div><div><span class=3D"Apple-style-span" style=3D"font-family: &#=
39;courier new&#39;, monospace; ">-------------------------------------</sp=
an></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 =C2=A0solutions (&quot;$sso_public_ho=
st_$myLanguage&quot;, &quot;$(sso_public_host_$myLanguage)&quot;, ...) and =
none of them work.</div>

<div>I&#39;ve also tried to change my data storage file to have something l=
ike :</div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#=
39;, monospace">de:=C2=A0</font></div><div><font class=3D"Apple-style-span"=
 face=3D"&#39;courier new&#39;, monospace">=C2=A0=C2=A0=C2=A0sso_host_publi=
c: ...</font></div>

<div>but I&#39;ve got the exact same issue with <font class=3D"Apple-style-=
span" face=3D"&#39;courier new&#39;, monospace">$self[$myLanguage][&#39;sso=
_host_public&#39;]</font> and 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><div>R=C3=A9=
mi</div>


--bcaec5196973f0c52804ad4e58e4--


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

--===============7087458474317223140==--