[Checkins] SVN: zope.generic/trunk/src/zope/generic/ few small api changes

Dominik Huber dominik.huber at perse.ch
Mon Apr 10 11:14:44 EDT 2006


Log message for revision 66789:
  few small api changes

Changed:
  A   zope.generic/trunk/src/zope/generic/component/README.txt
  U   zope.generic/trunk/src/zope/generic/component/interfaces.py
  A   zope.generic/trunk/src/zope/generic/component/tests.py
  U   zope.generic/trunk/src/zope/generic/configuration/api.py
  U   zope.generic/trunk/src/zope/generic/configuration/base.py
  U   zope.generic/trunk/src/zope/generic/configuration/helper.py
  U   zope.generic/trunk/src/zope/generic/configurator/interfaces.py
  A   zope.generic/trunk/src/zope/generic/type/EXAMPLE.txt
  U   zope.generic/trunk/src/zope/generic/type/README.txt
  U   zope.generic/trunk/src/zope/generic/type/adapter.py
  U   zope.generic/trunk/src/zope/generic/type/tests.py

-=-
Added: zope.generic/trunk/src/zope/generic/component/README.txt
===================================================================
--- zope.generic/trunk/src/zope/generic/component/README.txt	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/component/README.txt	2006-04-10 15:14:43 UTC (rev 66789)
@@ -0,0 +1,9 @@
+=========
+Component
+=========
+
+The generic package relies heavily on interfaces. Often we have relation between
+different interfaces. One component does provide serveral interfaces so 
+Informations provides dedicated data about an certain 
+marker interface. Informations are implemented as utility. The package provides
+some convenience functions and directives.
\ No newline at end of file


Property changes on: zope.generic/trunk/src/zope/generic/component/README.txt
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: zope.generic/trunk/src/zope/generic/component/interfaces.py
===================================================================
--- zope.generic/trunk/src/zope/generic/component/interfaces.py	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/component/interfaces.py	2006-04-10 15:14:43 UTC (rev 66789)
@@ -27,12 +27,7 @@
 
 
 
-class IInterfaceKeyed(Interface):
-    """Provide an interface key by implementation or adaption."""
-
-
-
-class IInterfaceKey(IInterfaceKeyed):
+class IInterfaceKey(Interface):
     """Declare an interface as information-specific-key."""
 
     interface = Object(

Added: zope.generic/trunk/src/zope/generic/component/tests.py
===================================================================
--- zope.generic/trunk/src/zope/generic/component/tests.py	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/component/tests.py	2006-04-10 15:14:43 UTC (rev 66789)
@@ -0,0 +1,44 @@
+##############################################################################
+#
+# Copyright (c) 2005, 2006 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.
+#
+##############################################################################
+
+"""
+$Id$
+"""
+
+import unittest
+
+from zope import component
+from zope import interface
+from zope.testing import doctest
+
+from zope.generic.configuration.testing import placelesssetup
+from zope.generic.testing.testing import registerDirective
+
+from zope.generic.component import testing
+
+
+
+def test_suite():
+    return unittest.TestSuite((
+        doctest.DocFileSuite('README.txt',
+                             setUp=placelesssetup.setUp,
+                             tearDown=placelesssetup.tearDown,
+                             globs={'component': component, 'interface': interface,
+                             'registerDirective': registerDirective,
+                             'testing': testing},
+                             optionflags=doctest.NORMALIZE_WHITESPACE+
+                                            doctest.ELLIPSIS),
+        ))
+
+if __name__ == '__main__': unittest.main()


Property changes on: zope.generic/trunk/src/zope/generic/component/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: zope.generic/trunk/src/zope/generic/configuration/api.py
===================================================================
--- zope.generic/trunk/src/zope/generic/configuration/api.py	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/configuration/api.py	2006-04-10 15:14:43 UTC (rev 66789)
@@ -19,6 +19,7 @@
 # usage see README.txt
 from zope.generic.configuration.base import ConfigurationData
 from zope.generic.configuration.interfaces import *
+from zope.generic.configuration.helper import deleteConfigurationData
 from zope.generic.configuration.helper import provideConfigurationData
 from zope.generic.configuration.helper import queryConfigurationData
 from zope.generic.configuration.helper import queryConfigurationHandler

Modified: zope.generic/trunk/src/zope/generic/configuration/base.py
===================================================================
--- zope.generic/trunk/src/zope/generic/configuration/base.py	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/configuration/base.py	2006-04-10 15:14:43 UTC (rev 66789)
@@ -74,7 +74,7 @@
         >>> config_data = ConfigurationData(IFooConfiguration, {})
         Traceback (most recent call last):
         ...
-        KeyError: 'Missed keys: foo.'
+        AttributeError: 'IFooConfiguration' object has no attribute 'foo'.
 
     The schema should not contain methods:
 
@@ -94,15 +94,15 @@
 
     def __init__(self, schema, data):
         # preconditions
-        missedKeys = []
+        missedArguments = []
         for name in schema:
             if name not in data:
                 field = schema[name]
                 if field.required is True:
-                    missedKeys.append(name)
+                    missedArguments.append(name)
         
-        if missedKeys:
-            raise KeyError('Missed keys: %s.' % ', '.join(missedKeys))
+        if missedArguments:
+            raise AttributeError("'%s' object has no attribute '%s'." % (schema.__name__, ', '.join(missedArguments)))
     
         # essentials
         self.__dict__['_ConfigurationData__data'] = PersistentDict(data)
@@ -129,7 +129,7 @@
                 v = lambda: value
             else:
                 v = value
-            # setattr(self, name, v)
+            #setattr(self, name, v)
             return v
         raise AttributeError(name)
 
@@ -145,7 +145,7 @@
             else:
                 if field.readonly is True:
                     raise ValueError(name, 'Data is readonly.')
-            data['name'] = value
+            data[name] = value
         else:
             super(ConfigurationData, self).__setattr__(name, value)
 

Modified: zope.generic/trunk/src/zope/generic/configuration/helper.py
===================================================================
--- zope.generic/trunk/src/zope/generic/configuration/helper.py	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/configuration/helper.py	2006-04-10 15:14:43 UTC (rev 66789)
@@ -55,6 +55,7 @@
         return interface(configurations, default)
 
 
+
 def provideConfigurationData(context, interface, data):
     """Set configuration data into the context."""
     from zope.generic.configuration.base import ConfigurationData 
@@ -66,6 +67,15 @@
 
 
 
+def deleteConfigurationData(context, interface):
+    """Delete configuration from context."""
+    
+    configurations = IConfigurations(context, None)
+    if configurations:
+        del configurations[interface]
+
+
+
 def queryConfigurationHandler(interface, default=None):
     # cyclic import :(
     from zope.generic.information.api import queryInformation

Modified: zope.generic/trunk/src/zope/generic/configurator/interfaces.py
===================================================================
--- zope.generic/trunk/src/zope/generic/configurator/interfaces.py	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/configurator/interfaces.py	2006-04-10 15:14:43 UTC (rev 66789)
@@ -48,7 +48,6 @@
 
 
 
-
 class IInitializer(IConfigurator):
     """Initialize an object."""
 

Added: zope.generic/trunk/src/zope/generic/type/EXAMPLE.txt
===================================================================
--- zope.generic/trunk/src/zope/generic/type/EXAMPLE.txt	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/type/EXAMPLE.txt	2006-04-10 15:14:43 UTC (rev 66789)
@@ -0,0 +1,185 @@
+=====================
+Configuration example
+=====================
+
+The example defines an article. The article has a text and a note part.
+
+
+Step 1: Logical Domain Model
+----------------------------
+
+Define the logical domain types and their inheritance. The logical model of our 
+domain consist of three types. We use marker interfaces to show their 
+inheritance relationships.
+
+    >>> class IText(interface.Interface):
+    ...     """Logical text marker"""
+
+    >>> class INote(interface.Interface):
+    ...     """Logical note marker"""
+
+    >>> class IArticle(IText, INote):
+    ...     """Logical article marker"""
+    
+
+Step 2: Building Blocks
+-----------------------
+
+Specify the building blocks. In this example the 
+
+
+In the generic model these components may be configurations or annotations.
+
+    >>> class ITextConfig(interface.Interface):
+    ...     """The text configuration"""
+    ...     body = schema.Text(title=u"Text body")
+
+    >>> class INoteConfig(interface.Interface):
+    ...     """The note configuration"""
+    ...     body = schema.Text(title=u"Text body")
+
+    >>> class IArticleInitializationConfiguration(interface.Interface):
+    ...     """The articleconfiguration used for initialization only"""
+    ...     text = schema.Text(title=u"Text body")
+    ...     note = schema.Text(title=u"Note body", required=False)
+
+    >>> registerDirective('''
+    ... <generic:configuration
+    ...     interface="example.ITextConfig"
+    ...     />
+    ... ''') 
+
+    >>> registerDirective('''
+    ... <generic:configuration
+    ...     interface="example.INoteConfig"
+    ...     />
+    ... ''')
+
+    >>> registerDirective('''
+    ... <generic:configuration
+    ...     interface="example.IArticleInitializationConfiguration"
+    ...     />
+    ... ''')
+
+Step 3: Implement Specific Handlers and the type-specific default data
+----------------------------------------------------------------------
+
+    >>> from zope.generic.configuration.api import queryConfigurationData
+    >>> from zope.generic.configuration.api import provideConfigurationData
+    >>> from zope.generic.configuration.api import deleteConfigurationData
+    >>> from zope.generic.type.api import queryTypeConfiguration
+
+Let's implement the initializer of the article. Initializers are called from
+``__init__``. Initializers are used to add additional logic at the 
+initialization process.
+
+    >>> def articleInitializer(context, *pos, **kws):
+    ...     """Initializer Policy chosen:
+    ...     - look up article configuration
+    ...     - if there isn't any defined use the base types configuration
+    ...     - raises an exception if no configurationis defined
+    ...     """
+    ...     # looks up the allready configured data on the object
+    ...     article_config = queryConfigurationData(context, 
+    ...         IArticleInitializationConfiguration)
+    ...     if article_config is None:
+    ...         text_config = queryTypeConfiguration(context, ITextConfig)
+    ...         note_config = queryTypeConfiguration(context, INoteConfig)
+    ...         text, note = text_config.text, note_config.text
+    ...     else:
+    ...         text, note = article_config.text, article_config.note
+    ...
+    ...     provideConfigurationData(context, ITextConfig, {'body': text})
+    ...     provideConfigurationData(context, INoteConfig, {'body': note})
+    ...     deleteConfigurationData(context, IArticleInitializationConfiguration)
+ 
+    >>> from zope.generic.configuration.api import ConfigurationData
+    
+    >>> textDefaults = ConfigurationData(ITextConfig, 
+    ...     {'body': u"This is the default text."})
+
+    >>> noteDefaults = ConfigurationData(INoteConfig, 
+    ...     {'body': u"This is the default note."})
+
+    >>> articleTextDefaults = ConfigurationData(ITextConfig, 
+    ...     {'body': u"This is the default article."})
+    >>> articleNoteDefaults = ConfigurationData(INoteConfig, 
+    ...     {'body': u""})
+
+
+
+    >>> registerDirective('''
+    ... <generic:type
+    ...     interface="example.IText"
+    ...     class='zope.generic.type.api.Object'
+    ...     >
+    ...    <initializer
+    ...         interface='example.ITextConfig'
+    ...    />
+    ...    <configuration
+    ...        interface='example.ITextConfig'
+    ...        data='example.textDefaults'
+    ...    />
+    ... </generic:type>
+    ... ''')
+
+    >>> registerDirective('''
+    ... <generic:type
+    ...     interface="example.INote"
+    ...     class='zope.generic.type.api.Object'
+    ...     >
+    ...    <initializer
+    ...         interface='example.INoteConfig'
+    ...    />
+    ...    <configuration
+    ...        interface='example.INoteConfig'
+    ...        data='example.noteDefaults'
+    ...    />
+    ... </generic:type>
+    ... ''')
+
+    >>> registerDirective('''
+    ... <generic:type
+    ...     interface="example.IArticle"
+    ...     class='zope.generic.type.api.Object'
+    ...     >
+    ...    <initializer
+    ...         interface='example.IArticleInitializationConfiguration'
+    ...         handler="example.articleInitializer"
+    ...    />
+    ...    <configuration
+    ...        interface='example.ITextConfig'
+    ...        data='example.articleTextDefaults'
+    ...    />
+    ...    <configuration
+    ...        interface='example.INoteConfig'
+    ...        data='example.articleNoteDefaults'
+    ...    />
+    ... </generic:type>
+    ... ''')
+
+
+
+Step 4: Enjoy your logical domain...
+------------------------------------
+
+To create an article we use the logical domain interface
+
+    >>> from zope.generic.type.api import createObject
+
+    >>> article = createObject(IArticle, text="First version of article.")
+    
+    >>> IArticle.providedBy(article)
+    True
+
+    >>> queryConfigurationData(article, ITextConfig).body
+    'First version of article.'
+    
+    >>> queryConfigurationData(article, IArticleInitializationConfiguration).text
+    Traceback (most recent call last):
+    ...
+    AttributeError: 'NoneType' object has no attribute 'text'
+    
+    #>>> IText(article, None)
+    #>>> INote(article, None)
+    
\ No newline at end of file


Property changes on: zope.generic/trunk/src/zope/generic/type/EXAMPLE.txt
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: zope.generic/trunk/src/zope/generic/type/README.txt
===================================================================
--- zope.generic/trunk/src/zope/generic/type/README.txt	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/type/README.txt	2006-04-10 15:14:43 UTC (rev 66789)
@@ -178,9 +178,9 @@
 	>>> bar = api.createObject(IBarMarker)
 	Traceback (most recent call last):
 	...
-	KeyError: 'Missed keys: other.'
+	AttributeError: 'IOtherConfiguration' object has no attribute 'other'.
 
-	>>> bar = api.createObject(IBarMarker, **{'other': u'Specific initialization data.'})
+	>>> bar = api.createObject(IBarMarker, other=u'Specific initialization data.')
 	Initializing ...
 
 This registration attached the specific configuration to the type information.

Modified: zope.generic/trunk/src/zope/generic/type/adapter.py
===================================================================
--- zope.generic/trunk/src/zope/generic/type/adapter.py	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/type/adapter.py	2006-04-10 15:14:43 UTC (rev 66789)
@@ -55,5 +55,6 @@
                 provideConfigurationData(self.context, config.interface, kws)
 
             # invoke initialization handler
+
             if config.handler:
                 config.handler(self.context, *pos, **kws)

Modified: zope.generic/trunk/src/zope/generic/type/tests.py
===================================================================
--- zope.generic/trunk/src/zope/generic/type/tests.py	2006-04-10 15:10:56 UTC (rev 66788)
+++ zope.generic/trunk/src/zope/generic/type/tests.py	2006-04-10 15:14:43 UTC (rev 66789)
@@ -20,6 +20,7 @@
 
 from zope import component
 from zope import interface
+from zope import schema
 from zope.testing import doctest
 
 
@@ -40,6 +41,14 @@
                              'testing': testing},
                              optionflags=doctest.NORMALIZE_WHITESPACE+
                                             doctest.ELLIPSIS),
+        doctest.DocFileSuite('EXAMPLE.txt',
+                             setUp=testing.placelesssetup.setUp,
+                             tearDown=testing.placelesssetup.tearDown,
+                             globs={'component': component, 'interface': interface,
+                             'schema': schema, 'registerDirective': registerDirective,
+                             'testing': testing},
+                             optionflags=doctest.NORMALIZE_WHITESPACE+
+                                            doctest.ELLIPSIS),
         ))
 
 if __name__ == '__main__': unittest.main()



More information about the Checkins mailing list