-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
value.py
171 lines (129 loc) · 4.49 KB
/
value.py
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
import logging
from rwproperty import getproperty, setproperty
from zope.interface import implements
from plone.app.textfield.interfaces import IRichTextValue, ITransformer, TransformError
from ZODB.blob import Blob
LOG = logging.getLogger('plone.app.textfield')
class RichTextValue(object):
"""The actual value.
Note that this is not a persistent object, to avoid a separate ZODB object
being loaded. Instead, we store the value and set _p_changed on the
parent (with the _p_jar), to which we keep a reference.
"""
implements(IRichTextValue)
def __init__(self, parent=None, defaultOutputMimeType=None, raw=None,
mimeType=None, encoding='utf-8', readonly=False):
self.__parent__ = parent
self._blob = Blob()
self._defaultOutputMimeType = defaultOutputMimeType
self._set = False
self._mimeType = mimeType
self._output = None
self._encoding = encoding
self._readonly = readonly
if raw:
self.raw = raw
self.modified()
# output: the cached transformed value. Not stored in a BLOB since it
# is probably used on the main view of the object and should thus be
# loaded with the object
@getproperty
def output(self):
return self._output
# the raw value - stored in a BLOB
@getproperty
def raw(self):
value = self.raw_encoded
return value.decode(self._encoding)
@setproperty
def raw(self, value):
assert isinstance(value, unicode)
if self.readonly:
raise TypeError("Value is readonly. Use copy() first.")
fp = self._blob.open('w')
try:
fp.write(value.encode(self._encoding))
self._set = True
finally:
fp.close()
self.update()
self.modified()
# Encoded raw value
@getproperty
def raw_encoded(self):
fp = self._blob.open('r')
try:
return fp.read()
finally:
fp.close()
# the current mime type
@getproperty
def mimeType(self):
return self._mimeType
@setproperty
def mimeType(self, value):
if self.readonly:
raise TypeError("Value is readonly. Use copy() first.")
self._mimeType = value
self.update()
self.modified()
# the default mime type
@getproperty
def defaultOutputMimeType(self):
return self._defaultOutputMimeType
@setproperty
def defaultOutputMimeType(self, value):
if self.readonly:
raise TypeError("Value is readonly. Use copy() first.")
self._defaultOutputMimeType = value
self.update()
self.modified()
# is the object readonly?
@getproperty
def readonly(self):
return self._readonly
@setproperty
def readonly(self, value):
self._readonly = value
self.modified()
# cached output
def update(self):
"""Update the cache
"""
if not self._set:
return
if self.defaultOutputMimeType is None:
return
transformer = ITransformer(self.__parent__, None)
if transformer is None:
return
try:
self._output = transformer(self, self.defaultOutputMimeType)
except TransformError, e:
self._output = None
self.modified()
def modified(self):
"""Notify the parent that the value has been modified
"""
if self.__parent__ is not None:
self.__parent__._p_changed = 1
def copy(self, parent=None, readonly=False):
"""Return a copy of this value, without the given parent.
"""
newvalue = RichTextValue(parent)
newvalue._blob = Blob()
newvalue._defaultOutputMimeType = self.defaultOutputMimeType
newvalue._mimeType = self.mimeType
newvalue._output = self._output
newvalue._encoding = self._encoding
newvalue._readonly = readonly
fp = newvalue._blob.open('w')
try:
fp.write(self.raw_encoded)
finally:
fp.close()
newvalue._set = True
newvalue.modified()
return newvalue
def __repr__(self):
return u"RichTextValue object. (Did you mean <attribute>.raw or <attribute>.output?)"