/usr/share/pyshared/grokcore/component/util.py is in python-grokcore.component 2.5-0ubuntu1.
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 | ##############################################################################
#
# Copyright (c) 2006-2007 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.
#
##############################################################################
"""Grok utility functions.
"""
import types
import zope.component.hooks
from zope.interface.interfaces import IInterface
from zope.interface import alsoProvides
from grokcore.component import directive
def _sort_key(component):
# If components have a grok.order directive, sort by that.
explicit_order, implicit_order = directive.order.bind().get(component)
return (explicit_order,
component.__module__,
implicit_order,
component.__class__.__name__)
def sort_components(components, key=None):
"""Sort a list of components using the information provided by
`grok.order`.
"""
sort_key = _sort_key
if key is not None:
sort_key = lambda item: _sort_key(key(item))
return sorted(components, key=sort_key)
def getSiteManager():
site = zope.component.hooks.getSite()
if site is None:
sm = zope.component.getGlobalSiteManager()
else:
sm = site.getSiteManager()
return sm
def provideUtility(component, provides=None, name=u''):
sm = getSiteManager()
sm.registerUtility(component, provides, name, event=False)
def provideAdapter(factory, adapts=None, provides=None, name=''):
sm = getSiteManager()
sm.registerAdapter(factory, adapts, provides, name, event=False)
def provideSubscriptionAdapter(factory, adapts=None, provides=None):
sm = getSiteManager()
sm.registerSubscriptionAdapter(factory, adapts, provides, event=False)
def provideHandler(factory, adapts=None):
sm = getSiteManager()
sm.registerHandler(factory, adapts, event=False)
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, types.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
sm = getSiteManager()
sm.registerUtility(interface, iface_type, id, info)
|