|
From: Fuzzyman <fuz...@vo...> - 2005-12-05 09:17:54
|
Matthew Brett wrote:
>Hi,
>
>
>
>> You would still have to produce your own error messages from these of
>>course - would it be useful if I showed you a (recursive) function to
>>produce a list like this ?
>>
>>
>
>I ended up with something like this:
>
># Check / processing functions for options
>def check_result(sct, res, err_strs=[], levels=['[root]']):
> if res is True: return
> if res is False: res = dict(zip(sct.keys(), [res]*len(sct)))
>
> for (k, v) in sct.items():
> if res[k] is True: continue
> if isinstance(v, dict):
> # Go down one level
> levels.append(k)
> check_result(v, res[k], err_strs, levels)
> continue
> try:
> vtor.check(sct.configspec[k], v)
> except validate.ValidateError, err:
> err_strs.append("%s, %s: %s" % (", ".join(levels), k, err))
>
> # Go up one level
> if levels: levels.pop()
>
> return err_strs
>
># Run checks
>val_res = opts.validate(vtor)
>if val_res is not True:
> print "\n".join(check_result(opts, val_res))
>
>I hope this is efficient, I am rather new to python. Of course I was
>wondering if I was missing some way of avoiding redoing the error
>checks, but it sounds like I haven't, and this works fine for me. I
>wonder if there is any interest in a patch to the configobj.py
>funciton, on the lines of:
>
>def validate(self, validator, section=None, error_strs=[], levels=['base'])
>
>then
>
>val_res = opts.validate(vtor, errror_strs = my_list)?
>
>
Right, I see.
The validate method does actually lose information - the check method
*does* give you a more useful error message (like value too large, too
small etc). The validate method just turns this into True/False.
So currently - the only option is to re-run the test to retrieve the
actual error.
Yes - it sounds like an extra option to validate would be a good idea.
I don't think I'd return it as a flattened list by default - maybe just
replace the True/False with the error messages, and maybe include a
function like yours as an example.
ConfigObj 4.0.3 anyone :-) (I'll probably implement this in the next few
days - it's a good idea).
Your function looks fine by the way.
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
>Thanks a lot.
>
>Matthew
>
>
>
|