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

zopefoundation / zope.component / 4686002484

pending completion
4686002484

push

github

Michael Howitz
Fix too long line.

460 of 475 branches covered (96.84%)

Branch coverage included in aggregate %.

4646 of 4646 relevant lines covered (100.0%)

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
import zope.interface.interface
1✔
19
from zope.hookable import hookable
1✔
20
from zope.interface import Interface
1✔
21
from zope.interface.interfaces import ComponentLookupError
1✔
22
from zope.interface.interfaces import IComponentLookup
1✔
23

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

27

28
# getSiteManager() returns a component registry.  Although the term
29
# "site manager" is deprecated in favor of "component registry",
30
# the old term is kept around to maintain a stable API.
31
base = None
1✔
32

33

34
@hookable
1✔
35
@inherits_docs
1✔
36
def getSiteManager(context=None):
1✔
37
    """ See IComponentArchitecture.
38
    """
39
    global base
40
    if context is None:
1✔
41
        if base is None:
1✔
42
            from zope.component.globalregistry import base
1✔
43
        return base
1✔
44
    else:
45
        # Use the global site manager to adapt context to `IComponentLookup`
46
        # to avoid the recursion implied by using a local `getAdapter()` call.
47
        try:
1✔
48
            return IComponentLookup(context)
1✔
49
        except TypeError as error:
1✔
50
            raise ComponentLookupError(*error.args)
1✔
51

52
# Adapter API
53

54

55
@inherits_docs
1✔
56
def getAdapterInContext(object, interface, context):
1✔
57
    adapter = queryAdapterInContext(object, interface, context)
1✔
58
    if adapter is None:
1✔
59
        raise ComponentLookupError(object, interface)
1✔
60
    return adapter
1✔
61

62

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

86
    if interface.providedBy(object):
1✔
87
        return object
1✔
88

89
    return getSiteManager(context).queryAdapter(object, interface, '', default)
1✔
90

91

92
@inherits_docs
1✔
93
def getAdapter(object, interface=Interface, name='', context=None):
1✔
94
    adapter = queryAdapter(object, interface, name, None, context)
1✔
95
    if adapter is None:
1✔
96
        raise ComponentLookupError(object, interface, name)
1✔
97
    return adapter
1✔
98

99

100
@inherits_docs
1✔
101
def queryAdapter(object, interface=Interface, name='', default=None,
1✔
102
                 context=None):
103
    if context is None:
1✔
104
        return adapter_hook(interface, object, name, default)
1✔
105
    return getSiteManager(context).queryAdapter(object, interface, name,
1✔
106
                                                default)
107

108

109
@inherits_docs
1✔
110
def getMultiAdapter(objects, interface=Interface, name='', context=None):
1✔
111
    adapter = queryMultiAdapter(objects, interface, name, context=context)
1✔
112
    if adapter is None:
1✔
113
        raise ComponentLookupError(objects, interface, name)
1✔
114
    return adapter
1✔
115

116

117
@inherits_docs
1✔
118
def queryMultiAdapter(objects, interface=Interface, name='', default=None,
1✔
119
                      context=None):
120
    try:
1✔
121
        sitemanager = getSiteManager(context)
1✔
122
    except ComponentLookupError:
1✔
123
        # Oh blast, no site manager. This should *never* happen!
124
        return default
1✔
125

126
    return sitemanager.queryMultiAdapter(objects, interface, name, default)
1✔
127

128

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

138

139
@inherits_docs
1✔
140
def subscribers(objects, interface, context=None):
1✔
141
    try:
1✔
142
        sitemanager = getSiteManager(context)
1✔
143
    except ComponentLookupError:
1✔
144
        # Oh blast, no site manager. This should *never* happen!
145
        return []
1✔
146
    return sitemanager.subscribers(objects, interface)
1✔
147

148

149
@inherits_docs
1✔
150
def handle(*objects):
1✔
151
    getSiteManager(None).subscribers(objects, None)
1✔
152

153
#############################################################################
154
# Register the component architectures adapter hook, with the adapter hook
155
# registry of the `zope.inteface` package. This way we will be able to call
156
# interfaces to create adapters for objects. For example, `I1(ob)` is
157
# equvalent to `getAdapterInContext(I1, ob, '')`.
158

159

160
@hookable
1✔
161
def adapter_hook(interface, object, name='', default=None):
1✔
162
    try:
1✔
163
        sitemanager = getSiteManager()
1✔
164
    except ComponentLookupError:  # pragma: no cover w/o context, cannot test
165
        # Oh blast, no site manager. This should *never* happen!
166
        return None
167
    return sitemanager.queryAdapter(object, interface, name, default)
1✔
168

169

170
zope.interface.interface.adapter_hooks.append(adapter_hook)
1✔
171
#############################################################################
172

173

174
# Utility API
175
@inherits_docs
1✔
176
def getUtility(interface, name='', context=None):
1✔
177
    utility = queryUtility(interface, name, context=context)
1✔
178
    if utility is not None:
1✔
179
        return utility
1✔
180
    raise ComponentLookupError(interface, name)
1✔
181

182

183
@inherits_docs
1✔
184
def queryUtility(interface, name='', default=None, context=None):
1✔
185
    return getSiteManager(context).queryUtility(interface, name, default)
1✔
186

187

188
@inherits_docs
1✔
189
def getUtilitiesFor(interface, context=None):
1✔
190
    return getSiteManager(context).getUtilitiesFor(interface)
1✔
191

192

193
@inherits_docs
1✔
194
def getAllUtilitiesRegisteredFor(interface, context=None):
1✔
195
    return getSiteManager(context).getAllUtilitiesRegisteredFor(interface)
1✔
196

197

198
_marker = object()
1✔
199

200

201
@inherits_docs
1✔
202
def queryNextUtility(context, interface, name='', default=None):
1✔
203
    """Query for the next available utility.
204

205
    Find the next available utility providing `interface` and having the
206
    specified name. If no utility was found, return the specified `default`
207
    value.
208
    """
209
    try:
1✔
210
        sm = getSiteManager(context)
1✔
211
    except ComponentLookupError:
1✔
212
        return default
1✔
213
    bases = sm.__bases__
1✔
214
    for base in bases:
1✔
215
        util = base.queryUtility(interface, name, _marker)
1✔
216
        if util is not _marker:
1✔
217
            return util
1✔
218
    return default
1✔
219

220

221
@inherits_docs
1✔
222
def getNextUtility(context, interface, name=''):
1✔
223
    """Get the next available utility.
224

225
    If no utility was found, a `ComponentLookupError` is raised.
226
    """
227
    util = queryNextUtility(context, interface, name, _marker)
1✔
228
    if util is _marker:
1✔
229
        raise ComponentLookupError(
1✔
230
            "No more utilities for {}, '{}' have been found.".format(
231
                interface, name))
232
    return util
1✔
233

234

235
# Factories
236

237
@inherits_docs
1✔
238
def createObject(__factory_name, *args, **kwargs):
1✔
239
    """Invoke the named factory and return the result.
240

241
    ``__factory_name`` is a positional-only argument.
242
    """
243
    context = kwargs.pop('context', None)
1✔
244
    return getUtility(IFactory, __factory_name, context)(*args, **kwargs)
1✔
245

246

247
@inherits_docs
1✔
248
def getFactoryInterfaces(name, context=None):
1✔
249
    """Return the interface provided by the named factory's objects
250

251
    Result might be a single interface. XXX
252
    """
253
    return getUtility(IFactory, name, context).getInterfaces()
1✔
254

255

256
@inherits_docs
1✔
257
def getFactoriesFor(interface, context=None):
1✔
258
    """Return info on all factories implementing the given interface.
259
    """
260
    utils = getSiteManager(context)
1✔
261
    for (name, factory) in utils.getUtilitiesFor(IFactory):
1✔
262
        interfaces = factory.getInterfaces()
1✔
263
        try:
1✔
264
            if interfaces.isOrExtends(interface):
1!
265
                yield name, factory
1✔
266
        except AttributeError:
1✔
267
            for iface in interfaces:
1!
268
                if iface.isOrExtends(interface):
1✔
269
                    yield name, factory
1✔
270
                    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