Subversion Repositories Scribus

Rev

Details | Last modification | View Log | RSS feed

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