Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install plone.transformchain

How to install plone.transformchain

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install plone.transformchain
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.0.2
1.0.3Never BuiltWhy not?
1.0.2 Available View build log
1.0 Available View build log
1.0b1 Available View build log
Windows (64-bit)
1.0.2
1.0.3Never BuiltWhy not?
1.0.2 Available View build log
1.0 Available View build log
1.0b1 Available View build log
Mac OS X (10.5+)
1.0.2
1.0.3Never BuiltWhy not?
1.0.2 Available View build log
1.0 Available View build log
1.0b1 Available View build log
Linux (32-bit)
1.0.2
1.0.3Never BuiltWhy not?
1.0.2 Available View build log
1.0 Available View build log
1.0b1 Available View build log
Linux (64-bit)
1.0.3 Available View build log
1.0.2 Available View build log
1.0 Available View build log
1.0b1 Available View build log
 
Author
License
BSD
Lastest release
version 1.0.3 on Jan 9th, 2014

Introduction

This package provides a means of modifying the response from a page published with repoze.zope2 or the "classic" ZPublisher before it is returned to the browser.

Register a uniquely named adapter from (published, request) providing the ITransform interface. published is the published object, e.g. a view; request is the current request.

The order of the transforms can be maintained using the order property of the adapter.

One of three methods will be called, depending on what type of input was obtained from the publisher and/or the previous method.

  • transformBytes() is called if the input is a str (bytes) object
  • transformUnicode() is called if the input is a unicode object
  • transformIterable() is called if the input is another type of iterable

Each stage can return a byte string, a unicode string, or an iterable.

Most transformers will have a "natural" representation of the result, and will implement the respective method to return another value of the same representation, e.g. implement transformUnicode() to transform and return a unicode object. The other methods may then either be implemented to return None (do nothing) or convert the value to the appropriate type.

The first transformer in the chain is likely to get:

  • A byte string if the transformer is running under the standard Zope 2 ZPublisher.
  • An iterable if the transformer is running under repoze.zope2 or another WSGI pipeline.

Check self.request.response.getHeader('content-type') to see the type of result. The iterable, when unwound, will conform to this type, e.g. for text/html, ''.join(result) should be an HTML string.

The return value is passed to the next transform in the chain. The final transform should return a unicode string, an encoded string, or an iterable.

If a byte string or unicode string is returned by the last transform in the chain, the Content-Length header will be automatically updated

Return None to signal that the result should not be changed from the previous transform.

Here is an example that uppercases everything:

from zope.interface import implements, Interface
from zope.component import adapts

from plone.transformchain.interfaces import ITransform

class UpperTransform(object):
    implements(ITransform)
    adapts(Interface, Interface) # any context, any request

    order = 1000

    def __init__(self, published, request):
        self.published = published
        self.request = request

    def transformBytes(self, result, encoding):
        return result.upper()

    def transformUnicode(self, result, encoding):
        return result.upper()

    def transformIterable(self, result, encoding):
        return [s.upper() for s in result]

You could register this in ZCML like so:

<adapter factory=".transforms.UpperTransform" name="example.uppertransform" />

If you need to turn off transformations for a particular request, you can set a key in request.environ:

request.environ['plone.transformchain.disable'] = True

This will leave the response untouched and will not invoke any ITransform adapters at all.

Changelog

1.0.3 (2013-01-13)
  • There was a problem with the charset regular expression, it expected one space, and only one, between mimetype and charset. So a valid values like "text/html;charset=utf-8" didn't match and default_encoding was returned. We fixed it by allowing any number of spaces (including zero). [jpgimenez]
1.0.2 (2012-01-26)
  • Fix packaging error. [esteele]
1.0.1 (2012-01-26)
  • Compute error_status and store it on request. Work around bug with Zope 2.13 publication events : response.status is not set when IPubBeforeAbort is notified. [gotcha]
  • Don't transform FTP requests [rochecompaan]
1.0 - 2011-05-13
  • Release 1.0 Final. [esteele]
1.0b1 - 2010-04-21
  • Initial release

Subscribe to package updates

Last updated Jan 9th, 2014

Download Stats

Last month:3

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.