Re: [Rest2web-develop] sections and configspec
Brought to you by:
mjfoord
|
From: Michael F. <mi...@pc...> - 2005-08-11 12:04:09
|
Nicola Larosa wrote:
>>>The only thing really missing now is having "lists of sections", for the
>>>cases when their number cannot be determined in advance. Maybe some easy
>>>workaround can be found.
>
>
>>I don't understand what you want. If you give me an example I'll think
>>about it. It sounds like you need a way of referencing (naming)
>>individual sections. Is this just for the programmer - or for the system
>>administrator (the user).
>
>
> My cousin has several cats and several dogs, and wants to "configure" them.
> (I know, peculiar cousin.) I don't exactly know how many they are, and
> moreover, their number could change with time.
>
> All the cats have the same params among them, the same with dogs, but cats'
> params are slightly different from dogs' params.
>
> I write this configspec:
>
> [ cats ]
> [[ cat ]]
> color = string
> whiskers = int
> [ dogs ]
> [[ dog ]]
> color = string
> trained = boolean
>
> Then I write this config file:
>
> [ cats ]
> [[ felix ]]
> color = black
> whiskers = 5
> [[ miao ]]
> color = white
> whiskers = 7
> [ dogs ]
> [[ fido ]]
> color = brown
> trained = yes
> [[ lassie ]]
> color = grey
> trained = no
> [[ rintintin ]]
> color = pink
> trained = no
>
> How do I specify that the "cat" configspec has to be used for the "felix"
> and "miao" sections, while the "dog" configspec has to be used for the
> "fido", "lassie" and "rintintin" sections?
>
> Or better, how do I specify that, when reading the config file, all
> subsections of the "cats" section have to be validated against the "cat"
> configspec, and that all subsections of the "dogs" section have to be
> validated against the "dog" configspec?
>
You want named types of configspec sections and to be able to apply
those types in your config file.
In this particular example you are saying that all 'dogs' (sub-sections
in the dogs section) should use the [[dog]] configspec and 'cats'
(sub-sections in the cats section) should use the 'cat' configspec.
Two possible implementations spring to mind - named sections, or
repeated sections. Repeated sections would meet your specific example
here - but aren't a generic solution. I'll suggest an alternative
implementation of named sections, that is less complicated than yesterdays.
Named Sections
==============
The trouble is that with a hierarchical structure (rather than a flat
one) there is no unique identifier per section (sections have non unique
names).
The alternatives are either to allow sections to be named - a magic
value (either '__name__' or '__type__') or to use something like a
dotted syntax to reference sections 'section.sub-section.sub-sub-section'.
I think my previous explanation was overly complex. I favour this
solution - using a '__type__' magic value in sections. These can be
treated specially when parsing configspecs and are not treated specially
by the ``write`` method.
I write this configspec:
[ cats ]
[[ cat ]]
__type__ = cat
color = string
whiskers = int
[ dogs ]
[[ dog ]]
__type__ = dog
color = string
trained = boolean
Then I write this config file:
[ cats ]
[[ felix ]]
__type__ = cat
color = black
whiskers = 5
[[ miao ]]
__type__ = cat
color = white
whiskers = 7
[ dogs ]
[[ fido ]]
__type__ = dog
color = brown
trained = yes
[[ lassie ]]
__type__ = dog
color = grey
trained = no
[[ rintintin ]]
__type__ = dog
color = pink
trained = no
When parsing a configspec any __type__ values are not treated as checks,
but a *separate* dictionary of types is kept.
If the validate method encounters a section with a __type__ value it
looks for the configspec for this section in the type dictionary.
There are a couple of implementation questions if you like this
solution.....
Repeated sections could solve this particular example without the use of
a magic value in the config file.
You could do something like
[ cats ]
[[ __repeated__ ]]
color = string
whiskers = int
[ dogs ]
[[ __repeated__ ]]
__type__ = dog
color = string
trained = boolean
Effectively saying that any subsection of 'cats' or 'dogs' has to meet
the __repeated__ specification. This isn't useful if you want to be able
to have a 'dog' somewhere else of course.....
Using the dotted syntax allows you to avoid magic values in your
configspec (which is less useful).
You could do :
[ dogs ]
[[ fido ]]
__type__ = dogs.dog
color = brown
trained = yes
[[ lassie ]]
__type__ = dogs.dog
color = grey
trained = no
[[ rintintin ]]
__type__ = dogs.dog
color = pink
trained = no
I don't like this personally.....
Regards,
Fuzzy
http://www.voidspace.org.uk/python
|