summaryrefslogtreecommitdiff
path: root/src/zope/i18nmessageid/message.py
blob: f41466482a7e05307ca3ffd796cc35df3e3c8d26 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
##############################################################################
#
# Copyright (c) 2004 Zope Corporation 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.
#
##############################################################################
"""I18n Messages

$Id$
"""
__docformat__ = "reStructuredText"

class Message(unicode):
    """Message (Python implementation)

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

    These are the doc tests from message.txt. Note that we have to create the
    message manually since MessageFactory would return the C implementation.

    >>> from zope.i18nmessageid.message import pyMessage as Message
    >>> robot = Message(u"robot-message", 'futurama', u"${name} is a robot.")

    >>> robot
    u'robot-message'
    >>> isinstance(robot, unicode)
    True

    >>> robot.default
    u'${name} is a robot.'
    >>> robot.mapping

    Only the python implementation has a _readonly attribute
    >>> robot._readonly
    True

    >>> robot.domain = "planetexpress"
    Traceback (most recent call last):
    ...
    TypeError: readonly attribute

    >>> robot.default = u"${name} is not a robot."
    Traceback (most recent call last):
    ...
    TypeError: readonly attribute

    >>> robot.mapping = {u'name': u'Bender'}
    Traceback (most recent call last):
    ...
    TypeError: readonly attribute

    >>> new_robot = Message(robot, mapping={u'name': u'Bender'})
    >>> new_robot
    u'robot-message'
    >>> new_robot.domain
    'futurama'
    >>> new_robot.default
    u'${name} is a robot.'
    >>> new_robot.mapping
    {u'name': u'Bender'}

    >>> callable, args = new_robot.__reduce__()
    >>> callable is Message
    True
    >>> args
    (u'robot-message', 'futurama', u'${name} is a robot.', {u'name': u'Bender'})

    >>> fembot = Message(u'fembot')
    >>> callable, args = fembot.__reduce__()
    >>> callable is Message
    True
    >>> args
    (u'fembot', None, None, None)

    Change classes for pickle tests
    >>> import zope.i18nmessageid.message
    >>> oldMessage = zope.i18nmessageid.message.Message
    >>> zope.i18nmessageid.message.Message = Message

    At first check if pickling and unpicklung from pyMessage to pyMessage works
    >>> from pickle import dumps, loads
    >>> pystate = dumps(new_robot)
    >>> pickle_bot = loads(pystate)
    >>> pickle_bot, pickle_bot.domain, pickle_bot.default, pickle_bot.mapping
    (u'robot-message', 'futurama', u'${name} is a robot.', {u'name': u'Bender'})
    >>> pickle_bot._readonly
    True
    >>> from zope.i18nmessageid.message import pyMessage
    >>> pickle_bot.__reduce__()[0] is pyMessage
    True
    >>> del pickle_bot

    At second check if cMessage is able to load the state of a pyMessage
    >>> from _zope_i18nmessageid_message import Message
    >>> zope.i18nmessageid.message.Message = Message
    >>> c_bot = loads(pystate)
    >>> c_bot, c_bot.domain, c_bot.default, c_bot.mapping
    (u'robot-message', 'futurama', u'${name} is a robot.', {u'name': u'Bender'})
    >>> c_bot._readonly
    Traceback (most recent call last):
    AttributeError: 'zope.i18nmessageid.message.Message' object has no attribute '_readonly'
    >>> from _zope_i18nmessageid_message import Message as cMessage
    >>> c_bot.__reduce__()[0] is cMessage
    True

    At last check if pyMessage can load a state of cMessage
    >>> cstate = dumps(c_bot)
    >>> del c_bot
    >>> from zope.i18nmessageid.message import pyMessage as Message
    >>> zope.i18nmessageid.message.Message = Message
    >>> py_bot = loads(cstate)
    >>> py_bot, py_bot.domain, py_bot.default, py_bot.mapping
    (u'robot-message', 'futurama', u'${name} is a robot.', {u'name': u'Bender'})
    >>> py_bot._readonly
    True
    >>> py_bot.__reduce__()[0] is pyMessage
    True

    Both pickle states should be equal
    >>> pystate == cstate
    True

    Finally restore classes for other unit tests
    >>> zope.i18nmessageid.message.Message = oldMessage
    """

    __slots__ = ('domain', 'default', 'mapping', '_readonly')

    def __new__(cls, ustr, domain=None, default=None, mapping=None):
        self = unicode.__new__(cls, ustr)
        if isinstance(ustr, self.__class__):
            domain = ustr.domain and ustr.domain[:] or domain
            default = ustr.default and ustr.default[:] or default
            mapping = ustr.mapping and ustr.mapping.copy() or mapping
            ustr = unicode(ustr)
        self.domain = domain
        if default is None:
            # MessageID does: self.default = ustr
            self.default = default
        else:
            self.default = unicode(default)
        self.mapping = mapping
        self._readonly = True
        return self

    def __setattr__(self, key, value):
        """Message is immutable

        It cannot be changed once the message id is created.
        """
        if getattr(self, '_readonly', False):
            raise TypeError('readonly attribute')
        else:
            return unicode.__setattr__(self, key, value)

    def __reduce__(self):
        return self.__class__, self.__getstate__()

    def __getstate__(self):
        return unicode(self), self.domain, self.default, self.mapping

# save a copy for the unit tests
pyMessage = Message

try:
    from _zope_i18nmessageid_message import Message
except ImportError:
    pass

class MessageFactory(object):
    """Factory for creating i18n messages."""

    def __init__(self, domain):
        self._domain = domain

    def __call__(self, ustr, default=None, mapping=None):
        return Message(ustr, self._domain, default, mapping)