| 1 |
|
| 2 |
''' |
| 3 |
Provides dummy Transaction and Response classes is used by Cheetah in place |
| 4 |
of real Webware transactions when the Template obj is not used directly as a |
| 5 |
Webware servlet. |
| 6 |
|
| 7 |
Warning: This may be deprecated in the future, please do not rely on any |
| 8 |
specific DummyTransaction or DummyResponse behavior |
| 9 |
''' |
| 10 |
|
| 11 |
import logging |
| 12 |
from .compat import unicode |
| 13 |
|
| 14 |
|
| 15 |
class DummyResponseFailure(Exception): |
| 16 |
pass |
| 17 |
|
| 18 |
|
| 19 |
class DummyResponse(object): |
| 20 |
''' |
| 21 |
A dummy Response class is used by Cheetah in place of real Webware |
| 22 |
Response objects when the Template obj is not used directly as a Webware |
| 23 |
servlet |
| 24 |
''' |
| 25 |
def __init__(self): |
| 26 |
self._outputChunks = [] |
| 27 |
|
| 28 |
def flush(self): |
| 29 |
pass |
| 30 |
|
| 31 |
def safeConvert(self, chunk): |
| 32 |
|
| 33 |
|
| 34 |
if not chunk: |
| 35 |
return u'' |
| 36 |
if isinstance(chunk, unicode): |
| 37 |
return chunk |
| 38 |
try: |
| 39 |
return chunk.decode('utf-8', 'strict') |
| 40 |
except UnicodeDecodeError: |
| 41 |
try: |
| 42 |
return chunk.decode('latin-1', 'strict') |
| 43 |
except UnicodeDecodeError: |
| 44 |
return chunk.decode('ascii', 'ignore') |
| 45 |
except AttributeError: |
| 46 |
return unicode(chunk, errors='ignore') |
| 47 |
return chunk |
| 48 |
|
| 49 |
def write(self, value): |
| 50 |
self._outputChunks.append(value) |
| 51 |
|
| 52 |
def writeln(self, txt): |
| 53 |
self.write(txt) |
| 54 |
self.write('\n') |
| 55 |
|
| 56 |
def getvalue(self, outputChunks=None): |
| 57 |
chunks = outputChunks or self._outputChunks |
| 58 |
try: |
| 59 |
return u''.join(chunks) |
| 60 |
except UnicodeDecodeError: |
| 61 |
logging.debug( |
| 62 |
'Trying to work around a UnicodeDecodeError in getvalue()') |
| 63 |
logging.debug( |
| 64 |
'...perhaps you could fix "%s" while you\'re debugging') |
| 65 |
return ''.join((self.safeConvert(c) for c in chunks)) |
| 66 |
|
| 67 |
def writelines(self, *lines): |
| 68 |
|
| 69 |
[self.writeln(ln) for ln in lines] |
| 70 |
|
| 71 |
|
| 72 |
class DummyTransaction(object): |
| 73 |
''' |
| 74 |
A dummy Transaction class is used by Cheetah in place of real Webware |
| 75 |
transactions when the Template obj is not used directly as a Webware |
| 76 |
servlet. |
| 77 |
|
| 78 |
It only provides a response object and method. All other methods and |
| 79 |
attributes make no sense in this context. |
| 80 |
''' |
| 81 |
def __init__(self, *args, **kwargs): |
| 82 |
self._response = None |
| 83 |
|
| 84 |
def response(self, resp=None): |
| 85 |
if self._response is None: |
| 86 |
self._response = resp or DummyResponse() |
| 87 |
return self._response |
| 88 |
|
| 89 |
|
| 90 |
class TransformerResponse(DummyResponse): |
| 91 |
def __init__(self, *args, **kwargs): |
| 92 |
super(TransformerResponse, self).__init__(*args, **kwargs) |
| 93 |
self._filter = None |
| 94 |
|
| 95 |
def getvalue(self, **kwargs): |
| 96 |
output = super(TransformerResponse, self).getvalue(**kwargs) |
| 97 |
if self._filter: |
| 98 |
_filter = self._filter |
| 99 |
if isinstance(_filter, type): |
| 100 |
_filter = _filter() |
| 101 |
return _filter.filter(output) |
| 102 |
return output |
| 103 |
|
| 104 |
|
| 105 |
class TransformerTransaction(object): |
| 106 |
def __init__(self, *args, **kwargs): |
| 107 |
self._response = None |
| 108 |
|
| 109 |
def response(self): |
| 110 |
if self._response: |
| 111 |
return self._response |
| 112 |
return TransformerResponse() |