自分で作成した設定オプションを使う

設定ファイル内で自分で作った設定オプションを用意することができます。自分でセクションとオプションを追加するだけです。そうしたら、コード内でこれらの情報はグローバル変数configFileを通して可能になります。この変数はConfigParserオブジェクトです。 以下はこれの使い方の一例です:
**** File HelloServer.cfg ****
[server]
socketPort=80

[staticContent]
static=/home/remi/static

# Here I add my own configuration options
[user]
name=Remi
[database]
login=remiLogin
password=remiPassword


*** File Hello.cpy ****
CherryClass Root:
view:
    def index(self):
        <html><body>
            Hello, <py-eval="configFile.get('user','name')"><br>
            to connect to the database, you should use:<br>
            <py-eval="'Login:%s, Password:%s'%(configFile.get('database','login'), configFile.get('data
base','password'))">
        </body></html>
This will be rendered as:
<html><body>
    Hello, Remi<br>
    to connect to the database, you should use:<br>
    Login:remiLogin, Password:remiPassword
</body></html>

次の章では、CherryPyの特別な変数と、特別な関数について学ぶことにします。。。



Debian User 2003-10-13