vendor/Five/1.2b-r20590

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