python 2.7 - Is this a valid structure for voluptuous or am I missing something -
i've yaml file. i've required , optional keys , i've optional blocks, 'linux', 'http' , on.
--- project: name: lolproj url: http://wiki.tld/lolproj contact: name: foobar email: foobar@gmail.com plugins: linux: settings: disk: critical: 90 warning: 80 http: ...
i loading , validating way:
from voluptuous import * def main(): open('mytiny.yaml', 'r') f: mon = yaml.safe_load(f) project = { required('name'): all(str, length(min=5), msg="must string!"), required('url'): all(str, length(min=5)) } contact = { required('name'): all(str, length(min=5)), required('email'): all(str, length(min=5)), } disk_settings = { 'warning': int, 'critical': int, } plugins = ['linux','http','mysql'] schema = schema({ required('project'): project, required('contact'): contact, optional('plugins'): plugins, optional('settings'): { optional('disk'): disk_settings, } })
but getting error:
traceback (most recent call last): file "./readmon.py", line 143, in <module> main() file "./readmon.py", line 43, in main schema(mon) file "/usr/local/lib/python2.7/site-packages/voluptuous.py", line 207, in __call__ return self._compiled([], data) file "/usr/local/lib/python2.7/site-packages/voluptuous.py", line 386, in validate_dict return base_validate(path, iteritems(data), out) file "/usr/local/lib/python2.7/site-packages/voluptuous.py", line 287, in validate_mapping raise multipleinvalid(errors) voluptuous.multipleinvalid: expected list dictionary value @ data['plugins']
so, looks i'am doing bad. tip on this? thing is, have like:
['plugins']['linux']['settings']['disk'], or ['plugins']['http']['settings']['disk'] being 'linux'or'http' variables fields optional settings.
voluptuous documentation not newbie friendly :/
just changed yaml layout to
" ... plugins: linux: disk: critical: 90 warning: 80 exec: exitcode: 0 http: url: http://127.0.0.1 port: 8080 status: 200 ... "
and validated as
''' plugins setup ''' disk_settings = { optional('critical'): int, optional('warning'): int, optional('volume'): str, } exec_settings = { optional('exitcode'): int, } linux_settings = { optional('disk'): disk_settings, optional('exec'): exec_settings, } http_settings = { optional('url'): str, optional('port'): int, optional('status'): int, } plugins = { optional('linux'): linux_settings, optional('http'): http_settings, } ''' core schema ''' schema = schema({ required('project'): project, required('contact'): contact, optional('plugins'): plugins, })
it's working :)
Comments
Post a Comment