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

zopefoundation / z3c.form / 10347759702

12 Aug 2024 07:34AM UTC coverage: 95.627% (+0.2%) from 95.448%
10347759702

Pull #122

github

icemac
Update Python version support.
Pull Request #122: Update Python version support.

1248 of 1339 branches covered (93.2%)

Branch coverage included in aggregate %.

10 of 17 new or added lines in 10 files covered. (58.82%)

5 existing lines in 2 files now uncovered.

3694 of 3829 relevant lines covered (96.47%)

0.96 hits per line

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

93.33
/src/z3c/form/value.py
1
##############################################################################
2
#
3
# Copyright (c) 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
"""Attribute Value Implementation
15

16
$Id$
17
"""
18
__docformat__ = "reStructuredText"
1✔
19
import zope.component
1✔
20
import zope.interface
1✔
21

22
from z3c.form import interfaces
1✔
23
from z3c.form import util
1✔
24

25

26
@zope.interface.implementer(interfaces.IValue)
1✔
27
class StaticValue:
1✔
28
    """Static value adapter."""
29

30
    def __init__(self, value):
1✔
31
        self.value = value
1✔
32

33
    def get(self):
1✔
34
        return self.value
1✔
35

36
    def __repr__(self):
1✔
NEW
37
        return f'<{self.__class__.__name__} {self.value!r}>'
×
38

39

40
@zope.interface.implementer(interfaces.IValue)
1✔
41
class ComputedValue:
1✔
42
    """Static value adapter."""
43

44
    def __init__(self, func):
1✔
45
        self.func = func
1✔
46

47
    def get(self):
1✔
48
        return self.func(self)
1✔
49

50
    def __repr__(self):
1✔
NEW
51
        return f'<{self.__class__.__name__} {self.get()!r}>'
×
52

53

54
class ValueFactory:
1✔
55
    """Computed value factory."""
56

57
    def __init__(self, value, valueClass, discriminators):
1✔
58
        self.value = value
1✔
59
        self.valueClass = valueClass
1✔
60
        self.discriminators = discriminators
1✔
61

62
    def __call__(self, *args):
1✔
63
        sv = self.valueClass(self.value)
1✔
64
        for name, value in zip(self.discriminators, args):
1✔
65
            setattr(sv, name, value)
1✔
66
        return sv
1✔
67

68

69
class ValueCreator:
1✔
70
    """Base class for value creator"""
71

72
    valueClass = StaticValue
1✔
73

74
    def __init__(self, discriminators):
1✔
75
        self.discriminators = discriminators
1✔
76

77
    def __call__(self, value, **kws):
1✔
78
        # Step 1: Check that the keyword argument names match the
79
        #         discriminators
80
        if set(kws).difference(set(self.discriminators)):
1!
81
            raise ValueError(
×
82
                'One or more keyword arguments did not match the '
83
                'discriminators.')
84
        # Step 2: Create an attribute value factory
85
        factory = ValueFactory(value, self.valueClass, self.discriminators)
1✔
86
        # Step 3: Build the adaptation signature
87
        signature = []
1✔
88
        for disc in self.discriminators:
1✔
89
            spec = util.getSpecification(kws.get(disc))
1✔
90
            signature.append(spec)
1✔
91
        # Step 4: Assert the adaptation signature onto the factory
92
        zope.component.adapter(*signature)(factory)
1✔
93
        zope.interface.implementer(interfaces.IValue)(factory)
1✔
94
        return factory
1✔
95

96

97
class StaticValueCreator(ValueCreator):
1✔
98
    """Creates static value."""
99

100
    valueClass = StaticValue
1✔
101

102

103
class ComputedValueCreator(ValueCreator):
1✔
104
    """Creates computed value."""
105

106
    valueClass = ComputedValue
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