[Checkins] SVN: zope.schema/branches/roger-sources/src/zope/schema/ - implemented some term bases

Roger Ineichen roger at projekt01.ch
Sat Aug 30 11:34:17 EDT 2008


Log message for revision 90618:
  - implemented some term bases
  - make SimpleTerm aware of title changes
  - implemented ITerms which offers an API for query ITerms

Changed:
  U   zope.schema/branches/roger-sources/src/zope/schema/interfaces.py
  A   zope.schema/branches/roger-sources/src/zope/schema/term.py
  U   zope.schema/branches/roger-sources/src/zope/schema/vocabulary.py

-=-
Modified: zope.schema/branches/roger-sources/src/zope/schema/interfaces.py
===================================================================
--- zope.schema/branches/roger-sources/src/zope/schema/interfaces.py	2008-08-30 14:02:23 UTC (rev 90617)
+++ zope.schema/branches/roger-sources/src/zope/schema/interfaces.py	2008-08-30 15:34:16 UTC (rev 90618)
@@ -500,6 +500,7 @@
 
     title = TextLine(title=_(u"Title"))
 
+
 class ISource(Interface):
     """A set of values from which to choose
 
@@ -515,6 +516,94 @@
         """Return whether the value is available in this source
         """
 
+class IIterableSource(ISource):
+    """Source which supports iteration over allowed values.
+
+    The objects iteration provides must be values from the source.
+    """
+
+    def __iter__():
+        """Return an iterator which provides the values from the source."""
+
+    def __len__():
+        """Return the number of valid values, or sys.maxint."""
+
+class ITokenizedSource(ISource):
+    """Tokenized source."""
+
+    def getTerm(value):
+        """Return the ITerm object for the term 'value'.
+
+        If `value` is not represented in the source, `LookupError`
+        is raised.
+        """
+
+    def getValue(token):
+        """Return a value for a given identifier token
+
+        If `value` is not represented in the source, `LookupError`
+        is raised.
+        """
+
+    def getTermByToken(token):
+        """Return an ITokenizedTerm for the passed-in token.
+
+        If `token` is not represented in the source, `LookupError`
+        is raised.
+        """
+
+class IIterableTokenizedSource(IIterableSource, ITokenizedSource):
+    """Iterable tokenized source."""
+
+
+class IContextSourceBinder(Interface):
+
+    def __call__(context):
+        """Return a context-bound instance that implements ISource.
+        """
+
+class ITerms(Interface):
+    """Terms offer a simple query API for ISource used in schema fields
+
+    Since the ITerms API needs to offer a concept for communicate the term
+    values via HTML force and back we provide by default the tokenized term
+    concept. There are some use case which terms can use the real values 
+    intead of the token representation, but that's not relevant.
+    
+    To use a token as value representation is always a good idea even if the
+    token representaents the real value.
+
+    Terms returned from getTerm() and provided by iteration must
+    conform to ITokenizedTerm.
+    """
+
+    def getTerm(value):
+        """Return the ITokenizedTerm object for the term 'value'.
+
+        If `value` is not represented in the source, `LookupError`
+        is raised.
+        """
+
+    def getValue(token):
+        """Return a value for a given identifier token
+
+        If `value` is not represented in the source, `LookupError`
+        is raised.
+        """
+
+    def getTermByToken(token):
+        """Return an ITokenizedTerm for the passed-in token.
+
+        If `token` is not represented in the source, `LookupError`
+        is raised.
+        """
+
+    def __iter__():
+        """Return an iterator which provides the values from the source."""
+
+    def __len__():
+        """Return the number of valid values, or sys.maxint."""
+
 class ISourceQueriables(Interface):
     """A collection of objects for querying sources
     """
@@ -538,12 +627,7 @@
 
         """
 
-class IContextSourceBinder(Interface):
-    def __call__(context):
-        """Return a context-bound instance that implements ISource.
-        """
 
-
 class IBaseVocabulary(ISource):
     """Representation of a vocabulary.
 
@@ -560,19 +644,6 @@
         """
 
 
-class IIterableSource(ISource):
-    """Source which supports iteration over allowed values.
-
-    The objects iteration provides must be values from the source.
-    """
-
-    def __iter__():
-        """Return an iterator which provides the values from the source."""
-
-    def __len__():
-        """Return the number of valid values, or sys.maxint."""
-
-
 # BBB vocabularies are pending deprecation, hopefully in 3.3
 class IIterableVocabulary(Interface):
     """Vocabulary which supports iteration over allowed values.

Added: zope.schema/branches/roger-sources/src/zope/schema/term.py
===================================================================
--- zope.schema/branches/roger-sources/src/zope/schema/term.py	                        (rev 0)
+++ zope.schema/branches/roger-sources/src/zope/schema/term.py	2008-08-30 15:34:16 UTC (rev 90618)
@@ -0,0 +1,99 @@
+##############################################################################
+#
+# Copyright (c) 2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Term support for schema.
+
+$Id:$
+"""
+
+import zope.interface
+import zope.interface.declarations
+from zope.schema import interfaces
+
+
+# ITerm
+class SimpleTerm(object):
+    """Simple tokenized term implementation."""
+
+    zope.interface.implements(interfaces.ITokenizedTerm)
+
+    _title = None
+
+    def __init__(self, value, token=None, title=None):
+        """Create a term for value and token. If token is omitted,
+        str(value) is used for the token.  If title is provided, 
+        term implements ITitledTokenizedTerm. If we change a title after the
+        initiaization we will adjust the ITitledTokenizedTerm support
+        """
+        self.value = value
+        if token is None:
+            token = value
+        self.token = str(token)
+        self.title = title
+
+    @apply
+    def title():
+        def get(self):
+            return self._title
+        def set(self, title):
+            self._title = title
+            if self._title is None:
+                if interfaces.ITitledTokenizedTerm.providedBy(self):
+                    zope.interface.declarations.noLongerProvides(self,
+                        interfaces.ITitledTokenizedTerm)
+            else:
+                zope.interface.declarations.alsoProvides(self,
+                    interfaces.ITitledTokenizedTerm)
+        return property(get, set)
+
+
+class Term(object):
+    """Term implementation."""
+
+    zope.interface.implements(interfaces.ITerm)
+
+    def __init__(self, value):
+        """Create a term for value.
+        """
+        self.value = value
+
+
+class TokenizedTerm(object):
+    """Tokenized term implementation."""
+
+    zope.interface.implements(interfaces.ITokenizedTerm)
+
+    def __init__(self, value, token=None):
+        """Create a term for value and token. If token is omitted,
+        str(value) is used for the token.
+        """
+        self.value = value
+        if token is None:
+            token = value
+        self.token = str(token)
+
+
+class TitledTokenizedTerm(object):
+    """Title tokenized term implementation."""
+
+    zope.interface.implements(interfaces.ITitledTokenizedTerm)
+
+    def __init__(self, value, token=None, title=None):
+        """Create a term for value, token and title. If token is omitted,
+        str(value) is used for the token.
+        """
+        self.value = value
+        if token is None:
+            token = value
+        self.token = str(token)
+        self.title = title


Property changes on: zope.schema/branches/roger-sources/src/zope/schema/term.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: zope.schema/branches/roger-sources/src/zope/schema/vocabulary.py
===================================================================
--- zope.schema/branches/roger-sources/src/zope/schema/vocabulary.py	2008-08-30 14:02:23 UTC (rev 90617)
+++ zope.schema/branches/roger-sources/src/zope/schema/vocabulary.py	2008-08-30 15:34:16 UTC (rev 90618)
@@ -20,29 +20,11 @@
 from zope.schema.interfaces import IVocabularyRegistry
 from zope.schema.interfaces import IVocabulary, IVocabularyTokenized
 from zope.schema.interfaces import ITokenizedTerm, ITitledTokenizedTerm
+from zope.schema.term import SimpleTerm
 
-# simple vocabularies performing enumerated-like tasks
-
 _marker = object()
 
-class SimpleTerm(object):
-    """Simple tokenized term used by SimpleVocabulary."""
 
-    implements(ITokenizedTerm)
-
-    def __init__(self, value, token=None, title=None):
-        """Create a term for value and token. If token is omitted,
-        str(value) is used for the token.  If title is provided, 
-        term implements ITitledTokenizedTerm.
-        """
-        self.value = value
-        if token is None:
-            token = value
-        self.token = str(token)
-        self.title = title
-        if title is not None:
-            directlyProvides(self, ITitledTokenizedTerm)
-
 class SimpleVocabulary(object):
     """Vocabulary that works from a sequence of terms."""
 



More information about the Checkins mailing list