Re: docs: how to upgrade plugins for 1.2
Steven Armstrong <[email protected]>
| Newsgroups | gmane.comp.web.pyblosxom.devel |
|---|---|
| Message-ID | <[email protected]> |
On 02/10/05 18:59, Steven Armstrong wrote:
> Hi all
>
> Here's a short guide how to upgrade plugins to work with the v1.2 API.
>
> cheers
> Steven
>
>
> With the new Pyblosxom 1.2 release there have been a few API changes.
> The idea behind these changes is to make it possible to run Pyblosxom in
> environments other then cgi, for example mod_python or twisted. Using
> the Pyblosxom wsgi application it should be possible to run Pyblosxom on
> any wsgi compliant application server.
>
> Pyblosxom now offers an abstraction layer to access the underlying
> environment. That is, things like os.environ, sys.stdin and sys.stdout.
> It's importend that you don't access these directly from your plugins as
> with some application servers they are not available or are not used in
> the same way as with CGI.
>
>
> Here's a guide how to upgrade your plugins.
>
> os.environ:
> Pyblosxom allows access to the os.environ dict through its _http dict.
> So instead of using os.environ directly you should use:
> http = request.getHttp()
> path_info = http['HTTP_PATHINFO']
>
>
> sys.stdin:
> The Pyblosxom Request object implements the following methods to access
> the input stream:
> __iter__, next, read, readline, readlines, seek, tell
>
> Usage:
> data = request.read()
> data_part = request.read(1024)
> one_line = request.readline()
> lines = request.readlines()
>
>
> sys.stdout:
> The output stream should be accessed through the Pyblosxom Response
> object. The following methods are implemented:
> __iter__, close, flush, next, read, readline, readlines, seek,
> tell, write, writelines, setStatus, addHeader
>
> Usage:
> response = request.getResponse()
> response.addHeader('Status', '200 Ok')
> response.addHeader('Content-type', 'text/html')
> response.write("Hello World")
> response.writelines(["list", "of", "data"])
>
> The status is stored as response.status, the headers as response.headers.
>
>
> form:
> Instead of using:
> form = request.getHttp()['form']
> use:
> form = request.getForm()
>
> Although this is implemented in a backwards compatible way, so both
> methods will work.
> This allows the form to be created/parsed on demand. So if nobody
> actually uses the form, cgi.FieldStorage will not be called. This
> prevents the input stream from beeing unnecessarily consumed.
>
>
> config.py:
> You should not access the config dict by directly importing and using
> config.py. Instead get it from the request object like:
> config = request.getConfiguration()
>
> This is not strictly necessary but it makes things easier as the
> configuration is accessed in a known/consistent way. It gives Pyblosxom
> the chance to change the config on the fly which can be helpfull for
> development and debugging.
>
Update:
variables in the data dict:
If your plugin stores variables in the data dict be sure to clean them
out again in a cb_end callback.
Explanation:
I've just been fiddling about with this for hours. Annoying stuff this is.
It seems mod_python and twisted store/remember/cache stuff between
requests.
Take the following example:
def cb_prepare(args):
request = args["request"]
data = request.getData()
if len(data["entry_list"]) == 0:
data['my_message'] = "no entries"
elif len(data["entry_list"]) == 1:
data['my_message'] = "single entry"
else:
data['my_message'] = "multiple entries"
Now when I click arround the blog, viewing multiple entries, then a
single entry then again multiple entries. The variable 'my_message' will
change randomly. Doing a page reload on a single entry once shows
"single entry", after the next reload it shows "multiple entries", after
the next reload "single entry" and so on.
As for mod_python I guess this happens cause the different requests get
served by different apache subprocesses and therefore different python
interpreters. Alltough it still doesn't make sense.
As for twisted I have no idea why this happens.
The only workaround I have found is to explicitly delete the
"my_message" variable in a cb_end callback, like:
def cb_end(args):
request = args["request"]
data = request.getData()
if 'my_message' in data:
del data['my_message']
As a new data dict get's created for every request I really don't
understand what's going on here. Totaly weird this is. Anybody got an
idea why this is happening?
cheers
Steven
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click