1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
##############################################################################
#
# Copyright (c) 2001, 2002, 2009 Zope Foundation 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.
#
##############################################################################
"""Examples supporting Sphinx doctest snippets.
"""
import sys
from zope.interface import Interface
from zope.interface import implementer
from zope.interface.interfaces import IInterface
from zope.component._declaration import adapter
from zope.component.testfiles.views import IC
def write(x):
sys.stdout.write('%s\n' % x)
class ITestType(IInterface):
pass
class I1(Interface):
pass
class I2(Interface):
pass
class I3(Interface):
pass
class I4(Interface):
pass
class IGI(Interface):
pass
class IQI(Interface):
pass
class ISI(Interface):
pass
class ISII(Interface):
pass
class U(object):
def __init__(self, name):
self.__name__ = name
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, self.__name__)
@implementer(I1)
class U1(U):
pass
@implementer(I1, I2)
class U12(U):
pass
@adapter(I1)
def handle1(x):
write('handle1 %s' % x)
def handle2(*objects):
write( 'handle2 ' + repr(objects))
@adapter(I1)
def handle3(x):
write( 'handle3 %s' % x)
@adapter(I1)
def handle4(x):
write( 'handle4 %s' % x)
class GlobalRegistry:
pass
from zope.component.globalregistry import GlobalAdapterRegistry
base = GlobalAdapterRegistry(GlobalRegistry, 'adapters')
GlobalRegistry.adapters = base
def clear_base():
base.__init__(GlobalRegistry, 'adapters')
@implementer(I1)
class Ob(object):
def __repr__(self):
return '<instance Ob>'
ob = Ob()
@implementer(I2)
class Ob2(object):
def __repr__(self):
return '<instance Ob2>'
@implementer(IC)
class Ob3(object):
pass
@implementer(I2)
class Comp(object):
def __init__(self, context):
self.context = context
comp = Comp(1)
class ConformsToIComponentLookup(object):
"""Allow a dummy sitemanager to conform/adapt to `IComponentLookup`."""
def __init__(self, sitemanager):
self.sitemanager = sitemanager
def __conform__(self, interface):
"""This method is specified by the adapter PEP to do the adaptation."""
from zope.interface.interfaces import IComponentLookup
if interface is IComponentLookup:
return self.sitemanager
def clearZCML(test=None):
from zope.configuration.xmlconfig import XMLConfig
import zope.component
from zope.component.testing import setUp
from zope.component.testing import tearDown
tearDown()
setUp()
XMLConfig('meta.zcml', zope.component)()
|