/usr/share/pyshared/zope/component/interface.py is in python-zope.component 3.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""Interface utility functions
"""
__docformat__ = 'restructuredtext'
from types import ClassType
import zope.component
from zope.component.interfaces import ComponentLookupError
from zope.interface import alsoProvides
from zope.interface.interfaces import IInterface
def provideInterface(id, interface, iface_type=None, info=''):
"""register Interface with global site manager as utility
>>> gsm = zope.component.getGlobalSiteManager()
>>> from zope.interface import Interface
>>> from zope.interface.interfaces import IInterface
>>> from zope.component.tests import ITestType
>>> class I(Interface):
... pass
>>> IInterface.providedBy(I)
True
>>> ITestType.providedBy(I)
False
>>> interfaces = gsm.getUtilitiesFor(ITestType)
>>> list(interfaces)
[]
# provide first interface type
>>> provideInterface('', I, ITestType)
>>> ITestType.providedBy(I)
True
>>> interfaces = list(gsm.getUtilitiesFor(ITestType))
>>> [name for (name, iface) in interfaces]
[u'zope.component.interface.I']
>>> [iface.__name__ for (name, iface) in interfaces]
['I']
# provide second interface type
>>> class IOtherType(IInterface):
... pass
>>> provideInterface('', I, IOtherType)
>>> ITestType.providedBy(I)
True
>>> IOtherType.providedBy(I)
True
>>> interfaces = list(gsm.getUtilitiesFor(ITestType))
>>> [name for (name, iface) in interfaces]
[u'zope.component.interface.I']
>>> interfaces = list(gsm.getUtilitiesFor(IOtherType))
>>> [name for (name, iface) in interfaces]
[u'zope.component.interface.I']
>>> class I1(Interface):
... pass
>>> provideInterface('', I1)
>>> IInterface.providedBy(I1)
True
>>> ITestType.providedBy(I1)
False
>>> interfaces = list(gsm.getUtilitiesFor(ITestType))
>>> [name for (name, iface) in interfaces]
[u'zope.component.interface.I']
>>> [iface.__name__ for (name, iface) in interfaces]
['I']
"""
if not id:
id = "%s.%s" % (interface.__module__, interface.__name__)
if not IInterface.providedBy(interface):
if not isinstance(interface, (type, ClassType)):
raise TypeError(id, "is not an interface or class")
return
if iface_type is not None:
if not iface_type.extends(IInterface):
raise TypeError(iface_type, "is not an interface type")
alsoProvides(interface, iface_type)
else:
iface_type = IInterface
gsm = zope.component.getGlobalSiteManager()
gsm.registerUtility(interface, iface_type, id, info)
def getInterface(context, id):
"""Return interface or raise ComponentLookupError
>>> from zope.interface import Interface
>>> from zope.component.tests import ITestType
>>> class I4(Interface):
... pass
>>> IInterface.providedBy(I4)
True
>>> ITestType.providedBy(I4)
False
>>> getInterface(None, 'zope.component.interface.I4')
Traceback (most recent call last):
...
ComponentLookupError: zope.component.interface.I4
>>> provideInterface('', I4, ITestType)
>>> ITestType.providedBy(I4)
True
>>> iface = queryInterface( """\
""" 'zope.component.interface.I4')
>>> iface.__name__
'I4'
"""
iface = queryInterface(id, None)
if iface is None:
raise ComponentLookupError(id)
return iface
def queryInterface(id, default=None):
"""return interface or ``None``
>>> from zope.interface import Interface
>>> from zope.interface.interfaces import IInterface
>>> from zope.component.tests import ITestType
>>> class I3(Interface):
... pass
>>> IInterface.providedBy(I3)
True
>>> ITestType.providedBy(I3)
False
>>> queryInterface('zope.component.interface.I3')
>>> provideInterface('', I3, ITestType)
>>> ITestType.providedBy(I3)
True
>>> iface = queryInterface('zope.component.interface.I3')
>>> iface.__name__
'I3'
"""
return zope.component.queryUtility(IInterface, id, default)
def searchInterface(context, search_string=None, base=None):
"""Interfaces search
>>> from zope.interface import Interface
>>> from zope.interface.interfaces import IInterface
>>> from zope.component.tests import ITestType
>>> class I5(Interface):
... pass
>>> IInterface.providedBy(I5)
True
>>> ITestType.providedBy(I5)
False
>>> searchInterface(None, 'zope.component.interface.I5')
[]
>>> provideInterface('', I5, ITestType)
>>> ITestType.providedBy(I5)
True
>>> iface = searchInterface(None, 'zope.component.interface.I5')
>>> iface[0].__name__
'I5'
"""
return [iface_util[1] for iface_util in
searchInterfaceUtilities(context, search_string, base)]
def searchInterfaceIds(context, search_string=None, base=None):
"""Interfaces search
>>> from zope.interface import Interface
>>> from zope.interface.interfaces import IInterface
>>> from zope.component.tests import ITestType
>>> class I5(Interface):
... pass
>>> IInterface.providedBy(I5)
True
>>> ITestType.providedBy(I5)
False
>>> searchInterface(None, 'zope.component.interface.I5')
[]
>>> provideInterface('', I5, ITestType)
>>> ITestType.providedBy(I5)
True
>>> iface = searchInterfaceIds(None, 'zope.component.interface.I5')
>>> iface
[u'zope.component.interface.I5']
"""
return [iface_util[0] for iface_util in
searchInterfaceUtilities(context, search_string, base)]
def searchInterfaceUtilities(context, search_string=None, base=None):
gsm = zope.component.getGlobalSiteManager()
iface_utilities = gsm.getUtilitiesFor(IInterface)
if search_string:
search_string = search_string.lower()
iface_utilities = [iface_util for iface_util in iface_utilities
if (getInterfaceAllDocs(iface_util[1]).\
find(search_string) >= 0)]
if base:
res = [iface_util for iface_util in iface_utilities
if iface_util[1].extends(base)]
else:
res = [iface_util for iface_util in iface_utilities]
return res
def getInterfaceAllDocs(interface):
iface_id = '%s.%s' %(interface.__module__, interface.__name__)
docs = [str(iface_id).lower(),
str(interface.__doc__).lower()]
if IInterface.providedBy(interface):
for name in interface:
docs.append(
str(interface.getDescriptionFor(name).__doc__).lower())
return '\n'.join(docs)
def nameToInterface(context, id):
if id == 'None':
return None
iface = getInterface(context, id)
return iface
def interfaceToName(context, interface):
if interface is None:
return 'None'
items = searchInterface(context, base=interface)
ids = [('%s.%s' %(iface.__module__, iface.__name__))
for iface in items
if iface == interface]
if not ids:
# Do not fail badly, instead resort to the standard
# way of getting the interface name, cause not all interfaces
# may be registered as utilities.
return interface.__module__ + '.' + interface.__name__
assert len(ids) == 1, "Ambiguous interface names: %s" % ids
return ids[0]
|