473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Parsing environment variables in ConfigParser files

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
Jul 18 '05 #1
6 6754
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
da***@dalkescientific.com
Jul 18 '05 #2

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

Jul 18 '05 #3
Matthew Barnes wrote:
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:
from ConfigParser import ConfigParser
class ExpandingParser(ConfigParser): .... def getexpanded(self, section, option):
.... import os
.... return self._get(section, os.path.expandvars, option)
.... p = ExpandingParser()
p.read("expand.ini")
p.get("paths", "mydir") '$HOME/of/the/brave' p.getexpanded("paths", "mydir") '/home/peter/of/the/brave'


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


Jul 18 '05 #4
Matthew Barnes <ma*****@barnes.net> pisze:
[section_name]
data_file=${HOME}/mydata.dat


Curly braces are so... php'ish... No, I don't like this idea.

--
Jarek Zgoda
Unregistered Linux User # -1
http://www.zgoda.biz/ JID:zg***@chrome.pl http://zgoda.jogger.pl/
Jul 18 '05 #5
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" <ma*****@barnes.net>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Friday, December 19, 2003 7:36 PM
Subject: Parsing environment variables in ConfigParser files

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
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #6

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

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Greg Krohn | last post by:
I'm trying to subclass ConfigParser so I can use a custom __read method (the custom format doesn't conform to RFC 822) when needed. Needless to say, it's not working as expected. In the...
4
by: Marian Jancar | last post by:
Hi, Is there a module for parsing spec files available? Marian -- -- Best Regards,
11
by: Manlio Perillo | last post by:
Regards. Since sections in CongiParser files are delimited by , why there is not an escape (and unescape) function for escaping &, characters to &amp;, &lbrack; and &rbrack; ? Thanks ...
2
by: rzed | last post by:
I am working with PythonCard in one of my apps. For its purposes, it uses an .ini file that is passed to ConfigParser. For my app, I also need configuration information, but for various reasons,...
10
by: Terry Carroll | last post by:
It looks like ConfigParser will accept a list to be writing to the *.ini file; but when reading it back in, it treats it as a string. Example: ############################### import...
4
by: Danil Dotsenko | last post by:
Wrote a little "user-friedly" wrapper for ConfigParser for a KDE's SuperKaramba widget. (http://www.kde-look.org/content/show.php?content=32185) I was using 2.4.x python docs as reference and...
3
by: Tristan | last post by:
Hello community: I post this because I could not find satisfactory answers in the posts generated by this nice group. I work on winXP. I have many little python applications in different folders,...
1
by: Tommy Grav | last post by:
Hi, I have a setup file for some numerical simulation code written in C that looks like this: dt 0.1 time 0.0 nupdate 10 noutput 100 ntotal 10000
3
by: smitty1e | last post by:
Just a fun exercise to unify some of the major input methods for a script into a single dictionary. Here is the output, given a gr.conf file in the same directory with the contents stated below: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.