• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / zope.component / 3827624917

pending completion
3827624917

push

github

GitHub
Update src/zope/component/tests/test_interface.py

467 of 489 branches covered (95.5%)

Branch coverage included in aggregate %.

4620 of 4625 relevant lines covered (99.89%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.88
/src/zope/component/_api.py
1
##############################################################################
2
#
3
# Copyright (c) 2001, 2002 Zope Foundation 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
"""Zope 3 Component Architecture
1✔
15
"""
16
import sys
1✔
17

18
from zope.hookable import hookable
1✔
19
from zope.interface import Interface
1✔
20
from zope.interface.interfaces import ComponentLookupError
1✔
21
from zope.interface.interfaces import IComponentLookup
1✔
22

23
from zope.component.interfaces import IFactory
1✔
24
from zope.component.interfaces import inherits_arch_docs as inherits_docs
1✔
25

26

27
# getSiteManager() returns a component registry.  Although the term
28
# "site manager" is deprecated in favor of "component registry",
29
# the old term is kept around to maintain a stable API.
30
base = None
1✔
31
@hookable
1✔
32
@inherits_docs
1✔
33
def getSiteManager(context=None):
1✔
34
    """ See IComponentArchitecture.
35
    """
36
    global base
37
    if context is None:
1✔
38
        if base is None:
1✔
39
            from zope.component.globalregistry import base
1✔
40
        return base
1✔
41
    else:
42
        # Use the global site manager to adapt context to `IComponentLookup`
43
        # to avoid the recursion implied by using a local `getAdapter()` call.
44
        try:
1✔
45
            return IComponentLookup(context)
1✔
46
        except TypeError as error:
1✔
47
            raise ComponentLookupError(*error.args)
1✔
48

49
# Adapter API
50
@inherits_docs
1✔
51
def getAdapterInContext(object, interface, context):
1✔
52
    adapter = queryAdapterInContext(object, interface, context)
1✔
53
    if adapter is None:
1✔
54
        raise ComponentLookupError(object, interface)
1✔
55
    return adapter
1✔
56

57
@inherits_docs
1✔
58
def queryAdapterInContext(object, interface, context, default=None):
1✔
59
    conform = getattr(object, '__conform__', None)
1✔
60
    if conform is not None:
1✔
61
        try:
1✔
62
            adapter = conform(interface)
1✔
63
        except TypeError:
1✔
64
            # We got a TypeError. It might be an error raised by
65
            # the __conform__ implementation, or *we* may have
66
            # made the TypeError by calling an unbound method
67
            # (object is a class).  In the later case, we behave
68
            # as though there is no __conform__ method. We can
69
            # detect this case by checking whether there is more
70
            # than one traceback object in the traceback chain:
71
            if sys.exc_info()[2].tb_next is not None:
1✔
72
                # There is more than one entry in the chain, so
73
                # reraise the error:
74
                raise
1✔
75
            # This clever trick is from Phillip Eby
76
        else:
77
            if adapter is not None:
1✔
78
                return adapter
1✔
79

80
    if interface.providedBy(object):
1✔
81
        return object
1✔
82

83
    return getSiteManager(context).queryAdapter(object, interface, '', default)
1✔
84

85
@inherits_docs
1✔
86
def getAdapter(object, interface=Interface, name=u'', context=None):
1✔
87
    adapter = queryAdapter(object, interface, name, None, context)
1✔
88
    if adapter is None:
1✔
89
        raise ComponentLookupError(object, interface, name)
1✔
90
    return adapter
1✔
91

92
@inherits_docs
1✔
93
def queryAdapter(object, interface=Interface, name=u'', default=None,
1✔
94
                 context=None):
95
    if context is None:
1✔
96
        return adapter_hook(interface, object, name, default)
1✔
97
    return getSiteManager(context).queryAdapter(object, interface, name,
1✔
98
                                                default)
99

100
@inherits_docs
1✔
101
def getMultiAdapter(objects, interface=Interface, name=u'', context=None):
1✔
102
    adapter = queryMultiAdapter(objects, interface, name, context=context)
1✔
103
    if adapter is None:
1✔
104
        raise ComponentLookupError(objects, interface, name)
1✔
105
    return adapter
1✔
106

107
@inherits_docs
1✔
108
def queryMultiAdapter(objects, interface=Interface, name=u'', default=None,
1✔
109
                      context=None):
110
    try:
1✔
111
        sitemanager = getSiteManager(context)
1✔
112
    except ComponentLookupError:
1✔
113
        # Oh blast, no site manager. This should *never* happen!
114
        return default
1✔
115

116
    return sitemanager.queryMultiAdapter(objects, interface, name, default)
1✔
117

118
@inherits_docs
1✔
119
def getAdapters(objects, provided, context=None):
1✔
120
    try:
1✔
121
        sitemanager = getSiteManager(context)
1✔
122
    except ComponentLookupError:
1✔
123
        # Oh blast, no site manager. This should *never* happen!
124
        return []
1✔
125
    return sitemanager.getAdapters(objects, provided)
1✔
126

127
@inherits_docs
1✔
128
def subscribers(objects, interface, context=None):
1✔
129
    try:
1✔
130
        sitemanager = getSiteManager(context)
1✔
131
    except ComponentLookupError:
1✔
132
        # Oh blast, no site manager. This should *never* happen!
133
        return []
1✔
134
    return sitemanager.subscribers(objects, interface)
1✔
135

136
@inherits_docs
1✔
137
def handle(*objects):
1✔
138
    getSiteManager(None).subscribers(objects, None)
1✔
139

140
#############################################################################
141
# Register the component architectures adapter hook, with the adapter hook
142
# registry of the `zope.inteface` package. This way we will be able to call
143
# interfaces to create adapters for objects. For example, `I1(ob)` is
144
# equvalent to `getAdapterInContext(I1, ob, '')`.
145
@hookable
1✔
146
def adapter_hook(interface, object, name='', default=None):
1✔
147
    try:
1✔
148
        sitemanager = getSiteManager()
1✔
149
    except ComponentLookupError: #pragma NO COVER w/o context, cannot test
150
        # Oh blast, no site manager. This should *never* happen!
151
        return None
152
    return sitemanager.queryAdapter(object, interface, name, default)
1✔
153

154
import zope.interface.interface
1✔
155
zope.interface.interface.adapter_hooks.append(adapter_hook)
1✔
156
#############################################################################
157

158

159
# Utility API
160
@inherits_docs
1✔
161
def getUtility(interface, name='', context=None):
1✔
162
    utility = queryUtility(interface, name, context=context)
1✔
163
    if utility is not None:
1✔
164
        return utility
1✔
165
    raise ComponentLookupError(interface, name)
1✔
166

167
@inherits_docs
1✔
168
def queryUtility(interface, name='', default=None, context=None):
1✔
169
    return getSiteManager(context).queryUtility(interface, name, default)
1✔
170

171
@inherits_docs
1✔
172
def getUtilitiesFor(interface, context=None):
1✔
173
    return getSiteManager(context).getUtilitiesFor(interface)
1✔
174

175
@inherits_docs
1✔
176
def getAllUtilitiesRegisteredFor(interface, context=None):
1✔
177
    return getSiteManager(context).getAllUtilitiesRegisteredFor(interface)
1✔
178

179

180
_marker = object()
1✔
181

182
@inherits_docs
1✔
183
def queryNextUtility(context, interface, name='', default=None):
1✔
184
    """Query for the next available utility.
185

186
    Find the next available utility providing `interface` and having the
187
    specified name. If no utility was found, return the specified `default`
188
    value.
189
    """
190
    try:
1✔
191
        sm = getSiteManager(context)
1✔
192
    except ComponentLookupError:
1✔
193
        return default
1✔
194
    bases = sm.__bases__
1✔
195
    for base in bases:
1✔
196
        util = base.queryUtility(interface, name, _marker)
1✔
197
        if util is not _marker:
1✔
198
            return util
1✔
199
    return default
1✔
200

201
@inherits_docs
1✔
202
def getNextUtility(context, interface, name=''):
1✔
203
    """Get the next available utility.
204

205
    If no utility was found, a `ComponentLookupError` is raised.
206
    """
207
    util = queryNextUtility(context, interface, name, _marker)
1✔
208
    if util is _marker:
1✔
209
        raise ComponentLookupError(
1✔
210
            "No more utilities for %s, '%s' have been found." % (
211
                interface, name))
212
    return util
1✔
213

214

215
# Factories
216

217
@inherits_docs
1✔
218
def createObject(__factory_name, *args, **kwargs):
1✔
219
    """Invoke the named factory and return the result.
220

221
    ``__factory_name`` is a positional-only argument.
222
    """
223
    context = kwargs.pop('context', None)
1✔
224
    return getUtility(IFactory, __factory_name, context)(*args, **kwargs)
1✔
225

226
@inherits_docs
1✔
227
def getFactoryInterfaces(name, context=None):
1✔
228
    """Return the interface provided by the named factory's objects
229

230
    Result might be a single interface. XXX
231
    """
232
    return getUtility(IFactory, name, context).getInterfaces()
1✔
233

234
@inherits_docs
1✔
235
def getFactoriesFor(interface, context=None):
1✔
236
    """Return info on all factories implementing the given interface.
237
    """
238
    utils = getSiteManager(context)
1✔
239
    for (name, factory) in utils.getUtilitiesFor(IFactory):
1✔
240
        interfaces = factory.getInterfaces()
1✔
241
        try:
1✔
242
            if interfaces.isOrExtends(interface):
1!
243
                yield name, factory
1✔
244
        except AttributeError:
1✔
245
            for iface in interfaces:
1!
246
                if iface.isOrExtends(interface):
1✔
247
                    yield name, factory
1✔
248
                    break
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc