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
189
190
191
192
import os, sys
if __name__ == '__main__':
execfile(os.path.join(sys.path[0], 'framework.py'))
from Testing import ZopeTestCase
from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
from Products.PortalTransforms.utils import TransformException
from Products.PortalTransforms.interfaces import *
from Products.PortalTransforms.chain import chain
import urllib
import time
import re
from zope.interface import implements
class BaseTransform:
def name(self):
return getattr(self, '__name__', self.__class__.__name__)
class HtmlToText(BaseTransform):
implements(itransform)
inputs = ('text/html',)
output = 'text/plain'
def __call__(self, orig, **kwargs):
orig = re.sub('<[^>]*>(?i)(?m)', '', orig)
return urllib.unquote(re.sub('\n+', '\n', orig)).strip()
def convert(self, orig, data, **kwargs):
orig = self.__call__(orig)
data.setData(orig)
return data
class HtmlToTextWithEncoding(HtmlToText):
output_encoding = 'ascii'
class FooToBar(BaseTransform):
implements(itransform)
inputs = ('text/*',)
output = 'text/plain'
def __call__(self, orig, **kwargs):
orig = re.sub('foo', 'bar', orig)
return urllib.unquote(re.sub('\n+', '\n', orig)).strip()
def convert(self, orig, data, **kwargs):
orig = self.__call__(orig)
data.setData(orig)
return data
class TransformNoIO(BaseTransform):
implements(itransform)
class BadTransformMissingImplements(BaseTransform):
#__implements__ = None
inputs = ('text/*',)
output = 'text/plain'
class BadTransformBadMIMEType1(BaseTransform):
implements(itransform)
inputs = ('truc/muche',)
output = 'text/plain'
class BadTransformBadMIMEType2(BaseTransform):
implements(itransform)
inputs = ('text/plain',)
output = 'truc/muche'
class BadTransformNoInput(BaseTransform):
implements(itransform)
inputs = ()
output = 'text/plain'
class BadTransformWildcardOutput(BaseTransform):
implements(itransform)
inputs = ('text/plain',)
output = 'text/*'
class TestEngine(ATSiteTestCase):
def afterSetUp(self):
ATSiteTestCase.afterSetUp(self)
self.engine = self.portal.portal_transforms
self.data = '<b>foo</b>'
def register(self):
#A default set of transforms to prove the interfaces work
self.engine.registerTransform(HtmlToText())
self.engine.registerTransform(FooToBar())
def testRegister(self):
self.register()
def testFailRegister(self):
register = self.engine.registerTransform
self.assertRaises(TransformException, register, TransformNoIO())
self.assertRaises(TransformException, register, BadTransformMissingImplements())
self.assertRaises(TransformException, register, BadTransformBadMIMEType1())
self.assertRaises(TransformException, register, BadTransformBadMIMEType2())
self.assertRaises(TransformException, register, BadTransformNoInput())
self.assertRaises(TransformException, register, BadTransformWildcardOutput())
def testCall(self):
self.register()
data = self.engine('HtmlToText', self.data)
self.failUnlessEqual(data, "foo")
data = self.engine('FooToBar', self.data)
self.failUnlessEqual(data, "<b>bar</b>")
def testConvert(self):
self.register()
data = self.engine.convert('HtmlToText', self.data)
self.failUnlessEqual(data.getData(), "foo")
self.failUnlessEqual(data.getMetadata()['mimetype'], 'text/plain')
self.failUnlessEqual(data.getMetadata().get('encoding'), None)
self.failUnlessEqual(data.name(), "HtmlToText")
self.engine.registerTransform(HtmlToTextWithEncoding())
data = self.engine.convert('HtmlToTextWithEncoding', self.data)
self.failUnlessEqual(data.getMetadata()['mimetype'], 'text/plain')
self.failUnlessEqual(data.getMetadata()['encoding'], 'ascii')
self.failUnlessEqual(data.name(), "HtmlToTextWithEncoding")
def testConvertTo(self):
self.register()
data = self.engine.convertTo('text/plain', self.data, mimetype="text/html")
self.failUnlessEqual(data.getData(), "foo")
self.failUnlessEqual(data.getMetadata()['mimetype'], 'text/plain')
self.failUnlessEqual(data.getMetadata().get('encoding'), None)
self.failUnlessEqual(data.name(), "text/plain")
self.engine.unregisterTransform('HtmlToText')
self.engine.unregisterTransform('FooToBar')
self.engine.registerTransform(HtmlToTextWithEncoding())
data = self.engine.convertTo('text/plain', self.data, mimetype="text/html")
self.failUnlessEqual(data.getMetadata()['mimetype'], 'text/plain')
# HtmlToTextWithEncoding. Now None is the right
#self.failUnlessEqual(data.getMetadata()['encoding'], 'ascii')
# XXX the new algorithm is choosing html_to_text instead of
self.failUnlessEqual(data.getMetadata()['encoding'], None)
self.failUnlessEqual(data.name(), "text/plain")
def testChain(self):
self.register()
hb = chain('hbar')
hb.registerTransform(HtmlToText())
hb.registerTransform(FooToBar())
self.engine.registerTransform(hb)
cache = self.engine.convert('hbar', self.data)
self.failUnlessEqual(cache.getData(), "bar")
self.failUnlessEqual(cache.name(), "hbar")
def testSame(self):
data = "This is a test"
mt = "text/plain"
out = self.engine.convertTo('text/plain', data, mimetype=mt)
self.failUnlessEqual(out.getData(), data)
self.failUnlessEqual(out.getMetadata()['mimetype'], 'text/plain')
def testCache(self):
data = "This is a test"
other_data = 'some different data'
mt = "text/plain"
self.engine.max_sec_in_cache = 20
out = self.engine.convertTo(mt, data, mimetype=mt, object=self)
self.failUnlessEqual(out.getData(), data, out.getData())
out = self.engine.convertTo(mt, other_data, mimetype=mt, object=self)
self.failUnlessEqual(out.getData(), data, out.getData())
self.engine.max_sec_in_cache = -1
out = self.engine.convertTo(mt, data, mimetype=mt, object=self)
self.failUnlessEqual(out.getData(), data, out.getData())
out = self.engine.convertTo(mt, other_data, mimetype=mt, object=self)
self.failUnlessEqual(out.getData(), other_data, out.getData())
def test_suite():
from unittest import TestSuite, makeSuite
suite = TestSuite()
suite.addTest(makeSuite(TestEngine))
return suite
if __name__ == '__main__':
framework()