|
From: Fuzzyman <fuz...@vo...> - 2005-12-05 12:48:12
|
Matthew Brett wrote:
>Hi,
>
>
>
>>First off you could create an external config file (or dictionary) with
>>all your default values.
>>
>>Create the initial config file from the defaults. Then *update* with the
>>user one.
>>
>>
>
>I _think_ there's a problem with just an update though. Let's say I
>have this in the defaults:
>
># File is default.ini
>[section1]
>option1 = True
>[[subsection]]
>more_options = False
># end of file
>
>and I have this in my user config file:
>
># File is user.ini
>[section1]
>option1 = False
># end of file
>
>If I do:
>
>a = ConfigObj('defaults.ini')
>b = ConfigObj('user.ini')
>c = a.update(b)
>
>- doesn't this end up wiping out my subsection, because update is not recursive?
>
>
>
This is correct - and it's a bug.
Replace ``Section.update`` with :
def update(self, indict):
"""A version of update that uses our ``__setitem__``."""
for key, val in indict.items():
if key in self and isinstance(self[key], dict):
self[key].update(val)
else:
self[key] = val
and it should work.
Update to follow. :-)
Thanks
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
>>*However* - validation includes a system for setting default values. You
>>do this by including the default in the configspec.
>>
>>
>
>I think I can't use the configspec defaults, because this will mean
>that the defaults are filled in for every file I load, so I won't be
>able to tell in my merge whether the values came from the file, or
>from the defaults - but very happy to be corrected if that's not the
>case...
>
>Thanks again,
>
>Matthew
>
>
>
|