vendor/Five/1.2b-r20590

diff bridge.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line diff
     1.1 new file mode 100644
     1.2 --- /dev/null
     1.3 +++ b/bridge.py
     1.4 @@ -0,0 +1,89 @@
     1.5 +##############################################################################
     1.6 +#
     1.7 +# Copyright (c) 2004, 2005 Zope Corporation and Contributors.
     1.8 +# All Rights Reserved.
     1.9 +#
    1.10 +# This software is subject to the provisions of the Zope Public License,
    1.11 +# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
    1.12 +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
    1.13 +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    1.14 +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    1.15 +# FOR A PARTICULAR PURPOSE.
    1.16 +#
    1.17 +##############################################################################
    1.18 +""" Z2 -> Z3 bridge utilities.
    1.19 +
    1.20 +$Id: bridge.py 14504 2005-07-11 15:49:59Z yuppie $
    1.21 +"""
    1.22 +from Interface._InterfaceClass import Interface as Z2_InterfaceClass
    1.23 +from Interface import Interface as Z2_Interface
    1.24 +from Interface import Attribute as Z2_Attribute
    1.25 +from Interface.Method import Method as Z2_Method
    1.26 +
    1.27 +from zope.interface.interface import InterfaceClass as Z3_InterfaceClass
    1.28 +from zope.interface.interface import Interface as Z3_Interface
    1.29 +from zope.interface.interface import Attribute as Z3_Attribute
    1.30 +from zope.interface.interface import Method as Z3_Method
    1.31 +
    1.32 +_bridges = {Z2_Interface: Z3_Interface}
    1.33 +
    1.34 +def fromZ2Interface(z2i):
    1.35 +    """ Return a Zope 3 interface corresponding to 'z2i'.
    1.36 +
    1.37 +    o 'z2i' must be a Zope 2 interface.
    1.38 +    """
    1.39 +    if not isinstance(z2i, Z2_InterfaceClass):
    1.40 +        raise ValueError, 'Not a Zope 2 interface!'
    1.41 +
    1.42 +    if z2i in _bridges:
    1.43 +        return _bridges[z2i]
    1.44 +
    1.45 +    name = z2i.getName()
    1.46 +    bases = [ fromZ2Interface(x) for x in z2i.getBases() ]
    1.47 +    attrs = {}
    1.48 +
    1.49 +    for k, v in z2i.namesAndDescriptions():
    1.50 +        if isinstance(v, Z2_Method):
    1.51 +            v = fromZ2Method(v)
    1.52 +
    1.53 +        elif isinstance(v, Z2_Attribute):
    1.54 +            v = fromZ2Attribute(v)
    1.55 +
    1.56 +        attrs[k] = v
    1.57 +
    1.58 +    # XXX: Note that we pass the original interface's __module__;
    1.59 +    #      we may live to regret that.
    1.60 +    z3i = Z3_InterfaceClass(name=name,
    1.61 +                            bases=tuple(bases),
    1.62 +                            attrs=attrs,
    1.63 +                            __doc__=z2i.getDoc(),
    1.64 +                            __module__=z2i.__module__)
    1.65 +    _bridges[z2i] = z3i
    1.66 +    return z3i
    1.67 +
    1.68 +def fromZ2Attribute(z2a):
    1.69 +    """ Return a Zope 3 interface attribute corresponding to 'z2a'.
    1.70 +
    1.71 +    o 'z2a' must be a Zope 2 interface attribute.
    1.72 +    """
    1.73 +    if not isinstance(z2a, Z2_Attribute):
    1.74 +        raise ValueError, 'Not a Zope 2 interface attribute!'
    1.75 +
    1.76 +    return Z3_Attribute(z2a.getName(), z2a.getDoc())
    1.77 +
    1.78 +def fromZ2Method(z2m):
    1.79 +    """ Return a Zope 3 interface method corresponding to 'z2m'.
    1.80 +
    1.81 +    o 'z2m' must be a Zope 2 interface method.
    1.82 +    """
    1.83 +    if not isinstance(z2m, Z2_Method):
    1.84 +        raise ValueError, 'Not a Zope 2 interface method!'
    1.85 +
    1.86 +    z3m = Z3_Method(z2m.getName(), z2m.getDoc())
    1.87 +    sig = z2m.getSignatureInfo()
    1.88 +    z3m.positional = sig['positional']
    1.89 +    z3m.required = sig['required']
    1.90 +    z3m.optional = sig['optional']
    1.91 +    z3m.varargs = sig['varargs']
    1.92 +    z3m.kwargs = sig['kwargs']
    1.93 +    return z3m