Parsing environment variables in ConfigParser files

M

Matthew Barnes

I'm considering submitting a patch for Python 2.4 to allow environment
variable expansion in ConfigParser files. The use cases for this
should be obvious. I'd like to be able to specify something like the
following in a configuration file:

[section_name]
data_file=${HOME}/mydata.dat

....(where HOME=/home/matt) and have ConfigParser automatically expand
it to:

[section_name]
data_file=/home/matt/mydata.dat

The change is pretty straight-forward, but I'm interested in feedback
on whether this is a good idea, what the syntax for environment
variable references should look like (currently I'm thinking
${varname}), or whether there are any hidden complexities or
backward-compatibility concerns.

Matthew Barnes
 
A

Andrew Dalke

Matthew Barnes:
I'm considering submitting a patch for Python 2.4 to allow environment
variable expansion in ConfigParser files. [...] I'd like to be
able to specify something like the following in a configuration file:

[section_name]
data_file=${HOME}/mydata.dat

Personally, I'm against it. The less magic the better. Eg,
what if I have a configuration file which sets the default
configurations, so that I want the actual string
${HOME}/mydata.dat
in the file. What's the escape rule for that.

Whereas as it is now I can get the string value then use
os.path.expandvars (or os.path.expanduser if I want ~home
expansion). It's an extra couple lines of code, granted,
but it's explicit instead of implicit and less likely to introduce
subtle errors.

Andrew
(e-mail address removed)
 
S

Skip Montanaro

Matthew> I'm considering submitting a patch for Python 2.4 to allow
Matthew> environment variable expansion in ConfigParser files.

data_file=${HOME}/mydata.dat -> data_file=/home/matt/mydata.dat

Matthew> The change is pretty straight-forward, but I'm interested in
Matthew> feedback on whether this is a good idea, what the syntax for
Matthew> environment variable references should look like (currently I'm
Matthew> thinking ${varname}), or whether there are any hidden
Matthew> complexities or backward-compatibility concerns.

There is already a %(foo)s expansion capability in ConfigParser, though the
source of key/value pairs is different. I'd check to see if it can be bent
(twisted?) to your needs by subclassing ConfigParser and merging a suitably
munged os.environ with the user's config file. Using your example, the
DEFAULT info would be synthesized or augmented to look like (logically
speaking, this might simply be modified to parse os.environ before reading
the config file):

[DEFAULT]
# stuff imported from os.environ
HOME=/home/matt
USER=matt
...
# user's other DEFAULT settings
...

and a simple regular expression replace would be performed on the actual
config file roughly like so:

configf = file("config.ini")
configdata = re.sub(r'${([^}]+)}', r'%(\1)s', configf.read())
configf = StringIO.StringIO(configdata)
configf.seek(0)

Skip
 
B

Bogdan Marinescu

Good idea, but since you want to do this, why stop here? I would really
like to have the possibility to expand some previous defined variables in
the file:

[sect1]
var1 = ...
..............
expansion=${var1}

Of course this should merely complete the environment variable
replacement feature.

----- Original Message -----
From: "Matthew Barnes" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Friday, December 19, 2003 7:36 PM
Subject: Parsing environment variables in ConfigParser files
 
P

Peter Otten

Matthew said:
I'm considering submitting a patch for Python 2.4 to allow environment
variable expansion in ConfigParser files. The use cases for this
should be obvious. I'd like to be able to specify something like the
following in a configuration file:

[section_name]
data_file=${HOME}/mydata.dat

...(where HOME=/home/matt) and have ConfigParser automatically expand
it to:

[section_name]
data_file=/home/matt/mydata.dat

The change is pretty straight-forward, but I'm interested in feedback
on whether this is a good idea, what the syntax for environment
variable references should look like (currently I'm thinking
${varname}), or whether there are any hidden complexities or
backward-compatibility concerns.

Matthew Barnes

I think this can be easily achieved by subclassing the ConfigParser:
.... def getexpanded(self, section, option):
.... import os
.... return self._get(section, os.path.expandvars, option)
....
If you will bother with the library implementation at all, I would prefer
this level of explicitness, i. e. a dedicated getexpanded() method in
ConfigParser, or alternatively a subclass that overrides _get() to always
expand in the getXXX() methods. With both approaches you can stay safely
away from backwards compatibility problems.

Peter
 
S

Skip Montanaro

Bogdan> Good idea, but since you want to do this, why stop here? I would
Bogdan> really like to have the possibility to expand some previous
Bogdan> defined variables in the file:

...

ConfigParser already does this.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top