|
From: Entity R. <ent...@gm...> - 2011-08-30 21:18:47
|
>
> *import StringIO*
>
> *
>> *
>
> *from configobj import ConfigObj, flatten_errors*
>
> *from validate import Validator*
>
> *
>> *
>
> *def validateConfig(config):*
>
> * validator = Validator()*
>
> * haserrors = config.validate(validator, preserve_errors=True)*
>
> * errors = []*
>
> * if haserrors is True:*
>
> * for (section_list, key, exc) in flatten_errors(config,
>> haserrors):*
>
> * if key is not None:*
>
> * errors.append('\t"%s" in "%s" failed validation. (%s)' %
>> (key, ', '.join(section_list), exc))*
>
> * else:*
>
> * errors.append('\tThe following sections were missing:%s '
>> % ', '.join(section_list))*
>
> *
>> *
>
> * return errors*
>
> *
>> *
>
> *def loadConfig(filename, validatefile, path="."):*
>
> * config = ConfigObj("%s/%s"%(path, filename),*
>
> * configspec=validatefile)*
>
> *
>> *
>
> * invalid = validateConfig(config)*
>
> * if invalid:*
>
> * return False*
>
> *
>> *
>
> * return config*
>
> *
>> *
>
> *# config file:*
>
> *# [general]*
>
> *# test = abcd*
>
> *
>> *
>
> *s = StringIO.StringIO()*
>
> *s.write("""*
>
> *[general]*
>
> *test = integer()*
>
> *""")*
>
> *
>> *
>
> *conf = loadConfig("config.conf", s)*
>
> *
>> *
>
> *errors = validateConfig(conf)*
>
> *print errors # should print something :(*
>
> *print conf*
>
>
> This doesn't seem to work :/
Output:
> *[]
> {'general': {'test': 'abcd'}} *
On Tue, Aug 30, 2011 at 12:39 PM, David Hostetler <neg...@gm...>wrote:
> Try StringIO.
>
>
> -hoss
>
>
>
> On Tue, Aug 30, 2011 at 15:26, Entityreborn <ent...@gm...>wrote:
>
>>
>> As opposed to setting your template for validation via a seperate file,
>> can this be done by providing the validator class with a string?
>>
>>
>> ------------------------------------------------------------------------------
>> Special Offer -- Download ArcSight Logger for FREE!
>> Finally, a world-class log management solution at an even better
>> price-free! And you'll get a free "Love Thy Logs" t-shirt when you
>> download Logger. Secure your free ArcSight Logger TODAY!
>> http://p.sf.net/sfu/arcsisghtdev2dev
>> _______________________________________________
>> Configobj-develop mailing list
>> Con...@li...
>> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>>
>
>
>
> ------------------------------------------------------------------------------
> Special Offer -- Download ArcSight Logger for FREE!
> Finally, a world-class log management solution at an even better
> price-free! And you'll get a free "Love Thy Logs" t-shirt when you
> download Logger. Secure your free ArcSight Logger TODAY!
> http://p.sf.net/sfu/arcsisghtdev2dev
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
--
~EntityReborn
|