summaryrefslogtreecommitdiff
path: root/src/DummyTransaction.py
blob: 9273c8c5c0d57667f3cbd7e2246915f50da7f888 (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
#!/usr/bin/env python
# $Id: DummyTransaction.py,v 1.13 2005/11/13 01:12:13 tavis_rudd Exp $

"""Provides dummy Transaction and Response classes is used by Cheetah in place
of real Webware transactions when the Template obj is not used directly as a
Webware servlet.

Meta-Data
==========
Author: Tavis Rudd <tavis@damnsimple.com>
Version: $Revision: 1.13 $
Start Date: 2001/08/30
Last Revision Date: $Date: 2005/11/13 01:12:13 $
"""
__author__ = "Tavis Rudd <tavis@damnsimple.com>"
__revision__ = "$Revision: 1.13 $"[11:-2]

import types

def flush():
    pass

class DummyResponse:
    
    """A dummy Response class is used by Cheetah in place of real Webware
    Response objects when the Template obj is not used directly as a Webware
    servlet.  """

    
    def __init__(self):
        self._outputChunks = outputChunks = []
        self.write = write = outputChunks.append
        def getvalue(outputChunks=outputChunks):
            return ''.join(outputChunks)
        self.getvalue = getvalue
            
        def writeln(txt):
            write(txt)
            write('\n')
        self.writeln = writeln        
        self.flush = flush

    def writelines(self, *lines):
        ## not used
        [self.writeln(ln) for ln in lines]
        
class DummyTransaction:

    """A dummy Transaction class is used by Cheetah in place of real Webware
    transactions when the Template obj is not used directly as a Webware
    servlet.

    It only provides a response object and method.  All other methods and
    attributes make no sense in this context.
    """
    
    def __init__(self, DummyResponse=DummyResponse):       
        def response(resp=DummyResponse()):
            return resp
        self.response = response

class TransformerResponse(object):
    def __init__(self, *args, **kwargs):
        self._output = []
        self._filter = None

    def write(self, value):
        self._output.append(value)

    def flush(self):
        pass

    def writeln(self, line):
        self.write(line)
        self.write('\n')

    def writelines(self, *lines):
        [self.writeln(line) for line in lines]

    def getvalue(self, **kwargs):
        output = kwargs.get('outputChunks') or self._output
        rc = ''.join(output)
        if self._filter:
            _filter = self._filter
            if isinstance(_filter, types.TypeType):
                _filter = _filter()
            return _filter.filter(rc)
        return rc


class TransformerTransaction(object):
    def __init__(self, *args, **kwargs):
        self._response = None
    def response(self):
        if self._response:
            return self._response
        return TransformerResponse()