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

zopefoundation / zope.i18nmessageid / 6447468609

05 Oct 2023 12:04PM UTC coverage: 100.0%. Remained the same
6447468609

push

github

dataflake
- vb [ci skip]

60 of 60 branches covered (100.0%)

Branch coverage included in aggregate %.

245 of 245 relevant lines covered (100.0%)

6.99 hits per line

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

100.0
/src/zope/i18nmessageid/message.py
1
##############################################################################
2
#
3
# Copyright (c) 2004 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
"""I18n Messages and factories.
7✔
15
"""
16

17
__docformat__ = "reStructuredText"
7✔
18
_marker = object()
7✔
19

20

21
class Message(str):
7✔
22
    """Message (Python implementation)
23

24
    This is a string used as a message.  It has a domain attribute that is
25
    its source domain, and a default attribute that is its default text to
26
    display when there is no translation.  domain may be None meaning there is
27
    no translation domain.  default may also be None, in which case the
28
    message id itself implicitly serves as the default text.
29
    """
30

31
    __slots__ = (
7✔
32
        'domain', 'default', 'mapping', '_readonly',
33
        'msgid_plural', 'default_plural', 'number')
34

35
    def __new__(cls, ustr, domain=_marker, default=_marker, mapping=_marker,
7✔
36
                msgid_plural=_marker, default_plural=_marker, number=_marker):
37
        self = str.__new__(cls, ustr)
7✔
38
        if isinstance(ustr, self.__class__):
7✔
39
            self.domain = ustr.domain
7✔
40
            self.default = ustr.default
7✔
41
            self.mapping = ustr.mapping
7✔
42
            self.msgid_plural = ustr.msgid_plural
7✔
43
            self.default_plural = ustr.default_plural
7✔
44
            self.number = ustr.number
7✔
45
        else:
46
            self.domain = None
7✔
47
            self.default = None
7✔
48
            self.mapping = None
7✔
49
            self.msgid_plural = None
7✔
50
            self.default_plural = None
7✔
51
            self.number = None
7✔
52

53
        if domain is not _marker:
7✔
54
            self.domain = domain
7✔
55
        if default is not _marker:
7✔
56
            self.default = default
7✔
57
        if mapping is not _marker:
7✔
58
            self.mapping = mapping
7✔
59
        if msgid_plural is not _marker:
7✔
60
            self.msgid_plural = msgid_plural
7✔
61
        if default_plural is not _marker:
7✔
62
            self.default_plural = default_plural
7✔
63
        if number is not _marker:
7✔
64
            self.number = number
7✔
65

66
        if self.number is not None and not isinstance(
7✔
67
                self.number, (int, float)):
68
            raise TypeError('`number` should be an integer or a float')
7✔
69

70
        self._readonly = True
7✔
71
        return self
7✔
72

73
    def __setattr__(self, key, value):
7✔
74
        """Message is immutable
75

76
        It cannot be changed once the message id is created.
77
        """
78
        if getattr(self, '_readonly', False):
7✔
79
            raise TypeError('readonly attribute')
7✔
80
        else:
81
            return str.__setattr__(self, key, value)
7✔
82

83
    def __getstate__(self):
7✔
84
        return (
7✔
85
            str(self), self.domain, self.default, self.mapping,
86
            self.msgid_plural, self.default_plural, self.number)
87

88
    def __reduce__(self):
7✔
89
        return self.__class__, self.__getstate__()
7✔
90

91

92
# Name the fallback Python implementation to make it easier to test.
93
pyMessage = Message
7✔
94

95

96
try:
7✔
97
    from ._zope_i18nmessageid_message import Message
7✔
98
except ImportError:  # pragma: no cover
99
    pass
100

101

102
class MessageFactory:
7✔
103
    """Factory for creating i18n messages."""
104

105
    def __init__(self, domain):
7✔
106
        self._domain = domain
7✔
107

108
    def __call__(self, ustr, default=None, mapping=None,
7✔
109
                 msgid_plural=None, default_plural=None, number=None):
110
        return Message(ustr, self._domain, default, mapping,
7✔
111
                       msgid_plural, default_plural, number)
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