Я пытаюсь реализовать шаблонный файл конфигурации.
Я бы предпочел Python, но я бы тоже ответил на Perl.
Я использовал Perl для моего примера.
Я немного искал и нашел
- Python один файл конфигурации
- ConfigObj
- генератор конфигурационных файлов python
- 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
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Спасибо за ваши предложения.