[Checkins] SVN: zc.sourcefactory/trunk/ Merge aaron-flexible-contextual-source-binder and update the CHANGES.txt
Aaron Lehmann
aaron at zope.com
Tue Jan 27 12:43:00 EST 2009
Log message for revision 95241:
Merge aaron-flexible-contextual-source-binder and update the CHANGES.txt
Changed:
U zc.sourcefactory/trunk/CHANGES.txt
U zc.sourcefactory/trunk/src/zc/sourcefactory/README.txt
U zc.sourcefactory/trunk/src/zc/sourcefactory/factories.py
-=-
Modified: zc.sourcefactory/trunk/CHANGES.txt
===================================================================
--- zc.sourcefactory/trunk/CHANGES.txt 2009-01-27 17:42:34 UTC (rev 95240)
+++ zc.sourcefactory/trunk/CHANGES.txt 2009-01-27 17:43:00 UTC (rev 95241)
@@ -5,7 +5,9 @@
0.4.1 (unreleased)
==================
- -
+ - FactoredContextualSourceBinder.__call__ now accepts arguments giving the
+ args to pass to source class. ContextualSourceFactory now uses a class
+ variable to tell what kind of Source to make.
0.4.0 (2008-12-11)
Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/README.txt
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/README.txt 2009-01-27 17:42:34 UTC (rev 95240)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/README.txt 2009-01-27 17:43:00 UTC (rev 95241)
@@ -87,7 +87,29 @@
>>> len(source)
4
+It's possible to have the default machinery return different sources, by
+providing a source_class argument when calling the binder. One can also
+provide arguments to the source.
+ >>> class MultiplierSource(zc.sourcefactory.source.FactoredContextualSource):
+ ... def __init__(self, factory, context, multiplier):
+ ... super(MultiplierSource, self).__init__(factory, context)
+ ... self.multiplier = multiplier
+ ...
+ ... def _get_filtered_values(self):
+ ... for value in self.factory.getValues(self.context):
+ ... yield self.multiplier * value
+ >>> class MultiplierSourceFactory(MyDynamicSource):
+ ... source_class = MultiplierSource
+ >>> binder = MultiplierSourceFactory()
+ >>> source = binder(context, multiplier=5)
+ >>> list(source)
+ [5, 10, 15, 20]
+ >>> 5 in source
+ True
+ >>> len(source)
+ 4
+
Filtering
=========
Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/factories.py
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/factories.py 2009-01-27 17:42:34 UTC (rev 95240)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/factories.py 2009-01-27 17:43:00 UTC (rev 95241)
@@ -45,11 +45,13 @@
Implementors must provide an implementation for `getValues`.
"""
+ source_class = zc.sourcefactory.source.FactoredContextualSource
+
def __new__(cls, *args, **kw):
"""Create the factory object and return source."""
factory = object.__new__(cls)
factory.__init__(*args, **kw)
- return FactoredContextualSourceBinder(factory)
+ return FactoredContextualSourceBinder(factory, cls.source_class)
class FactoredContextualSourceBinder(object):
@@ -57,9 +59,9 @@
zope.interface.implements(zope.schema.interfaces.IContextSourceBinder)
- def __init__(self, factory):
+ def __init__(self, factory, source_class):
self.factory = factory
+ self.source_class = source_class
- def __call__(self, context):
- return zc.sourcefactory.source.FactoredContextualSource(
- self.factory, context)
+ def __call__(self, context, *args, **kwargs):
+ return self.source_class(self.factory, context, *args, **kwargs)
More information about the Checkins
mailing list