~ubuntu-branches/ubuntu/feisty/pyblosxom/feisty

« back to all changes in this revision

Viewing changes to libs/renderers/base.py

  • Committer: Bazaar Package Importer
  • Author(s): Charles Majola
  • Date: 2005-02-21 14:38:10 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050221143810-c1r07ibzr5hr863u
Tags: 1.0.0-2ubuntu1
* Rebuild for python2.4
* Fixed patches also 2.3 -> 2.4 
* Standards Version 3.6.1.1 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: shiftwidth=4 tabstop=4 expandtab
2
 
 
3
 
import sys
4
 
 
5
 
class RendererBase:
6
 
    """
7
 
    Basic Renderer:
8
 
        - Pyblosxom Core handles the Input and Process of the system and passes
9
 
          the result of the process to the Renderers for output. All renderers
10
 
          are child classes of RendererBase. RenderBase will contain the public
11
 
          interfaces for all Renderer onject.
12
 
    """
13
 
 
14
 
    def __init__(self, request, stdoutput = sys.stdout):
15
 
        """
16
 
        Constructor: Initializes the Renderer
17
 
 
18
 
        @param request: The L{libs.Request.Request} object
19
 
        @type request: L{libs.Request.Request} object
20
 
        @param out: File like object to print to.
21
 
        @type out: file
22
 
        """
23
 
        self._request = request
24
 
        self._header = {}
25
 
        self._out = stdoutput
26
 
        self._content = None
27
 
        self._needs_content_type = 1
28
 
        self.rendered = None
29
 
 
30
 
 
31
 
    def write(self, data):
32
 
        """
33
 
        Convenience method for programs to use instead of accessing
34
 
        self._out.write()
35
 
 
36
 
        Other classes can override this if there is a unique way to
37
 
        write out data, for example, a two stream output, e.g. one
38
 
        output stream and one output log stream.
39
 
 
40
 
        Another use for this could be a plugin that writes out binary
41
 
        files, but because renderers and other frameworks may probably
42
 
        not want you to write to C{stdout} directly, this method assists
43
 
        you nicely. For example::
44
 
 
45
 
            def cb_start(args):
46
 
                req = args['request']
47
 
                renderer = req['renderer']
48
 
 
49
 
                if reqIsGif and gifFileExists(theGifFile):
50
 
                    # Read the file
51
 
                    data = file(theGifFile).read()
52
 
                    
53
 
                    # Modify header
54
 
                    renderer.addHeader('Content-type', 'image/gif')
55
 
                    renderer.addHeader('Content-Length', len(data))
56
 
                    renderer.showHeader()
57
 
 
58
 
                    # Write to output
59
 
                    renderer.write(data)
60
 
 
61
 
                    # Tell pyblosxom not to render anymore as data is
62
 
                    # processed already
63
 
                    renderer.rendered = 1
64
 
 
65
 
        This simple piece of pseudocode explains what you could do with
66
 
        this method, though I highly don't recommend this, unless
67
 
        pyblosxom is running continuously.
68
 
 
69
 
        @param data: Piece of string you want printed
70
 
        @type data: string
71
 
        """
72
 
        self._out.write(data)
73
 
 
74
 
 
75
 
    def addHeader(self, *args):
76
 
        """
77
 
        Populates the HTTP header with lines of text
78
 
 
79
 
        @param args: Paired list of headers
80
 
        @type args: argument lists
81
 
        @raises ValueError: This happens when the parameters are not correct
82
 
        """
83
 
        args = list(args)
84
 
        if not len(args) % 2:
85
 
            while args:
86
 
                key = args.pop(0).strip()
87
 
                if key.find(' ') != -1 or key.find(':') != -1:
88
 
                    raise ValueError, 'There should be no spaces in header keys'
89
 
                value = args.pop(0).strip()
90
 
                self._header.update({key: value})
91
 
        else:
92
 
            raise ValueError, 'Headers recieved are not in the correct form'
93
 
 
94
 
 
95
 
    def setContent(self, content):
96
 
        """
97
 
        Sets the content
98
 
 
99
 
        @param content: What content are we to show?
100
 
        @type content: C{list} List of entries to process or C{dict} Simple
101
 
            dict containing at least 'title' and 'body'
102
 
        """
103
 
        self._content = content
104
 
 
105
 
    
106
 
    def needsContentType(self, flag):
107
 
        """
108
 
        Use the renderer to determine 'Content-Type: x/x' default is to use the
109
 
        renderer for Content-Type, set flag to None to indicate no Content-Type
110
 
        generation.
111
 
 
112
 
        @param flag: True of false value
113
 
        """
114
 
        self._needs_content_type = flag
115
 
 
116
 
 
117
 
    def showHeaders(self):
118
 
        """
119
 
        Show HTTP Headers. Override this if your renderer uses headers in a
120
 
        different way
121
 
        """
122
 
        self.write('\n'.join(['%s: %s' % (x, self._header[x]) 
123
 
                for x in self._header]))
124
 
        self.write('\n\n')
125
 
 
126
 
 
127
 
    def render(self, header = 1):
128
 
        """
129
 
        Do final rendering.
130
 
 
131
 
        @param header: Do we want to show headers?
132
 
        @type header: boolean
133
 
        """
134
 
        if header:
135
 
            if self._header:
136
 
                self.showHeader()
137
 
            else:
138
 
                self.addHeader('Content-Type', 'text/plain')
139
 
                self.showHeader()
140
 
 
141
 
        if self._content:
142
 
            self.write(self._content)
143
 
        self.rendered = 1
144
 
 
145
 
class Renderer(RendererBase):
146
 
    pass