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=${HOM E}/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 6 6783
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=${HOM E}/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.expandv ars (or os.path.expandu ser 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***@dalkescie ntific.com
Matthew> I'm considering submitting a patch for Python 2.4 to allow
Matthew> environment variable expansion in ConfigParser files.
data_file=${HOM E}/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.in i")
configdata = re.sub(r'${([^}]+)}', r'%(\1)s', configf.read())
configf = StringIO.String IO(configdata)
configf.seek(0)
Skip
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=${HOM E}/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(sel f, section, option):
.... import os
.... return self._get(secti on, os.path.expandv ars, 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
Matthew Barnes <ma*****@barnes .net> pisze: [section_name] data_file=${HOM E}/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***@chrom e.pl http://zgoda.jogger.pl/
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=${var 1}
Of course this should merely complete the environment variable
replacement feature.
----- Original Message -----
From: "Matthew Barnes" <ma*****@barnes .net>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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=${HOM E}/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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 following code, I bind __read_ini to __read and override __read so it
can choose between __read_ini and __read_custom. But it seems that
__read_custom never gets called when I expect it to aways be called. I have
a feeling this has to do with me not...
|
by: Marian Jancar |
last post by:
Hi,
Is there a module for parsing spec files available?
Marian
--
--
Best Regards,
|
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 &, [ and ] ?
Thanks Manlio Perillo
|
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, I'd rather
use a syntax that ConfigParser can't handle.
I know I can maintain two separate configuration files, and if I have
to I will, but I'd rather avoid that, if possible, and a solution
that suits my purposes is quite straightforward. I...
|
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 ConfigParser
def whatzit(thingname, thing):
print thingname, "value:", thing
print thingname, "length:", len(thing)
| |
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
ConfigParser.read('non-existent-filename') returns in 2.4.x
One user with 2.3.x reported an error stemming from my use of
len(cfgObject.read('potentially-non-existent-filename'))
|
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, each application can share or not other objects located in the
same or other folders.
The way I work to use these applications is:
1) For almost everyone, I execute a corresponding ".bat file" into
|
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
|
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:
smitty@localhost ~/proj/mddl4/test $ ./inputs.py
{'source_db': '/home/sweet/home.db'}
smitty@localhost ~/proj/mddl4/test $ source_db="test_env" ./inputs.py
{'source_db': 'test_env'}
smitty@localhost ~/proj/mddl4/test $ ./inputs.py -f "test_cli"
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |