|
From: Louis C. <lco...@po...> - 2006-03-17 11:47:39
|
On Fri, 17 Mar 2006, Fuzzyman wrote:
> Can you explain to me what this actually does (sorry for my ignorance).
>
> You want to create a default config file from a configspec. You want to copy
> the comments from the configspec *into* the config file when this is done ?
Yes, retaining the layout of the configspec.
> You have attempted to do this with walk, but walk *doesn't* honour the order
> of the configspec, so you have implemented an alternative.
My main aim wasn't to re-implement walk. In some case I have seen
walk honour the order and in others not. (not too sure why)
I simply tried to cheat write() into writing a config-file that the
validator populated with default values. I used walk to recursively
empty each section's defaults member (section.defaults = []).
I noticed that write didn't write those entries that was in .defaults
so I removed them all. ;)
Here is an example where walk() doesn't honour my configspec's order.
For my needed end result, I don't really need walk() to do it in
order thought.
>>> configspec = '''
... [test]
... c = string(default='c')
... b = string(default='b')
... a = string(default='a')
... z = string(default='z')
... '''
>>> c = ConfigObj(options={'configspec': configspec.split('\n')})
>>> from validate import Validator
>>> validator = Validator()
>>> c.validate(validator)
True
>>> c
{'test': {'a': 'a', 'c': 'c', 'b': 'b', 'z': 'z'}}
>>> def p(s, k):
... print s.name, k
...
#
# The order is incorrect.
#
>>> c.walk(p)
test a
test c
test b
test z
{'test': {'a': None, 'c': None, 'b': None, 'z': None}}
However, if I were to use walk() to create a new ConfigObj() and
write that to file, it will write the entries in the file in the
order they we added to the config ConfigObj().
> Does that cover it all ?
Generally, yes.
> If so then I need to fix ``walk`` so that it *does* honour the order. Could I
> build your functionality into ConfigObj with an optional ``copy_comments``
> option to the validate method ?
Nice and clean, I like it.
> You could then create your default config file by doing :
>
> from validate import Validator
> from configobj import ConfigObj
>
> vdt = Validator()
> cfg = ConfigObj(configspec=filename)
> cfg.validate(vdt, copy_comments=True)
> cfg.write()
Beautiful.
> Is there any functionality you are suggesting that I have missed ?
Nope, you seems to cover it all.
Thanks, Louis.
--
Louis Cordier <lco...@po...> cell: +27721472305
Point45 Entertainment (Pty) Ltd. http://www.point45.org
|