This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
component.py
286 lines (236 loc) · 9.75 KB
/
component.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Component Inspection Utilities
"""
import base64
import six
import types
import zope.interface.declarations
from zope.component import getGlobalSiteManager
from zope.component.interfaces import IFactory
from zope.component.registry import (
AdapterRegistration,
HandlerRegistration,
SubscriptionRegistration,
UtilityRegistration)
from zope.i18nmessageid import ZopeMessageFactory as _
from zope.interface import Interface
from zope.interface.interface import InterfaceClass
from zope.publisher.interfaces import IRequest
from zope.apidoc._compat import unicode
from zope.apidoc.classregistry import classRegistry
from zope.apidoc.utilities import relativizePath, truncateSysPath
from zope.apidoc.utilities import getPythonPath, isReferencable, renderText
SPECIFIC_INTERFACE_LEVEL = 1
EXTENDED_INTERFACE_LEVEL = 2
GENERIC_INTERFACE_LEVEL = 4
def encodeUtilityName(name):
return base64.urlsafe_b64encode(name.encode('utf-8')).decode()
def _adapterishRegistrations(registry):
for r in registry.registeredAdapters():
yield r
for r in registry.registeredSubscriptionAdapters():
yield r
for r in registry.registeredHandlers():
yield r
def getRequiredAdapters(iface, withViews=False):
"""Get adapter registrations where the specified interface is required."""
gsm = getGlobalSiteManager()
for reg in _adapterishRegistrations(gsm):
# Ignore adapters that have no required interfaces
if len(reg.required) == 0:
continue
# Ignore views
if not withViews and reg.required[-1].isOrExtends(IRequest):
continue
# Only get the adapters for which this interface is required
for required_iface in reg.required:
if iface.isOrExtends(required_iface):
yield reg
def getProvidedAdapters(iface, withViews=False):
"""Get adapter registrations where this interface is provided."""
gsm = getGlobalSiteManager()
for reg in _adapterishRegistrations(gsm):
# Only get adapters
# Ignore adapters that have no required interfaces
if len(reg.required) == 0:
continue
# Ignore views
if not withViews and reg.required[-1] and \
reg.required[-1].isOrExtends(IRequest):
continue
# Only get adapters for which this interface is provided
if reg.provided is None or not reg.provided.isOrExtends(iface):
continue
yield reg
def filterAdapterRegistrations(regs, iface, level=SPECIFIC_INTERFACE_LEVEL):
"""Return only those registrations that match the specifed level"""
for reg in regs:
if level & GENERIC_INTERFACE_LEVEL:
for required_iface in reg.required:
if required_iface in (Interface, None):
yield reg
continue
if level & EXTENDED_INTERFACE_LEVEL:
for required_iface in reg.required:
if required_iface is not Interface and \
iface.extends(required_iface):
yield reg
continue
if level & SPECIFIC_INTERFACE_LEVEL:
for required_iface in reg.required:
if required_iface is iface:
yield reg
continue
def getClasses(iface):
"""Get the classes that implement this interface."""
return classRegistry.getClassesThatImplement(iface)
def getFactories(iface):
"""Return the factory registrations, who will return objects providing this
interface."""
gsm = getGlobalSiteManager()
for reg in gsm.registeredUtilities():
if reg.provided is not IFactory:
continue
interfaces = reg.component.getInterfaces()
try:
if interfaces.isOrExtends(iface):
yield reg
except AttributeError:
for interface in interfaces:
if interface.isOrExtends(iface):
yield reg
break
def getUtilities(iface):
"""Return all utility registrations that provide the interface."""
gsm = getGlobalSiteManager()
for reg in gsm.registeredUtilities():
if reg.provided.isOrExtends(iface):
yield reg
def getRealFactory(factory):
"""Get the real factory.
Sometimes the original factory is masked by functions. If the function
keeps track of the original factory, use it.
"""
# Remove all wrappers until none are found anymore.
while hasattr(factory, 'factory'):
factory = factory.factory
# If we have an instance, return its class
if not hasattr(factory, '__name__'):
return factory.__class__
return factory
def getParserInfoInfoDictionary(info):
"""Return a PT-friendly info dictionary for a parser info object."""
return {'file': relativizePath(info.file),
'url': truncateSysPath(info.file).replace('\\', '/'),
'line': info.line,
'eline': info.eline,
'column': info.column,
'ecolumn': info.ecolumn}
def getInterfaceInfoDictionary(iface):
"""Return a PT-friendly info dictionary for an interface."""
if isinstance(iface, zope.interface.declarations.Implements):
iface = iface.inherit
if iface is None:
return None
return {'module': getattr(iface, '__module__', _('<unknown>')),
'name': getattr(iface, '__name__', _('<unknown>'))}
def getInterfaceInfoDictionary(iface):
"""Return a PT-friendly info dictionary for an interface."""
if isinstance(iface, zope.interface.declarations.Implements):
iface = iface.inherit
if iface is None:
return None
return {'module': getattr(iface, '__module__', _('<unknown>')),
'name': getattr(iface, '__name__', _('<unknown>'))}
def getTypeInfoDictionary(type):
"""Return a PT-friendly info dictionary for a type."""
path = getPythonPath(type)
return {'name': type.__name__,
'module': type.__module__,
'url': isReferencable(path) and path.replace('.', '/') or None}
def getSpecificationInfoDictionary(spec):
"""Return an info dictionary for one specification."""
info = {'isInterface': False, 'isType': False}
if zope.interface.interfaces.IInterface.providedBy(spec):
info.update(getInterfaceInfoDictionary(spec))
info['isInterface'] = True
else:
info.update(getTypeInfoDictionary(spec.inherit))
info['isType'] = True
return info
def getAdapterInfoDictionary(reg):
"""Return a PT-friendly info dictionary for an adapter registration."""
factory = getRealFactory(reg.factory)
path = getPythonPath(factory)
url = None
if isReferencable(path):
url = path.replace('.', '/')
if isinstance(reg.info, (str, unicode)):
doc = reg.info
zcml = None
else:
doc = None
zcml = getParserInfoInfoDictionary(reg.info)
return {
'provided': getInterfaceInfoDictionary(reg.provided),
'required': [getSpecificationInfoDictionary(iface)
for iface in reg.required
if iface is not None],
'name': unicode(getattr(reg, 'name', u'')),
'factory': path,
'factory_url': url,
'doc': doc,
'zcml': zcml}
def getFactoryInfoDictionary(reg):
"""Return a PT-friendly info dictionary for a factory."""
factory = reg.component
callable = factory
# Usually only zope.component.factory.Factory instances have this attribute
if IFactory.providedBy(factory) and hasattr(factory, '_callable'):
callable = factory._callable
elif hasattr(callable, '__class__'):
callable = callable.__class__
path = getPythonPath(callable)
return {'name': unicode(reg.name) or _('<i>no name</i>'),
'title': getattr(factory, 'title', u''),
'description': renderText(getattr(factory, 'description', u''),
module=callable.__module__),
'url': isReferencable(path) and path.replace('.', '/') or None}
def getUtilityInfoDictionary(reg):
"""Return a PT-friendly info dictionary for a factory."""
component = reg.component
# Check whether we have an instance of some custom type or not
# Unfortunately, a lot of utilities have a `__name__` attribute, so we
# cannot simply check for its absence
# TODO: Once we support passive display of instances, this insanity can go
# away.
if not isinstance(component, (types.MethodType, types.FunctionType,
InterfaceClass)+six.class_types):
component = getattr(component, '__class__', component)
path = getPythonPath(component)
# provided interface id
iface_id = '%s.%s' % (reg.provided.__module__, reg.provided.getName())
# Determine the URL
if isinstance(component, InterfaceClass):
url = 'Interface/%s' % path
else:
url = None
if isReferencable(path):
url = 'Code/%s' % path.replace('.', '/')
return {'name': unicode(reg.name) or _('<i>no name</i>'),
'url_name': encodeUtilityName(reg.name or '__noname__'),
'iface_id': iface_id,
'path': path,
'url': url}