Re: Options for making AT schemas more adaptive
whit <whit-UE+B0n23d/[email protected]> Thu, 04 May 2006 10:54:59 -0500
| Newsgroups | gmane.comp.web.zope.plone.archetypes.devel |
|---|---|
| Message-ID | <[email protected]> |
<snip> > def Schema(self): > provider = ISchemaProvider(self) > return provider.Schema() > > and, in the default adapter which provides ISchemaProvider: > > def Schema(self): > return self.context.schema > > the schema provider layer would be the place where any aggregation could > occur. ISchemaProvider would define methods where specific schematas or > namespaced schemas could be returned, etc. > > schemas themselves would of course still implement an ISchema interface. > > if a non-BaseObject derived object wanted to use AT schemas for any > reason, it would have to either implement or adapt to ISchemaUser. > > i don't feel strongly re: one approach vs. the other. adapting directly > to the schema object is nice. adapting to get a provider that is the > explicit layer where schema aggregation can occur fits my brain a little > better, though. > we are getting closer...my suggestion in the previous email was the ISchema provides the layer behind which any aggregation/etc takes place. if an object can adapt to ISchema, it is provided for. Additional semantics seem unnecessary to me to cover the basic usecases. code may be be illustrative, so I've attached a rough example. I define 3 adapters: two that return a schema for direct usage, another than aids in indirect provision. the interface ISchemaProvider is a loose contract to which to register the IProvidedSchema adapter(I think this is similar to what Rob was intending). The aq_provision adapter could easily be changed to do aggregation instead of simply returning the first schema it finds. -w
adapter.txt
(text/plain, 1.6 KB)
Imports from the idealized future::
>>> from zope.component import provideAdapter
>>> from archetypes.schema.interfaces import\
>>> ISchema, IProvidedSchema, ISchemaProvider
>>> from archetypes.content.interfaces import IBaseObject, IBaseFolder, IBaseContent
>>> from archetypes.utils import aq_wrapper
configurable object for accessing schemas::
>>> class AttrGetter(object):
...
... attr = '_schema'
... def __init__(self, attr=None):
... if attr: self.attr=attr
...
... def __call__(self, context):
... val=getattr(context, self.attr)
... return aq_wrapper(val)
...
>>> getBasic = AttrGetter()
>>> getProvided = AttrGetter(attr='_provided')
>>> provideAdapter(getBasic, adapts=(IBaseFolder,), provides=ISchema)
>>> provideAdapter(getProvided, adapts=(ISchemaProvider,), provides=IProvidedSchema)
Adapter for provision by aquisition::
>>> def aq_provided(context):
... chain=context.aq_chain
... for link in chain:
... schema = IProvideSchema(link, None)
... if schema:
... return schema
>>> provideAdapter(aq_provided, adapts=(IBaseContent,), provides=ISchema)
From the implementation side. 'page' implements IBaseContent,
'folder' implements IBaseFolder::
>>> from Products.Five.utilities.marker import mark
>>> folder, page = app.site.atfolder, app.site.folder.atpage
>>> mark(folder, ISchemaProvider)
>>> folder._provided=wrappable_object
>>> folder._schema = folder._provided
>>> ISchema(page) == ISchema(folder)