summaryrefslogtreecommitdiff
path: root/src/zope/component/tests/examples.py
blob: a2b5b58c4f89a30731d051ce92a3469912c5b9fb (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
##############################################################################
#
# 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.globalregistry import GlobalAdapterRegistry
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:

    def __init__(self, name):
        self.__name__ = name

    def __repr__(self):
        return "{}({})".format(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


base = GlobalAdapterRegistry(GlobalRegistry, 'adapters')
GlobalRegistry.adapters = base


def clear_base():
    base.__init__(GlobalRegistry, 'adapters')


@implementer(I1)
class Ob:
    def __repr__(self):
        return '<instance Ob>'


ob = Ob()


@implementer(I2)
class Ob2:
    def __repr__(self):
        return '<instance Ob2>'


@implementer(IC)
class Ob3:
    pass


@implementer(I2)
class Comp:
    def __init__(self, context):
        self.context = context


comp = Comp(1)


class ConformsToIComponentLookup:
    """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)()