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

zopefoundation / grokcore.component / 16098871523

18 Jun 2025 06:24AM UTC coverage: 96.224%. Remained the same
16098871523

push

github

icemac
Back to development: 5.1

113 of 136 branches covered (83.09%)

Branch coverage included in aggregate %.

1416 of 1453 relevant lines covered (97.45%)

0.97 hits per line

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

81.67
/src/grokcore/component/util.py
1
##############################################################################
2
#
3
# Copyright (c) 2006-2007 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
"""Grok utility functions.
15
"""
16
import zope.component.hooks
1✔
17
from zope.interface import alsoProvides
1✔
18
from zope.interface.interfaces import IInterface
1✔
19

20
from grokcore.component import directive
1✔
21
from grokcore.component.compat import class_types
1✔
22

23

24
def _sort_key(component):
1✔
25
    # If components have a grok.order directive, sort by that.
26
    explicit_order, implicit_order = directive.order.bind().get(component)
1✔
27
    return (explicit_order,
1✔
28
            component.__module__,
29
            implicit_order,
30
            component.__class__.__name__)
31

32

33
def sort_components(components, key=None):
1✔
34
    """Sort a list of components using the information provided by
35
    `grok.order`.
36
    """
37
    if key is None:
1✔
38
        sort_key = _sort_key
1✔
39
    else:
40
        def sort_key(item):
1✔
41
            return _sort_key(key(item))
1✔
42

43
    return sorted(components, key=sort_key)
1✔
44

45

46
def getSiteManager():
1✔
47
    site = zope.component.hooks.getSite()
1✔
48
    if site is None:
1!
49
        sm = zope.component.getGlobalSiteManager()
1✔
50
    else:
51
        sm = site.getSiteManager()
×
52
    return sm
1✔
53

54

55
def provideUtility(component, provides=None, name=''):
1✔
56
    sm = getSiteManager()
1✔
57
    sm.registerUtility(component, provides, name, event=False)
1✔
58

59

60
def provideAdapter(factory, adapts=None, provides=None, name=''):
1✔
61
    sm = getSiteManager()
1✔
62
    sm.registerAdapter(factory, adapts, provides, name, event=False)
1✔
63

64

65
def provideSubscriptionAdapter(factory, adapts=None, provides=None):
1✔
66
    sm = getSiteManager()
1✔
67
    sm.registerSubscriptionAdapter(factory, adapts, provides, event=False)
1✔
68

69

70
def provideHandler(factory, adapts=None):
1✔
71
    sm = getSiteManager()
1✔
72
    sm.registerHandler(factory, adapts, event=False)
1✔
73

74

75
def provideInterface(id, interface, iface_type=None, info=''):
1✔
76
    """register Interface with global site manager as utility
77

78
    >>> gsm = zope.component.getGlobalSiteManager()
79

80
    >>> from zope.interface import Interface
81
    >>> from zope.interface.interfaces import IInterface
82
    >>> from zope.component.tests import ITestType
83

84
    >>> class I(Interface):
85
    ...     pass
86
    >>> IInterface.providedBy(I)
87
    True
88
    >>> ITestType.providedBy(I)
89
    False
90
    >>> interfaces = gsm.getUtilitiesFor(ITestType)
91
    >>> list(interfaces)
92
    []
93

94
    # provide first interface type
95
    >>> provideInterface('', I, ITestType)
96
    >>> ITestType.providedBy(I)
97
    True
98
    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
99
    >>> [name for (name, iface) in interfaces]
100
    [u'zope.component.interface.I']
101
    >>> [iface.__name__ for (name, iface) in interfaces]
102
    ['I']
103

104
    # provide second interface type
105
    >>> class IOtherType(IInterface):
106
    ...     pass
107
    >>> provideInterface('', I, IOtherType)
108

109
    >>> ITestType.providedBy(I)
110
    True
111
    >>> IOtherType.providedBy(I)
112
    True
113
    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
114
    >>> [name for (name, iface) in interfaces]
115
    [u'zope.component.interface.I']
116
    >>> interfaces = list(gsm.getUtilitiesFor(IOtherType))
117
    >>> [name for (name, iface) in interfaces]
118
    [u'zope.component.interface.I']
119

120
    >>> class I1(Interface):
121
    ...     pass
122
    >>> provideInterface('', I1)
123
    >>> IInterface.providedBy(I1)
124
    True
125
    >>> ITestType.providedBy(I1)
126
    False
127
    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
128
    >>> [name for (name, iface) in interfaces]
129
    [u'zope.component.interface.I']
130
    >>> [iface.__name__ for (name, iface) in interfaces]
131
    ['I']
132
    """
133
    if not id:
1!
134
        id = f"{interface.__module__}.{interface.__name__}"
1✔
135

136
    if not IInterface.providedBy(interface):
1✔
137
        if not isinstance(interface, class_types):
1!
138
            raise TypeError(id, "is not an interface or class")
×
139
        return
1✔
140

141
    if iface_type is not None:
1!
142
        if not iface_type.extends(IInterface):
×
143
            raise TypeError(iface_type, "is not an interface type")
×
144
        alsoProvides(interface, iface_type)
×
145
    else:
146
        iface_type = IInterface
1✔
147

148
    sm = getSiteManager()
1✔
149
    sm.registerUtility(interface, iface_type, id, info)
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