[Zope-dev] Re: [Zope3-dev] ZConfig: Keytype and section type
extension
Phillip J. Eby
pje at telecommunity.com
Mon Dec 29 14:53:00 EST 2003
At 01:53 PM 12/29/03 -0500, Fred L. Drake, Jr. wrote:
>Phillip J. Eby writes:
> > That reminds me... is there any way for section *names* to be
> > case-sensitive, or at least case-preserving? For example, if one were
> > simulating Apache-style configuration like:
>
>There isn't, but there was at some point. Nobody here at ZC seemed to
>think it useful at the time, so it was removed as a simplification.
>
>What we had was a "nametype" attribute for <sectiontype> elements; it
>was just another datatype function.
>
>I'd be happy to have that back if there's a real use for it. The
>current situation, where the conversion is coded directly in
>ZConfig.cfgparser, isn't very flexible.
Agreed. :)
>Would it suit your requirements if the name type were associated with
>the section type, and inherited from the base type if not specified?
Yep.
> > Currently, IIRC, the '_name' attribute of the resulting section will be
> > '/foo/bar/baz'.
>
>The _name attribute is an implementation detail; use the
>getSectionName() method instead. I'm likely to change the _name
>detail just to be facetious. ;-)
Well, in that case, can we have a 'getItems()' that returns only the
key-value pairs for actual attributes, and does *not* include keys for
values that weren't explicitly specified in the configuration file and
don't have defaults?
In PEAK, the binding.Component class has this constructor currently:
def fromZConfig(klass, section):
"""Classmethod: Create an instance from a ZConfig 'section'"""
# ZConfig uses unicode for keys and defaults unsupplied values to None
data = dict([(str(k),v) for k,v in section.__dict__.items()
if v is not None])
if not hasattr(klass,'_name') and '_name' in data:
del data['_name']
if not hasattr(klass,'_matcher') and '_matcher' in data:
del data['_matcher']
return klass(**data)
As you can see, I'm doing a fair amount of work in order to extract a
mapping I can use as keyword arguments for the regular constructor. The
standard way in PEAK to make a component configurable by ZConfig is simply
to specify "whatevertype.fromZConfig" as the "datatype" in the ZConfig schema.
If there were a 'getMapping' method available, the above would become
something like:
def fromZConfig(klass, section):
"""Classmethod: Create an instance from a ZConfig 'section'"""
data = dict([(str(k),v) for k,v in section.getItems()]
data['_name'] = section.getSectionName()
return klass(**data)
Unless of course the keys were strings rather than Unicode, in which case
it'd be even simpler. (The issue here is that Python requires keyword
arguments to be strings rather than Unicode, even though attribute names
can be Unicode. ZConfig sets attributes using unicode because ZConfig
schemas are XML, and therefore Unicode.)
More information about the Zope-Dev
mailing list