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

« back to all changes in this revision

Viewing changes to ReadMeForPlugins.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: tabstop=4 shiftwidth=4 expandtab
2
 
"""
3
 
C{README} for Plugins
4
 
 
5
 
Inside the C{contrib/} directory, you'll see the C{plugins/} directory. To
6
 
install a given plugin, move the plugin file you want from the C{contrib/}
7
 
directory to the C{libs/plugins/} directory of your installation.
8
 
 
9
 
Some plugins take effect immediately, like the C{conditionalhttp.py} and the
10
 
C{statusnotfound.py}. Some requires a little bit more information in using it,
11
 
like files to store data, or some variables to put inside your flavour
12
 
templates.  Do read the plugin file itself to see what extra steps you need 
13
 
to do before installing it.
14
 
 
15
 
Below is a basic documentation for plugin developers and it exposes them
16
 
of what callbacks are available to them and documents on how to use them
17
 
if possible.
18
 
 
19
 
B{The BlosxomRenderer plugin callbacks}
20
 
 
21
 
The L{BlosxomRenderer} plugins supports set of callback functions based on the
22
 
blosxom 2.0 callbacks.  The names arguments are different, but the
23
 
L{BlosxomRenderer} callbacks are called at the same points that the blosxom 2.0
24
 
callbacks are called.
25
 
 
26
 
All of the BlosxomRenderer callbacks take the same three arguments
27
 
 
28
 
The available blosxom renderer callbacks are:
29
 
 
30
 
    - L{cb_head} (corresponds to blosxom 2.0 head)
31
 
    - L{cb_date_head} (corresponds to blosxom 2.0 date)
32
 
    - L{cb_story} (corresponds to blosxom 2.0 story)
33
 
    - L{cb_foot} (corresponds to blosoxm 2.0 foot)
34
 
 
35
 
In PyBlosxom, the functionality some of the blosxom 2.0 callbacks are taken
36
 
care of by callback chains.
37
 
 
38
 
     - The blosxom 2.0 entries callback is handled by L{cb_filelist}
39
 
     - The blosxom 2.0 filter callback is handled by L{cb_prepare}
40
 
     - The blosxom 2.0 sort callback is handled by L{cb_prepare}
41
 
"""
42
 
import libs, os
43
 
from libs.Request import Request
44
 
from libs.renderers.blosxom import BlosxomRenderer
45
 
from libs.entries.base import EntryBase
46
 
from libs.pyblosxom import PyBlosxom
47
 
 
48
 
def cb_prepare(args):
49
 
    """
50
 
    A callback to prepare data before a renderin.
51
 
    
52
 
    This callback is called before we go through the renderer. Arguments
53
 
    contains:
54
 
 
55
 
     - C{'request'} - The L{Request} object at the particular moment
56
 
 
57
 
    Most plugins can use the prepare chain to either transform or add to the
58
 
    L{Request.getData()} dict. Some plugins could also use the C{'entry_list'}
59
 
    list of entries and modify data there.
60
 
 
61
 
    Here's an example of a prepare chain plugin::
62
 
 
63
 
        def cb_prepare(args):
64
 
            \"""
65
 
            This plugin shows the number of entry we are going to print and
66
 
            place the result in $countNoOfEntries
67
 
            \"""
68
 
            request = args['request']
69
 
            data = request.getData()
70
 
            config = request.getConfiguration()
71
 
            # Can anyone say Ternary? :)
72
 
            IF = lambda a,b,c:(a() and [b()] or [c()])[0]
73
 
 
74
 
            num_entry = config['num_entries']
75
 
            entries = len(data['entry_list'])
76
 
 
77
 
            data['countNoOfEntries'] = IF(num_entry > entries, num_entry, entries)
78
 
 
79
 
    @param args: A dict containing a L{Request()} object
80
 
    @type args: dict
81
 
    """
82
 
    pass
83
 
 
84
 
 
85
 
def cb_logrequest(args = {'filename': 'A file', 
86
 
        'return_code': 'A http return code', 'request': Request()}):
87
 
    """
88
 
    This callback is responsible for logging a typical request. 
89
 
    
90
 
    A dict, C{args} is given containing:
91
 
 
92
 
     - C{'filename'} - a filename (typically a base filename)
93
 
     - C{'return_code'} - A HTTP error code (e.g 200, 404, 304)
94
 
     - C{'request'} - a L{Request} object
95
 
 
96
 
    No return is expected from this callback. This is usually called at the
97
 
    last point of rendering
98
 
 
99
 
    A typical contents of args::
100
 
        filename = config.get('logfile', '')
101
 
        {'filename': filename, 
102
 
         'return_code': '200',
103
 
         'request': Request()}
104
 
 
105
 
    @param args: A dict containing the keys request, filename and return_code
106
 
    @type args: dict
107
 
    """
108
 
    pass
109
 
 
110
 
 
111
 
def cb_filestat(args = {'filename': 'A file', 'mtime': os.stat('/')}):
112
 
    """
113
 
    A callback that returns a file C{stat} based on the arguments received. 
114
 
    
115
 
    The args received is a dict containing:
116
 
        
117
 
     - C{'filename'} - a physical file and 
118
 
     - C{'mtime'} - what is returned by C{os.stat} function. 
119
 
 
120
 
    Plugins are supposed to transform the value of mtime if a certain condition
121
 
    is met, according to the plugin. All plugins that registers C{cb_filestat}
122
 
    are given a chance to take a peek at the args.
123
 
 
124
 
    A typical contents of args::
125
 
        filename = '/home/someone/blosxom/cat/file.txt'
126
 
        {'filename': filename, 
127
 
         'mtime': os.stat(filename)}
128
 
 
129
 
    @param args: A dict with two keys, filename and mtime
130
 
    @type args: dict
131
 
    """
132
 
    pass
133
 
 
134
 
 
135
 
def cb_filelist(args = {'request' : Request()}):
136
 
    """
137
 
    A callback to generate a list of L{EntryBase} subclasses. 
138
 
    
139
 
    If C{None} is returned, then the callback chain will try the next plugin in
140
 
    the list.
141
 
 
142
 
    @param args: A dict containing a L{Request()} object
143
 
    @type args: dict
144
 
    @returns: None or list of L{EntryBase}.
145
 
    @rtype: list
146
 
    """
147
 
    pass
148
 
 
149
 
 
150
 
def cb_entryparser(args = {'txt': 'A blosxom text entryparser'}):
151
 
    """
152
 
    A callback that tranforms a dict, containing a list of keys - the extension
153
 
    of files it can take, and a function reference, that accepts two arguments,
154
 
    a filename, and the standard request object.
155
 
 
156
 
    The function is supposed to return a dict, at least containing the key
157
 
    C{'title'} and C{'story'}. Entryparsers can use other callback facilities
158
 
    like L{cb_preformat} and the L{cb_postformat} callbacks. See
159
 
    L{libs.pyblosxom.PyBlosxom.defaultEntryParser} on how to use such facilities.
160
 
 
161
 
    All outputs of entryparsers (and together with preformatters and
162
 
    postformatters) will be cached by the caching mechanisms.
163
 
    
164
 
    Plugins are supposed to add more keys as the extension of the file it can
165
 
    handle. A plugin can also replace the standard txt entryparser if the need
166
 
    be.  All plugins that registers C{cb_filestat} are given a chance to take a
167
 
    peek at the args, append to it, or modify it (not advisable).
168
 
 
169
 
    By default, typical contents of args::
170
 
        {'txt': L{libs.pyblosxom.PyBlosxom.defaultEntryParser}}
171
 
 
172
 
    Here's an example code that reads *.plain files::
173
 
 
174
 
        import os
175
 
        def cb_entryparser(args):
176
 
            \"""
177
 
            Register self as plain file handler
178
 
            \"""
179
 
            args['plain'] = parse
180
 
            return args
181
 
 
182
 
        def parse(filename, request):
183
 
            \"""
184
 
            We just read everything off the file here, using the filename as
185
 
            title
186
 
            \"""
187
 
            entryData = {}
188
 
            entryData['title'] = os.path.basename(filename)
189
 
            entryData['story'] = file(filename).read()
190
 
            return entryData
191
 
 
192
 
    Upon a successful registration, pyblosxom will now read all *.plain and
193
 
    *.txt files from the data directory
194
 
 
195
 
    @param args: A dict that comtains function references to entryparsers
196
 
    @type args: dict
197
 
    """
198
 
    pass
199
 
 
200
 
 
201
 
def cb_preformat(args = 
202
 
        {'parser': 'somepreformatter', 
203
 
         'story': ['The\n','text\n'], 
204
 
         'request': Request()}):
205
 
    """
206
 
    A callback for preformatters.
207
 
    
208
 
    A preformatter is a text transformation tool.  Only one preformatter can
209
 
    run at an entry at a time. In this chain, all preformatters are called
210
 
    until one returns a string and not C{None}.
211
 
 
212
 
    Preformatters should act on the parser, and if it matches what the
213
 
    preformatter can handle it can carry on an deal with the story.
214
 
 
215
 
    C{args} contains:
216
 
 
217
 
     - C{'parser'} - A string that determines whether a preformatter should run
218
 
     - C{'story'} - A list containing lines of text (with '\\n' included)
219
 
     - C{'request'} - a L{Request} object
220
 
 
221
 
    A typical preformat plugin look like::
222
 
 
223
 
        def cb_preformat(args):
224
 
            if args['parser'] == 'linebreaks':
225
 
                return parse(''.join(args['story']))
226
 
 
227
 
        def parse(text):
228
 
            # A preformatter to convert linebreak to its HTML counterpart
229
 
            text = re.sub('\\n\\n+','</p><p>',text)
230
 
            text = re.sub('\\n','<br />',text)
231
 
            return '<p>%s</p>' % text
232
 
 
233
 
    @param args: A dict containing a L{Request()} object, parser identifier and
234
 
            story list of lines
235
 
    @type args: dict
236
 
    @returns: A string containing formatted text
237
 
    @rtype: string
238
 
    """
239
 
    pass
240
 
 
241
 
 
242
 
def cb_postformat(args = {'entry_data': {}, 'request': Request()}):
243
 
    """
244
 
    A callback for postformatters
245
 
 
246
 
    Postformatters are callbacks that may make further modification to the
247
 
    entry text, called after a preformatter, it can also be used for extensive
248
 
    operations on a particular entry, adding extra keys to the given
249
 
    'entry_data' dict. If a cache is used in a particular installation, the
250
 
    resulting data will be saved in the cache, so using this chain may not be
251
 
    useful for dynamic data like comment counts, for example. Acceptable
252
 
    operations includes:
253
 
 
254
 
        - Adding a word count
255
 
        - Using a macro replacement plugin (Radio Userland glossary)
256
 
        - Acronym expansion
257
 
        - A 'more' text processor
258
 
 
259
 
    A typical C{args} contains the following:
260
 
 
261
 
        - C{'entry_data'} - A dict that minimally contains a C{'title'} and a
262
 
              C{'story'}
263
 
        - C{'request'} - A typical L{Request} object
264
 
 
265
 
    @param args: A dict containing a L{Request()} object, and an entry_data dict
266
 
    @type args: dict
267
 
    """
268
 
    pass
269
 
 
270
 
def cb_start(args = {'request': Request()}):
271
 
    """
272
 
    A start up callback for plugins
273
 
    
274
 
    The start callback can be used to perform initialization of a callback.
275
 
    Use this callback for any setup code that your plugin needs, like
276
 
    
277
 
        - reading saved data from a file
278
 
        - checking to make sure configuration variables are set
279
 
    
280
 
    @param args: A dict containing a L{Request()} object
281
 
    @type args: dict
282
 
    """
283
 
    pass
284
 
 
285
 
def cb_end(args = {'request' : Request()}):
286
 
    """
287
 
    A finalization callback for plugins
288
 
    
289
 
    The end callback can be used to perform finalization for a callback.
290
 
    Use the end callback to clean up after your plugin has executed.  This
291
 
    is the place to
292
 
    
293
 
        - save data to a file
294
 
        - clean up any temporary files
295
 
    
296
 
    @param args: A dict containing a L{Request()} object
297
 
    @type args: dict
298
 
    """
299
 
    pass
300
 
 
301
 
 
302
 
def cb_head(args = {'renderer':'The Blosxom renderer', 
303
 
                    'entry':'The entry to render',
304
 
                    'template':'The template to be filled in'}):
305
 
    """
306
 
    A callback that is called before a head flavour template is rendered
307
 
    
308
 
    C{cb_head} is called before the variables in the entry are substituted
309
 
    into the template.  This is the place to modify the head template based
310
 
    on the entry content.  You can also set variables on the entry that will
311
 
    be used by the C{cb_story} or C{cb_foot} templates.  You have access to 
312
 
    all the content variables via entry.
313
 
    
314
 
    Blosxom 2.0 calls this callback 'head'
315
 
 
316
 
    C{args} contains
317
 
    
318
 
      - C{'renderer'} - the L{BlosxomRenderer} that called the callback
319
 
      - C{'entry'} - a L{EntryBase} to be rendered
320
 
      - C{'template'} - a string containing the flavour template to be processed
321
 
 
322
 
    @param args: a dict containing a L{BlosxomRenderer}, L{EntryBase}, and template
323
 
    @type args: dict
324
 
    """
325
 
    pass
326
 
 
327
 
def cb_date_head(args = {'renderer':'The Blosxom renderer', 
328
 
                         'entry':'The entry to render',
329
 
                         'template':'The template to be filled in'}):
330
 
    """
331
 
    A callback that is called before a date_head flavour template is rendered
332
 
    
333
 
    C{cb_head} is called before the variables in the entry are substituted
334
 
    into the template.  This is the place to modify the date_head template 
335
 
    based on the entry content.  You have access to all the content variables 
336
 
    via entry.
337
 
    
338
 
    Blosxom 2.0 calls this callback 'date'
339
 
 
340
 
    C{args} contains
341
 
    
342
 
      - C{'renderer'} - the L{BlosxomRenderer} that called the callback
343
 
      - C{'entry'} - a L{EntryBase} to be rendered
344
 
      - C{'template'} - a string containing the flavour template to be processed
345
 
 
346
 
    @param args: a dict containing a L{BlosxomRenderer}, L{EntryBase}, and template
347
 
    @type args: dict
348
 
    """
349
 
    pass
350
 
 
351
 
def cb_story(args = {'renderer':'The Blosxom renderer', 
352
 
                     'entry':'The entry to render',
353
 
                     'template':'The template to be filled in'}):
354
 
    """
355
 
    A callback that is called before a story flavour template is rendered
356
 
    
357
 
    C{cb_story} is called before the variables in the entry are substituted
358
 
    into the template.  This is the place to modify the story template based
359
 
    on the entry content.  You have access to all the content variables via 
360
 
    entry.
361
 
    
362
 
    Blosxom 2.0 calls this callback 'story'
363
 
 
364
 
    C{args} contains
365
 
    
366
 
      - C{'renderer'} - the L{BlosxomRenderer} that called the callback
367
 
      - C{'entry'} - a L{EntryBase} to be rendered
368
 
      - C{'template'} - a string containing the flavour template to be processed
369
 
 
370
 
    @param args: a dict containing a L{BlosxomRenderer}, L{EntryBase}, and template
371
 
    @type args: dict
372
 
    """
373
 
    pass
374
 
 
375
 
def cb_foot(args = {'renderer':'The Blosxom renderer', 
376
 
                    'entry':'The entry to render',
377
 
                    'template':'The template to be filled in'}):
378
 
    """
379
 
    A callback that is called before a footflavour template is rendered
380
 
    
381
 
    C{cb_foot} is called before the variables in the entry are substituted
382
 
    into the template.  This is the place to modify the foot template based
383
 
    on the entry content.  You have access to all the content variables via 
384
 
    entry.
385
 
    
386
 
    Blosxom 2.0 calls this callback 'foot'
387
 
 
388
 
    C{args} contains
389
 
    
390
 
      - C{'renderer'} - the L{BlosxomRenderer} that called the callback
391
 
      - C{'entry'} - a L{EntryBase} to be rendered
392
 
      - C{'template'} - a string containing the flavour template to be processed
393
 
 
394
 
    @param args: a dict containing a L{BlosxomRenderer}, L{EntryBase}, and template
395
 
    @type args: dict
396
 
    """
397
 
    pass
398