vendor/Five/1.2b-r20590

annotate bridge.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
rev   line source
fguillaume@0 1 ##############################################################################
fguillaume@0 2 #
fguillaume@0 3 # Copyright (c) 2004, 2005 Zope Corporation and Contributors.
fguillaume@0 4 # All Rights Reserved.
fguillaume@0 5 #
fguillaume@0 6 # This software is subject to the provisions of the Zope Public License,
fguillaume@0 7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
fguillaume@0 8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
fguillaume@0 9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
fguillaume@0 10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
fguillaume@0 11 # FOR A PARTICULAR PURPOSE.
fguillaume@0 12 #
fguillaume@0 13 ##############################################################################
fguillaume@0 14 """ Z2 -> Z3 bridge utilities.
fguillaume@0 15
fguillaume@0 16 $Id: bridge.py 14504 2005-07-11 15:49:59Z yuppie $
fguillaume@0 17 """
fguillaume@0 18 from Interface._InterfaceClass import Interface as Z2_InterfaceClass
fguillaume@0 19 from Interface import Interface as Z2_Interface
fguillaume@0 20 from Interface import Attribute as Z2_Attribute
fguillaume@0 21 from Interface.Method import Method as Z2_Method
fguillaume@0 22
fguillaume@0 23 from zope.interface.interface import InterfaceClass as Z3_InterfaceClass
fguillaume@0 24 from zope.interface.interface import Interface as Z3_Interface
fguillaume@0 25 from zope.interface.interface import Attribute as Z3_Attribute
fguillaume@0 26 from zope.interface.interface import Method as Z3_Method
fguillaume@0 27
fguillaume@0 28 _bridges = {Z2_Interface: Z3_Interface}
fguillaume@0 29
fguillaume@0 30 def fromZ2Interface(z2i):
fguillaume@0 31 """ Return a Zope 3 interface corresponding to 'z2i'.
fguillaume@0 32
fguillaume@0 33 o 'z2i' must be a Zope 2 interface.
fguillaume@0 34 """
fguillaume@0 35 if not isinstance(z2i, Z2_InterfaceClass):
fguillaume@0 36 raise ValueError, 'Not a Zope 2 interface!'
fguillaume@0 37
fguillaume@0 38 if z2i in _bridges:
fguillaume@0 39 return _bridges[z2i]
fguillaume@0 40
fguillaume@0 41 name = z2i.getName()
fguillaume@0 42 bases = [ fromZ2Interface(x) for x in z2i.getBases() ]
fguillaume@0 43 attrs = {}
fguillaume@0 44
fguillaume@0 45 for k, v in z2i.namesAndDescriptions():
fguillaume@0 46 if isinstance(v, Z2_Method):
fguillaume@0 47 v = fromZ2Method(v)
fguillaume@0 48
fguillaume@0 49 elif isinstance(v, Z2_Attribute):
fguillaume@0 50 v = fromZ2Attribute(v)
fguillaume@0 51
fguillaume@0 52 attrs[k] = v
fguillaume@0 53
fguillaume@0 54 # XXX: Note that we pass the original interface's __module__;
fguillaume@0 55 # we may live to regret that.
fguillaume@0 56 z3i = Z3_InterfaceClass(name=name,
fguillaume@0 57 bases=tuple(bases),
fguillaume@0 58 attrs=attrs,
fguillaume@0 59 __doc__=z2i.getDoc(),
fguillaume@0 60 __module__=z2i.__module__)
fguillaume@0 61 _bridges[z2i] = z3i
fguillaume@0 62 return z3i
fguillaume@0 63
fguillaume@0 64 def fromZ2Attribute(z2a):
fguillaume@0 65 """ Return a Zope 3 interface attribute corresponding to 'z2a'.
fguillaume@0 66
fguillaume@0 67 o 'z2a' must be a Zope 2 interface attribute.
fguillaume@0 68 """
fguillaume@0 69 if not isinstance(z2a, Z2_Attribute):
fguillaume@0 70 raise ValueError, 'Not a Zope 2 interface attribute!'
fguillaume@0 71
fguillaume@0 72 return Z3_Attribute(z2a.getName(), z2a.getDoc())
fguillaume@0 73
fguillaume@0 74 def fromZ2Method(z2m):
fguillaume@0 75 """ Return a Zope 3 interface method corresponding to 'z2m'.
fguillaume@0 76
fguillaume@0 77 o 'z2m' must be a Zope 2 interface method.
fguillaume@0 78 """
fguillaume@0 79 if not isinstance(z2m, Z2_Method):
fguillaume@0 80 raise ValueError, 'Not a Zope 2 interface method!'
fguillaume@0 81
fguillaume@0 82 z3m = Z3_Method(z2m.getName(), z2m.getDoc())
fguillaume@0 83 sig = z2m.getSignatureInfo()
fguillaume@0 84 z3m.positional = sig['positional']
fguillaume@0 85 z3m.required = sig['required']
fguillaume@0 86 z3m.optional = sig['optional']
fguillaume@0 87 z3m.varargs = sig['varargs']
fguillaume@0 88 z3m.kwargs = sig['kwargs']
fguillaume@0 89 return z3m