• 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

100.0
/src/zope/component/_declaration.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
"""Adapter declarations
1✔
15
"""
16
import sys
1✔
17

18

19
class adapter:
1✔
20
    """
21
    Decorator that declares that the decorated object adapts the given
22
    *interfaces*.
23

24
    This is commonly used in conjunction with :obj:`zope.interface.implementer`
25
    to declare what adapting the *interfaces* will provide.
26
    """
27

28
    def __init__(self, *interfaces):
1✔
29
        self.interfaces = interfaces
1✔
30

31
    def __call__(self, ob):
1✔
32
        if isinstance(ob, type):
1✔
33
            ob.__component_adapts__ = _adapts_descr(self.interfaces)
1✔
34
        else:
35
            ob.__component_adapts__ = self.interfaces
1✔
36

37
        return ob
1✔
38

39

40
def adapts(*interfaces):
1✔
41
    frame = sys._getframe(1)
1✔
42
    locals = frame.f_locals
1✔
43

44
    # Ensure we were called from a class def.
45
    if locals is frame.f_globals or '__module__' not in locals:
1✔
46
        raise TypeError("adapts can be used only from a class definition.")
1✔
47

48
    if '__component_adapts__' in locals:
1✔
49
        raise TypeError("adapts can be used only once in a class definition.")
1✔
50

51
    locals['__component_adapts__'] = _adapts_descr(interfaces)
1✔
52

53

54
def adaptedBy(ob):
1✔
55
    """
56
    Return the *interfaces* that *ob* will adapt, as declared by
57
    :obj:`adapter`.
58
    """
59
    return getattr(ob, '__component_adapts__', None)
1✔
60

61

62
def getName(ob):
1✔
63
    return getattr(ob, '__component_name__', '')
1✔
64

65

66
class _adapts_descr:
1✔
67
    def __init__(self, interfaces):
1✔
68
        self.interfaces = interfaces
1✔
69

70
    def __get__(self, inst, cls):
1✔
71
        if inst is None:
1✔
72
            return self.interfaces
1✔
73
        raise AttributeError('__component_adapts__')
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