""" Compatibility plugin for Pyblosxom versions smaller then 1.2 (as of this writing this means the CVS HEAD version). This let's you use plugins written for Pyblosxom 1.2+ with older versions. (<= 1.1) If you make any changes to this plugin, please send a patch to so I can incorporate them. Thanks! To install: 1) Put compatibility.py in your plugin directory. 2) In config.py add 'compatibility' to py['load_plugins'] Note: This must be the _first_ plugin loaded, or it may not work as expected. Dependencies: Todo: $Id: compatibility.py,v 1.5 2006/03/22 20:51:01 sar Exp $ """ __author__ = "Steven Armstrong " __version__ = "$Revision: 1.5 $ $Date: 2006/03/22 20:51:01 $" __url__ = "http://www.c-area.ch/code/" __description__ = "Compatibility plugin for non-WSGI versions of Pyblosxom" __license__ = "GPL 2+" # Python imports import sys # Pyblosxom imports from Pyblosxom import tools # Variables _stdin = sys.stdin _stdout = sys.stdout def verify_installation(request): config = request.getConfiguration() retval = 1 return retval def _request_read(*len): return _stdin.read(*len) def _request_readline(): return _stdin.readline() def _request_readlines(): return _stdin.readlines() class Response: def __init__(self, request): self._request = request self._out = request.getConfiguration().get('stdoutput', _stdout) self._headers = {} self._headers_sent = False def _check_headers_sent(self): if not self._headers_sent: self.showHeaders() def write(self, data): self._check_headers_sent() self._out.write(data) def writelines(self, data): self._check_headers_sent() self._out.writelines(data) def flush(self): self._out.flush() def addHeader(self, *args): args = list(args) if not len(args) % 2: while args: key = args.pop(0).strip() if key.find(' ') != -1 or key.find(':') != -1: raise ValueError, 'There should be no spaces in header keys' value = args.pop(0).strip() self._headers.update({key: str(value)}) else: raise ValueError, 'Headers recieved are not in the correct form' def showHeaders(self): if not self._headers_sent: self._out.write('\n'.join(['%s: %s' % (x, self._headers[x]) for x in self._headers.keys()])) self._out.write('\n\n') self._headers_sent = True #****************************** # Callbacks #****************************** #def cb_handle(args): def cb_start(args): request = args['request'] http = request.getHttp() if not getattr(request, 'read', None): request.read = _request_read request.readline = _request_readline request.readlines = _request_readlines # is it safe to do this? #sys.stdin = request if not getattr(request, 'getResponse', None): def _getResponse(): if not request._response: request._response = Response(request) return request._response request.getResponse = _getResponse # is it safe to do this? #sys.stdout = request.getResponse() if not getattr(request, 'getForm', None): def _getForm(): return request.getHttp()['form'] request.getForm = _getForm def cb_prepare(args): request = args['request'] response = request.getResponse() renderer = request.getData()['renderer'] setattr(renderer, 'addHeader', response.addHeader) if hasattr(response, 'showHeaders'): setattr(renderer, 'showHeaders', response.showHeaders)