|
From: Matthew B. <mat...@gm...> - 2005-12-03 01:14:31
|
Hi, I am very sorry if I missed something simple, but is there an easy way to get a list of informative validation error messages from the ConfigObj.validate interface? I have run: res =3D config_obj.validate(vtor) and have a nested list of boolean values - but I want to be able to print out a list of items that failed validation and why. I am just thinking about reiterating through the option dictionary, running the validation by hand (.check) for each False in the 'res' dictionary, but this seems very cludgy - is there a better way? Thanks a lot, Matthew |
|
From: Fuzzyman <fuz...@vo...> - 2005-12-05 08:40:05
|
Matthew Brett wrote:
>No problem, thanks very much for letting me know...
>
>
>
Hello Matthew,
I don't think my previous answer addressed all the issues you were
asking about.
I realise that you also want to include in your list *why* the test
failed. Again the question is "in what form do you want these error
messages" ?
Obviously the information you have to go off is the original test (or if
the entry is just plain missing) - if you had a flattened list of
failures (of the sort I discussed) you could have each entry as a tuple :
(entry_name, original_test)
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 ?
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
>On 12/3/05, Fuzzyman <fuz...@vo...> wrote:
>
>
>>Hello Matthew,
>>
>>I'm very short of time - I'll do you a proper reply on Monday, sorry
>>about that.
>>
>>Michael
>>
>>Matthew Brett wrote:
>>
>>
>>
>>>Hi,
>>>
>>>I am very sorry if I missed something simple, but is there an easy way
>>>to get a list of informative validation error messages from the
>>>ConfigObj.validate interface?
>>>
>>>I have run:
>>>
>>>res = config_obj.validate(vtor)
>>>
>>>and have a nested list of boolean values - but I want to be able to
>>>print out a list of items that failed validation and why. I am just
>>>thinking about reiterating through the option dictionary, running the
>>>validation by hand (.check) for each False in the 'res' dictionary,
>>>but this seems very cludgy - is there a better way?
>>>
>>>Thanks a lot,
>>>
>>>Matthew
>>>
>>>
>>>-------------------------------------------------------
>>>This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
>>>for problems? Stop! Download the new AJAX search engine that makes
>>>searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
>>>http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
>>>_______________________________________________
>>>Configobj-develop mailing list
>>>Con...@li...
>>>https://lists.sourceforge.net/lists/listinfo/configobj-develop
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
>
|
|
From: Matthew B. <mat...@gm...> - 2005-12-05 09:05:45
|
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=3D[], levels=3D['[root]']):
if res is True: return
if res is False: res =3D 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 =3D 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=3DNone, error_strs=3D[], levels=3D['b=
ase'])
then
val_res =3D opts.validate(vtor, errror_strs =3D my_list)?
Thanks a lot.
Matthew
|
|
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
>
>
>
|