【发布时间】:2011-10-14 17:27:55
【问题描述】:
我正在尝试实现模板化配置文件。 我更喜欢 python,但我也会在 perl 中回答。 我使用 perl 作为示例。
我搜索了一下,发现 - python single configuration file - ConfigObj - python configuration file generator - ePerl 但我不能从那些解决我的问题。
我正在尝试生成一个主要采用 INI 格式的配置文件(甚至没有部分):
# Comments
VAR1 = value1
EDITOR = vi
我需要从模板中生成的,我在文本中嵌入了脚本语言:
# Config:
MYPWD = <: `pwd` :>
'<:>' 之间的文本将使用脚本语言(python 或 perl)。与模板一样,它的标准输出被捕获并插入到结果文本中。示例中使用的模板基本上是 eperl,但如果可用,我更喜欢 python。
最后,定义的变量应该是可重用的:
# Config:
CODE_HOME = /some/path
CODE_BIN = <:=$CODE_HOME:>/bin
这是我读到的测试源文件:
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# platform.cfg
# This one variable
VAR =value
# this is a templated variable. The langage is perl, but could be python.
HELLO= <: print 'World' :>
# This is a multi-line code which should resolve to a single line value.
LONGER = <:
if (1) {
print "abc ";
}
$pwd = `/bin/pwd`;
chomp($pwd);
print $pwd;
:>
# Another one to test the carriage returns.
MULTIPLE = /<: print "proj" :>/<: print "tahiti":>/<:
print "pd/1/";
$system = `grep -w VAR platform.cfg | egrep -v 'print|platform.cfg' | cut -d = -f 2-`;
chomp($system);
print $system;
:>
# variables dependent from the previous variable definition
VAR1 = <: print $VAR :>1
# variables dependent from the previous variable definition
VAR2 = <: print $VAR1 :>2
# variables dependent from the previous variable definition
VAR3 = <: print $VAR2 :>3
# variables dependent from the previous variable definition
VAR4 = <: print $VAR3 :>4
# BTW, multi-line comments are significant
# and should be preserved as the documentation for the
# variable just below:
VAR5 = <: print $VAR4 :>5
VAR6 = <: print $VAR5 :>6
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
我希望从脚本中得到这个结果。 我不知道如何让配置文件中定义的变量成为解释器的一部分?
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# platform.cfg
# This one variable
VAR =value
# this is a templated variable. The langage is perl, but could be python.
HELLO= World
# This is a multi-line code which should resolve to a single line value.
LONGER = abc /src/byop/CODE
# Another one to test the carriage returns.
MULTIPLE = /proj/tahiti/pd/1/value
# variables dependent from the previous variable definition
VAR1 = value1
# variables dependent from the previous variable definition
VAR2 = value12
# variables dependent from the previous variable definition
VAR3 = value123
# variables dependent from the previous variable definition
VAR4 = value1234
# BTW, multi-line comments are significant
# and should be preserved as the documentation for the
# variable just below:
VAR5 = value12345
VAR6 = value123456
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
感谢您的建议。
【问题讨论】:
-
这似乎是一种非常具体和特殊的模板方言。你知道这应该是什么格式吗?
-
@TokenMacGuy:感谢您的评论。我已经编辑了我的问题,以更好地描述我合并了一个配置和一个模板。所以试着回答你的问题:传入文件的格式是一个带括号的配置文件:'<:>'。这些分隔符之间的任何内容都在脚本语言中(python 或 perl,选择一个),并增加了先前在模板中定义的变量应该可以被脚本语言访问的约束。 (这样更清楚吗?)
-
所以这是你当场编造的模板语言?
-
另外,
chomp()应该做什么?
标签: python perl templates configuration