Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a ConfigParser which keeps comments

3,473 views
Skip to first unread message

Gelonida N

unread,
Mar 14, 2012, 6:07:33 AM3/14/12
to pytho...@python.org
Hi,


At the moment I use ConfigParser
http://docs.python.org/library/configparser.html
for one of my applications.


Now I'm looking for a library, which behaves like config parser, but
with one minor difference.

The write() mehtod should keep existing comments.

Does anybody know or implement something like this or is there as
switrch, that I overlooked in hte documentaiton.


Terry Reedy

unread,
Mar 14, 2012, 1:06:30 PM3/14/12
to pytho...@python.org
Assuming that you have not overlooked anything, I would just subclass
ConfigParser with an altered write method.

--
Terry Jan Reedy

Tim Chase

unread,
Mar 14, 2012, 2:13:12 PM3/14/12
to Terry Reedy, pytho...@python.org
On 03/14/12 12:06, Terry Reedy wrote:
> On 3/14/2012 6:07 AM, Gelonida N wrote:
>> Now I'm looking for a library, which behaves like config parser, but
>> with one minor difference.
>>
>> The write() mehtod should keep existing comments.
>
> Assuming that you have not overlooked anything, I would just subclass
> ConfigParser with an altered write method.

It would require a lot more than that. It would entail changing
the reading as well so that it preserved the comments as well as
the order of sections & keys, and a way of storing those
associated comments in sequence. I looked into it a fair while
back and it was a LOT more work than I cared to do for minimal
gain. I wimped out and just documented it with "If you use the
ability to (re)write a configuration file, it will not keep any
comments or ordering from any original sources."

-tkc


Steven W. Orr

unread,
Mar 14, 2012, 10:48:32 PM3/14/12
to pytho...@python.org
On 3/14/2012 6:07 AM, Gelonida N wrote:
> Hi,
>
>
> At the moment I use ConfigParser
> http://docs.python.org/library/configparser.html
> for one of my applications.
>
>
> Now I'm looking for a library, which behaves like config parser, but
> with one minor difference.
>
> The write() mehtod should keep existing comments.
>
> Does anybody know or implement something like this or is there as
> switrch, that I overlooked in hte documentaiton.
>
>

I use ConfigObj.

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

Cameron Simpson

unread,
Mar 15, 2012, 5:42:24 PM3/15/12
to Tim Chase, pytho...@python.org, Terry Reedy
On 14Mar2012 13:13, Tim Chase <pytho...@tim.thechases.com> wrote:
| On 03/14/12 12:06, Terry Reedy wrote:
| > On 3/14/2012 6:07 AM, Gelonida N wrote:
| >> Now I'm looking for a library, which behaves like config parser, but
| >> with one minor difference.
| >>
| >> The write() mehtod should keep existing comments.
| >
| > Assuming that you have not overlooked anything, I would just subclass
| > ConfigParser with an altered write method.
|
| It would require a lot more than that. It would entail changing
| the reading as well so that it preserved the comments as well as
| the order of sections & keys, and a way of storing those
| associated comments in sequence. I looked into it a fair while
| back and it was a LOT more work than I cared to do for minimal
| gain. I wimped out and just documented it with "If you use the
| ability to (re)write a configuration file, it will not keep any
| comments or ordering from any original sources."

A low cost approach might be to patch the file instead of transcribing
the in-memory state. Not the same semantics, but it would not be too
hard to add a patch_config(filename, section, setting, value) that read
the old file and wrote a new one with an adjusted section, ignoring the
in-memory state (indeed, on that basis the siganture isn't a method but
a standalone function).

The logic is easy enough that I even wrote a shell script in 2004 to do
essentially this:

https://bitbucket.org/cameron_simpson/css/src/ef42896872b5/bin/winclauseappend

One could imagine an efficient python implementation and a ConfigParser
subclass that patched the file if a setting got changed, or had a .patch
method to apply particular setting changes as desired.

Cheers,
--
Cameron Simpson <c...@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

From sci.physics:
tr...@mailzone.com:
The only problem is, how do you send a message from Earth to Mars
instantly? Does anyone have any ideas about where we can start?
John Baez <ba...@math.mit.edu:
Just use a coordinate system in which the point at which the message is
received has the same t coordinate as the point at which the message was sent.

Karim

unread,
Mar 15, 2012, 5:49:57 PM3/15/12
to pytho...@python.org
Le 15/03/2012 03:48, Steven W. Orr a écrit :
> On 3/14/2012 6:07 AM, Gelonida N wrote:
>> Hi,
>>
>>
>> At the moment I use ConfigParser
>> http://docs.python.org/library/configparser.html
>> for one of my applications.
>>
>>
>> Now I'm looking for a library, which behaves like config parser, but
>> with one minor difference.
>>
>> The write() mehtod should keep existing comments.
>>
>> Does anybody know or implement something like this or is there as
>> switrch, that I overlooked in hte documentaiton.
>>
>>
>
> I use ConfigObj.
>

Sure configObj is a must...I use it too.

http://www.voidspace.org.uk/python/configobj.html

Cheers
Karim

Gelonida N

unread,
Mar 15, 2012, 8:34:44 PM3/15/12
to pytho...@python.org
On 03/15/2012 10:42 PM, Cameron Simpson wrote:
> On 14Mar2012 13:13, Tim Chase <pytho...@tim.thechases.com> wrote:
> | On 03/14/12 12:06, Terry Reedy wrote:
> | > On 3/14/2012 6:07 AM, Gelonida N wrote:
> | >> Now I'm looking for a library, which behaves like config parser, but
> | >> with one minor difference.
> | >>
> | >> The write() mehtod should keep existing comments.
> | >
> | > Assuming that you have not overlooked anything, I would just subclass
> | > ConfigParser with an altered write method.
> |
> | It would require a lot more than that. It would entail changing
> | the reading as well so that it preserved the comments as well as
> | the order of sections & keys, and a way of storing those
> | associated comments in sequence. I looked into it a fair while
> | back and it was a LOT more work than I cared to do for minimal
> | gain. I wimped out and just documented it with "If you use the
> | ability to (re)write a configuration file, it will not keep any
> | comments or ordering from any original sources."
>
> A low cost approach might be to patch the file instead of transcribing
> the in-memory state. Not the same semantics, but it would not be too
> hard to add a patch_config(filename, section, setting, value) that read
> the old file and wrote a new one with an adjusted section, ignoring the
> in-memory state (indeed, on that basis the siganture isn't a method but
> a standalone function).
>
> The logic is easy enough that I even wrote a shell script in 2004 to do
> essentially this:
>
> https://bitbucket.org/cameron_simpson/css/src/ef42896872b5/bin/winclauseappend
>
> One could imagine an efficient python implementation and a ConfigParser
> subclass that patched the file if a setting got changed, or had a .patch
> method to apply particular setting changes as desired.
>

Agreed, patching is simpler than parsing the file and keeping all the
comment info in the config object.

I will also look at ConfigObj as suggested by Steven and Karim

If this does what I want, then it's probably less effort to use this
library than patching Configparser.


0 new messages