|
From: Eli C. <el...@co...> - 2015-08-03 20:08:20
|
I don't believe there's any way to make a section optional in the way you
want. The reasoning is that when a section is defined in the configspec,
it always exists in your config even if it's not included in the config
file. Therefore, in your example [[[Location]]] will always exist for each
of the __many__ subsections regardless of whether the config file defines
it. In cases like this I would typically assign default values to all of
the options in that subsection.
- Eli
On Fri, Jul 31, 2015 at 8:08 PM, Sumit's Mailing Lists <
li...@ba...> wrote:
> I've been looking through the docs and StackOverflow and I'm still a
> little stumped on this. Can I have an optional section in my config
> specification? For example, say I had this as my configspec:
>
> config_specification = """
> [General]
>
> sample_rate = float
>
> [Data]
> [[__many__]]
> type = option('sense.xml')
> [[[Identifier]]]
> type = option("name", "mac", "uuid")
> adapter = string(default='')
> id = string(default='')
> file = string(default='')
> [[[Location]]]
> type = option("virtual", "gps")
> longitude = string(default='')
> latitude = string(default='')
> altitude = string(default='')
> speed = string(default='')
> climb = string(default='')
> track = string(default='')
> longutide_error = string(default='')
> latitude_error = string(default='')
> altitude_error = string(default='')
> speed_error = string(default='')
> climb_track = string(default='')
> track_error = string(default='')
> """
>
> Is there a way to make [[[Locaiton]]] optional, so it can be missing and
> still pass validation (assuming that I do validation like so):
>
> cfg = ConfigObj(filename,
> configspec=BootStrap.config_specification.split('\n'))
>
> test = cfg.validate(Validator())
>
> valid = True
> for (section_list, key, _) in flatten_errors(cfg, test):
> if key is not None:
> logging.error('Invalid value for key {0} in section
> {1}'.format(key, ', '.join(section_list)))
> valid = False
> else:
> logging.error('Section {0} failed validation'.format(',
> '.join(section_list)))
> valid = False
> if not valid:
> exit(3)
>
> Thanks
> Sumit
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
|