[Checkins] SVN: zc.sourcefactory/trunk/ - added contextual token policy

Christian Theune ct at gocept.com
Sun Jun 10 08:22:08 EDT 2007


Log message for revision 76570:
   - added contextual token policy
  

Changed:
  U   zc.sourcefactory/trunk/CHANGES.txt
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/README.txt
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/browser/source.py
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/interfaces.py
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/policies.py

-=-
Modified: zc.sourcefactory/trunk/CHANGES.txt
===================================================================
--- zc.sourcefactory/trunk/CHANGES.txt	2007-06-10 11:39:40 UTC (rev 76569)
+++ zc.sourcefactory/trunk/CHANGES.txt	2007-06-10 12:22:07 UTC (rev 76570)
@@ -2,11 +2,13 @@
 Changes
 =======
 
+0.2 - 2007-07-10
+================
 
-0.2
-===
+    - Added a contextual token policy interface that allows getToken and
+      getValue to access the cotext for contextual sources.
 
-    - Added a contextual term policy interface that allow createTerm and
+    - Added a contextual term policy interface that allows createTerm and
       getTitle to access the context for contextual sources.
 
     - Added compatibility for Zope 3.2 and Zope 2.9 (via Five 1.3)

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/README.txt
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/README.txt	2007-06-10 11:39:40 UTC (rev 76569)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/README.txt	2007-06-10 12:22:07 UTC (rev 76570)
@@ -18,21 +18,21 @@
 -----------
 
 In the most simple case, you only have to provide a method that returns a list
-of values and derive from 'BasicSourceFactory':
+of values and derive from `BasicSourceFactory`::
 
   >>> import zc.sourcefactory.basic
   >>> class MyStaticSource(zc.sourcefactory.basic.BasicSourceFactory):
   ...     def getValues(self):
   ...         return ['a', 'b', 'c']
 
-When calling the source factory, we get a source:
+When calling the source factory, we get a source::
 
   >>> source = MyStaticSource()
   >>> import zope.schema.interfaces
   >>> zope.schema.interfaces.ISource.providedBy(source)
   True
 
-The values match our `getValues`-method of the factory:
+The values match our `getValues`-method of the factory::
 
   >>> list(source)
   ['a', 'b', 'c']
@@ -47,7 +47,7 @@
 Sometimes we need context to determine the values. In this case, the
 `getValues`-method gets a parameter `context`.
 
-Let's assume we have a small object containing data to be used by the source:
+Let's assume we have a small object containing data to be used by the source::
 
   >>> class Context(object):
   ...      values = []
@@ -58,13 +58,13 @@
   ...     def getValues(self, context):
   ...         return context.values
 
-When instanciating, we get a ContextSourceBinder:
+When instanciating, we get a ContextSourceBinder::
 
   >>> binder = MyDynamicSource()
   >>> zope.schema.interfaces.IContextSourceBinder.providedBy(binder)
   True
 
-Binding it to a context, we get a source:
+Binding it to a context, we get a source::
 
   >>> context = Context()
   >>> source = binder(context)
@@ -74,7 +74,7 @@
   >>> list(source)
   []
 
-Modifying the context also modifies the data in the source:
+Modifying the context also modifies the data in the source::
 
   >>> context.values = [1,2,3,4]
   >>> list(source)
@@ -93,7 +93,7 @@
 
 This is useful if you want to have more specific sources (by subclassing) that
 share the same basic origin of the data but have different filters applied to
-it.
+it::
 
   >>> class FilteringSource(zc.sourcefactory.basic.BasicSourceFactory):
   ...     def getValues(self):
@@ -104,7 +104,7 @@
   >>> list(source)
   [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
 
-Subclassing modifies the filter, not the original data:
+Subclassing modifies the filter, not the original data::
 
   >>> class OtherFilteringSource(FilteringSource):
   ...     def filterValue(self, value):
@@ -113,14 +113,14 @@
   >>> list(source)
   [2, 4, 6, 8, 10, 12, 14, 16, 18]
 
-The "in" operator get's applied also to filtered values
+The "in" operator gets applied also to filtered values::
 
   >>> 2 in source
   True
   >>> 3 in source
   False
 
-The "len" also get's applied to filtered values:
+The "len" also gets applied to filtered values::
 
   >>> len(source)
   9

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/browser/source.py
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/browser/source.py	2007-06-10 11:39:40 UTC (rev 76569)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/browser/source.py	2007-06-10 12:22:07 UTC (rev 76570)
@@ -63,7 +63,7 @@
 
     def getTerm(self, value):
         title = self.source.factory.getTitle(self.source.context, value)
-        token = self.source.factory.getToken(value)
+        token = self.source.factory.getToken(self.source.context, value)
         return self.source.factory.createTerm(
             self.source.context, self.source, value, title, token,
             self.request)

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/interfaces.py
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/interfaces.py	2007-06-10 11:39:40 UTC (rev 76569)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/interfaces.py	2007-06-10 12:22:07 UTC (rev 76570)
@@ -59,6 +59,20 @@
         """Return a token for the value."""
 
 
+class IContextualTokenPolicy(zope.interface.Interface):
+    """The contextua token policy maps values and tokens.
+
+    It allows access to the context.
+
+    """
+
+    def getValue(context, source, token):
+        """Return a token for the value."""
+
+    def getToken(context, value):
+        """Return a token for the value."""
+
+
 class ITermPolicy(zope.interface.Interface):
     """The term policy creates terms and provides data for terms."""
 

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/policies.py
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/policies.py	2007-06-10 11:39:40 UTC (rev 76569)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/policies.py	2007-06-10 12:22:07 UTC (rev 76570)
@@ -100,6 +100,23 @@
         return zc.sourcefactory.interfaces.IToken(value)
 
 
+class BasicContextualTokenPolicy(BasicTokenPolicy):
+    """A basic contextual token policy.
+
+    This implements a fallback to the context-free token policy but satisfies
+    the contextual interfaces.
+
+    """
+
+    zope.interface.implements(zc.sourcefactory.interfaces.IContextualTokenPolicy)
+
+    def getValue(self, context, source, token):
+        return super(BasicContextualTokenPolicy, self).getValue(source, token)
+
+    def getToken(self, context, value):
+        return super(BasicContextualTokenPolicy, self).getToken(value)
+
+
 class IntIdTokenPolicy(object):
     """A token policy based on intids."""
 
@@ -164,7 +181,7 @@
 
 
 class BasicContextualSourcePolicy(
-    BasicContextualValuePolicy, BasicTokenPolicy, BasicContextualTermPolicy):
+    BasicContextualValuePolicy, BasicContextualTokenPolicy, BasicContextualTermPolicy):
     pass
 
 



More information about the Checkins mailing list