| 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 types |
|---|
| 12 | |
|---|
| 13 | class DummyResponseFailure(Exception): |
|---|
| 14 | pass |
|---|
| 15 | |
|---|
| 16 | class DummyResponse(object): |
|---|
| 17 | ''' |
|---|
| 18 | A dummy Response class is used by Cheetah in place of real Webware |
|---|
| 19 | Response objects when the Template obj is not used directly as a Webware |
|---|
| 20 | servlet |
|---|
| 21 | ''' |
|---|
| 22 | def __init__(self): |
|---|
| 23 | self._outputChunks = [] |
|---|
| 24 | |
|---|
| 25 | def flush(self): |
|---|
| 26 | pass |
|---|
| 27 | |
|---|
| 28 | def write(self, value): |
|---|
| 29 | self._outputChunks.append(value) |
|---|
| 30 | |
|---|
| 31 | def writeln(self, txt): |
|---|
| 32 | write(txt) |
|---|
| 33 | write('\n') |
|---|
| 34 | |
|---|
| 35 | def getvalue(self, outputChunks=None): |
|---|
| 36 | chunks = outputChunks or self._outputChunks |
|---|
| 37 | try: |
|---|
| 38 | return ''.join(chunks) |
|---|
| 39 | except UnicodeDecodeError, ex: |
|---|
| 40 | nonunicode = [c for c in chunks if not isinstance(c, unicode)] |
|---|
| 41 | raise DummyResponseFailure('''Looks like you're trying to mix encoded strings with Unicode strings |
|---|
| 42 | (most likely utf-8 encoded ones) |
|---|
| 43 | |
|---|
| 44 | This can happen if you're using the `EncodeUnicode` filter, or if you're manually |
|---|
| 45 | encoding strings as utf-8 before passing them in on the searchList (possible offenders: |
|---|
| 46 | %s) |
|---|
| 47 | (%s)''' % (nonunicode, ex)) |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | def writelines(self, *lines): |
|---|
| 51 | ## not used |
|---|
| 52 | [self.writeln(ln) for ln in lines] |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | class DummyTransaction(object): |
|---|
| 56 | ''' |
|---|
| 57 | A dummy Transaction class is used by Cheetah in place of real Webware |
|---|
| 58 | transactions when the Template obj is not used directly as a Webware |
|---|
| 59 | servlet. |
|---|
| 60 | |
|---|
| 61 | It only provides a response object and method. All other methods and |
|---|
| 62 | attributes make no sense in this context. |
|---|
| 63 | ''' |
|---|
| 64 | def __init__(self, *args, **kwargs): |
|---|
| 65 | self._response = None |
|---|
| 66 | |
|---|
| 67 | def response(self, resp=None): |
|---|
| 68 | if self._response is None: |
|---|
| 69 | self._response = resp or DummyResponse() |
|---|
| 70 | return self._response |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | class TransformerResponse(DummyResponse): |
|---|
| 74 | def __init__(self, *args, **kwargs): |
|---|
| 75 | super(TransformerResponse, self).__init__(*args, **kwargs) |
|---|
| 76 | self._filter = None |
|---|
| 77 | |
|---|
| 78 | def getvalue(self, **kwargs): |
|---|
| 79 | output = super(TransformerResponse, self).getvalue(**kwargs) |
|---|
| 80 | if self._filter: |
|---|
| 81 | _filter = self._filter |
|---|
| 82 | if isinstance(_filter, types.TypeType): |
|---|
| 83 | _filter = _filter() |
|---|
| 84 | return _filter.filter(output) |
|---|
| 85 | return output |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | class TransformerTransaction(object): |
|---|
| 89 | def __init__(self, *args, **kwargs): |
|---|
| 90 | self._response = None |
|---|
| 91 | def response(self): |
|---|
| 92 | if self._response: |
|---|
| 93 | return self._response |
|---|
| 94 | return TransformerResponse() |
|---|
| 95 | |
|---|