[Zope-Checkins] SVN: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/ ported and reversed bridging code from Five; added fromZ3Method

Yvo Schubbe y.2005- at wcm-solutions.de
Fri Jul 8 06:33:37 EDT 2005


Log message for revision 31025:
  ported and reversed bridging code from Five; added fromZ3Method

Changed:
  A   Zope/branches/yuppie-z3z2bridge/lib/python/Interface/bridge.py
  A   Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/bridge.txt
  A   Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/framework.py
  A   Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/test_bridge.py

-=-
Added: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/bridge.py
===================================================================
--- Zope/branches/yuppie-z3z2bridge/lib/python/Interface/bridge.py	2005-07-08 10:21:51 UTC (rev 31024)
+++ Zope/branches/yuppie-z3z2bridge/lib/python/Interface/bridge.py	2005-07-08 10:33:37 UTC (rev 31025)
@@ -0,0 +1,104 @@
+##############################################################################
+#
+# Copyright (c) 2005 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.
+#
+##############################################################################
+""" Z3 -> Z2 bridge utilities.
+
+$Id$
+"""
+
+from Interface._InterfaceClass import Interface as Z2_InterfaceClass
+from Interface import Interface as Z2_Interface
+from Interface import Attribute as Z2_Attribute
+from Interface.Method import Method as Z2_Method
+
+from zope.interface.interface import InterfaceClass as Z3_InterfaceClass
+from zope.interface.interface import Interface as Z3_Interface
+from zope.interface.interface import Attribute as Z3_Attribute
+from zope.interface.interface import Method as Z3_Method
+
+
+def fromZ3Interface(z3i):
+
+    """ Return a Zope 2 interface corresponding to 'z3i'.
+
+    o 'z3i' must be a Zope 3 interface.
+    """
+    if not isinstance(z3i, Z3_InterfaceClass):
+        raise ValueError, 'Not a Zope 3 interface!'
+
+    if z3i is Z3_Interface:  # special case; root in new hierarchy!
+        return Z2_Interface
+
+    name = z3i.getName()
+
+    bases = [ fromZ3Interface(x) for x in z3i.getBases() ]
+
+    attrs = {}
+
+    for k, v in z3i.namesAndDescriptions():
+
+        if isinstance(v, Z3_Method):
+            v = fromZ3Method(v)
+
+        elif isinstance(v, Z3_Attribute):
+            v = fromZ3Attribute(v)
+
+        attrs[k] = v
+
+    # XXX: Note that we pass the original interface's __module__;
+    #      we may live to regret that.
+    return Z2_InterfaceClass(name=name,
+                             bases=bases,
+                             attrs=attrs,
+                             __doc__=z3i.getDoc(),
+                             __module__=z3i.__module__,
+                            )
+
+def fromZ3Attribute(z3a):
+
+    """ Return a Zope 2 interface attribute corresponding to 'z3a'.
+
+    o 'z3a' must be a Zope 3 interface attribute.
+    """
+    if not isinstance(z3a, Z3_Attribute):
+        raise ValueError, 'Not a Zope 3 interface attribute!'
+
+    return Z2_Attribute(z3a.getName(), z3a.getDoc())
+
+def fromZ3Method(z3m):
+
+    """ Return a Zope 2 interface method corresponding to 'z3a'.
+
+    o 'z3a' must be a Zope 3 interface method.
+    """
+    if not isinstance(z3m, Z3_Method):
+        raise ValueError, 'Not a Zope 3 interface method!'
+
+    m = Z2_Method(z3m.getName(), z3m.getDoc())
+    sig = z3m.getSignatureInfo()
+    m.positional = sig['positional']
+    m.required = sig['required']
+    m.optional = sig['optional']
+    m.varargs = sig['varargs']
+    m.kwargs = sig['kwargs']
+    return m
+
+def createZope3Bridge(zope3, package, name):
+    # Map a Zope 3 interface into a Zope2 interface, seated within 'package'
+    # as 'name'.
+    z2i = fromZ3Interface(zope3)
+
+    if name is not None:
+        z2i.__dict__['__name__'] = name
+
+    z2i.__dict__['__module__'] = package.__name__
+    setattr(package, z2i.getName(), z2i)


Property changes on: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/bridge.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/bridge.txt
===================================================================
--- Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/bridge.txt	2005-07-08 10:21:51 UTC (rev 31024)
+++ Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/bridge.txt	2005-07-08 10:33:37 UTC (rev 31025)
@@ -0,0 +1,122 @@
+======
+Bridge
+======
+
+The ``bridge`` module provides functionality to convert a Zope 3
+interface into a Zope 2 one.  First we'll import all we know about
+interfaces from the two generations:
+
+  >>> from Interface import Interface as Z2_Interface
+  >>> from Interface import Attribute as Z2_Attribute
+  >>> from Interface.Method import Method as Z2_Method
+
+  >>> from zope.interface import Interface as Z3_Interface
+  >>> from zope.interface import Attribute as Z3_Attribute
+
+
+An empty interface
+------------------
+
+  >>> class IEmpty(Z3_Interface):
+  ...     pass
+
+  >>> from Interface.bridge import fromZ3Interface
+  >>> converted = fromZ3Interface(IEmpty)
+
+  >>> Z2_Interface.isEqualOrExtendedBy(converted)
+  1
+  >>> len(converted.names())
+  0
+
+
+Attributes
+----------
+
+  >>> class IAttributes(Z3_Interface):
+  ...     one = Z3_Attribute('one', 'One attribute')
+  ...     another = Z3_Attribute('another', 'Another attribute')
+
+  >>> converted = fromZ3Interface(IAttributes)
+
+  >>> Z2_Interface.isEqualOrExtendedBy(converted)
+  1
+  >>> len(converted.names())
+  2
+  >>> 'one' in converted.names()
+  True
+  >>> 'another' in converted.names()
+  True
+
+  >>> one = converted.getDescriptionFor('one')
+  >>> isinstance(one, Z2_Attribute)
+  True
+  >>> one.getName()
+  'one'
+  >>> one.getDoc()
+  'One attribute'
+
+  >>> another = converted.getDescriptionFor('another')
+  >>> isinstance(another, Z2_Attribute)
+  True
+  >>> another.getName() 
+  'another'
+  >>> another.getDoc()
+  'Another attribute'
+
+
+Methods
+-------
+
+  >>> class IMethods(Z3_Interface):
+  ...     def one():
+  ...         """One method."""
+  ...     def another(arg1, arg2):
+  ...         """Another method, taking arguments."""
+
+  >>> converted = fromZ3Interface(IMethods)
+
+  >>> Z2_Interface.isEqualOrExtendedBy(converted)
+  1
+  >>> len(converted.names())
+  2
+  >>> 'one' in converted.names()
+  True
+  >>> 'another' in converted.names()
+  True
+
+  >>> one = converted.getDescriptionFor('one')
+  >>> isinstance(one, Z2_Method)
+  True
+  >>> one.getName()
+  'one'
+  >>> one.getDoc()
+  'One method.'
+
+  >>> another = converted.getDescriptionFor('another')
+  >>> isinstance(another, Z2_Method)
+  True
+  >>> another.getName()
+  'another'
+  >>> another.getDoc()
+  'Another method, taking arguments.'
+
+
+Invalid parameters
+------------------
+
+  >>> fromZ3Interface(None)
+  Traceback (most recent call last):
+  ...
+  ValueError: Not a Zope 3 interface!
+
+  >>> fromZ3Interface(object())
+  Traceback (most recent call last):
+  ... 
+  ValueError: Not a Zope 3 interface!
+
+  >>> class IZ2_NotAllowed(Z2_Interface):
+  ...     pass
+  >>> fromZ3Interface(IZ2_NotAllowed)
+  Traceback (most recent call last):
+  ...
+  ValueError: Not a Zope 3 interface!


Property changes on: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/bridge.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/framework.py
===================================================================
--- Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/framework.py	2005-07-08 10:21:51 UTC (rev 31024)
+++ Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/framework.py	2005-07-08 10:33:37 UTC (rev 31025)
@@ -0,0 +1,116 @@
+##############################################################################
+#
+# Copyright (c) 2005 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.
+#
+##############################################################################
+"""ZopeTestCase framework
+
+COPY THIS FILE TO YOUR 'tests' DIRECTORY.
+
+This version of framework.py will use the SOFTWARE_HOME
+environment variable to locate Zope and the Testing package.
+
+If the tests are run in an INSTANCE_HOME installation of Zope,
+Products.__path__ and sys.path with be adjusted to include the
+instance's Products and lib/python directories respectively.
+
+If you explicitly set INSTANCE_HOME prior to running the tests,
+auto-detection is disabled and the specified path will be used 
+instead.
+
+If the 'tests' directory contains a custom_zodb.py file, INSTANCE_HOME
+will be adjusted to use it.
+
+If you set the ZEO_INSTANCE_HOME environment variable a ZEO setup 
+is assumed, and you can attach to a running ZEO server (via the 
+instance's custom_zodb.py).
+
+The following code should be at the top of every test module:
+
+  import os, sys
+  if __name__ == '__main__':
+      execfile(os.path.join(sys.path[0], 'framework.py'))
+
+...and the following at the bottom:
+
+  if __name__ == '__main__':
+      framework()
+
+$Id$
+"""
+
+__version__ = '0.2.4'
+
+# Save start state
+#
+__SOFTWARE_HOME = os.environ.get('SOFTWARE_HOME', '')
+__INSTANCE_HOME = os.environ.get('INSTANCE_HOME', '')
+
+if __SOFTWARE_HOME.endswith(os.sep):
+    __SOFTWARE_HOME = os.path.dirname(__SOFTWARE_HOME)
+
+if __INSTANCE_HOME.endswith(os.sep):
+    __INSTANCE_HOME = os.path.dirname(__INSTANCE_HOME)
+
+# Find and import the Testing package
+#
+if not sys.modules.has_key('Testing'):
+    p0 = sys.path[0]
+    if p0 and __name__ == '__main__':
+        os.chdir(p0)
+        p0 = ''
+    s = __SOFTWARE_HOME
+    p = d = s and s or os.getcwd()
+    while d:
+        if os.path.isdir(os.path.join(p, 'Testing')):
+            zope_home = os.path.dirname(os.path.dirname(p))
+            sys.path[:1] = [p0, p, zope_home]
+            break
+        p, d = s and ('','') or os.path.split(p)
+    else:
+        print 'Unable to locate Testing package.',
+        print 'You might need to set SOFTWARE_HOME.'
+        sys.exit(1)
+
+import Testing, unittest
+execfile(os.path.join(os.path.dirname(Testing.__file__), 'common.py'))
+
+# Include ZopeTestCase support
+#
+if 1:   # Create a new scope
+
+    p = os.path.join(os.path.dirname(Testing.__file__), 'ZopeTestCase')
+
+    if not os.path.isdir(p):
+        print 'Unable to locate ZopeTestCase package.',
+        print 'You might need to install ZopeTestCase.'
+        sys.exit(1)
+
+    ztc_common = 'ztc_common.py'
+    ztc_common_global = os.path.join(p, ztc_common)
+
+    f = 0
+    if os.path.exists(ztc_common_global):
+        execfile(ztc_common_global)
+        f = 1
+    if os.path.exists(ztc_common):
+        execfile(ztc_common)
+        f = 1
+
+    if not f:
+        print 'Unable to locate %s.' % ztc_common
+        sys.exit(1)
+
+# Debug
+#
+print 'SOFTWARE_HOME: %s' % os.environ.get('SOFTWARE_HOME', 'Not set')
+print 'INSTANCE_HOME: %s' % os.environ.get('INSTANCE_HOME', 'Not set')
+sys.stdout.flush()
+


Property changes on: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/framework.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/test_bridge.py
===================================================================
--- Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/test_bridge.py	2005-07-08 10:21:51 UTC (rev 31024)
+++ Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/test_bridge.py	2005-07-08 10:33:37 UTC (rev 31025)
@@ -0,0 +1,27 @@
+##############################################################################
+#
+# Copyright (c) 2005 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.
+#
+##############################################################################
+""" Unit tests for Z3 -> Z2 bridge utilities.
+
+$Id$
+"""
+
+import os, sys
+if __name__ == '__main__':
+    execfile(os.path.join(sys.path[0], 'framework.py'))
+
+def test_suite():
+    from Testing.ZopeTestCase import ZopeDocFileSuite
+    return ZopeDocFileSuite('bridge.txt', package="Interface.tests")
+
+if __name__ == '__main__':
+    framework()


Property changes on: Zope/branches/yuppie-z3z2bridge/lib/python/Interface/tests/test_bridge.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Zope-Checkins mailing list