plone.transformchain


Nameplone.transformchain JSON
Version 3.0.0 PyPI version JSON
download
home_pagehttps://pypi.org/project/plone.transformchain
SummaryHook into repoze.zope2 that allows third party packages to register a sequence of hooks that will be allowed to modify the response before it is returned to the browser
upload_time2023-04-26 22:05:45
maintainer
docs_urlNone
authorMartin Aspeli
requires_python>=3.8
licenseBSD
keywords zope2 repoze transform
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Introduction
============

``plone.transformchain`` provides methods to modify 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.component import adapter
    from zope.interface import implementer
    from zope.interface import Interface
    from plone.transformchain.interfaces import ITransform

    @implementer(ITransform)
    @adapter(Interface, Interface) # any context, any request
    class UpperTransform(object):

        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
=========

.. You should *NOT* be adding new change log entries to this file.
   You should create a file in the news directory instead.
   For helpful instructions, please see:
   https://github.com/plone/plone.releaser/blob/master/ADD-A-NEWS-ITEM.rst

.. towncrier release notes start

3.0.0 (2023-04-27)
------------------

Breaking changes:


- Drop Python 2 and Plone 5.2 compatibility.
  [gforcada] (#6)


Internal:


- Update configuration files.
  [plone devs] (3333c742)


2.0.2 (2020-04-22)
------------------

Bug fixes:


- Minor packaging updates. (#1)


2.0.1 (2018-11-04)
------------------

Bug fixes:

- More py3 test and functionality fixes.
  [pbauer, thet]


2.0.0 (2018-06-20)
------------------

Breaking changes:

- Drop support for Python 2.6.
  [jensens]

New features:

- Make ZServer optional

Bug fixes:

- More fixes for Python 2 / 3 compatibility.
  [pbauer, thet]


1.2.2 (2018-02-11)
------------------

Bug fixes:

- Add Python 2 / 3 compatibility
  [vincero]


1.2.1 (2017-06-28)
------------------

Bug fixes:

- Remove unittest2 dependency
  [kakshay21]


1.2.0 (2016-06-21)
------------------

New:

- Added events to notify before/after all/single transform(s) are executed.
  [jensens]


1.1.0 (2016-02-21)
------------------

New:

- Require Zope2 >= 2.13.23
  [jensens]

Fixes:

- PEP8 et al. use zca decorators, ...
  [jensens]


1.0.4 (2015-05-11)
------------------

- Minor cleanup: whitespace, git ignores, rst.
  [gforcada, rnix, maurits]


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

            

Raw data

            {
    "_id": null,
    "home_page": "https://pypi.org/project/plone.transformchain",
    "name": "plone.transformchain",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "zope2 repoze transform",
    "author": "Martin Aspeli",
    "author_email": "optilude@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b1/57/5b7e0ee4392a2545631795d4d47bb2917444d201a485847be5178f633e34/plone.transformchain-3.0.0.tar.gz",
    "platform": null,
    "description": "Introduction\n============\n\n``plone.transformchain`` provides methods to modify the response from a page published with ``repoze.zope2`` or the \"classic\" ``ZPublisher`` before it is returned to the browser.\n\nRegister a uniquely named adapter from ``(published, request)`` providing the ``ITransform`` interface.\n``published`` is the published object, e.g. a view; ``request`` is the current request.\n\nThe order of the transforms can be maintained using the ``order`` property of the adapter.\n\nOne of three methods will be called, depending on what type of input was obtained from the publisher and/or the previous method.\n\n* ``transformBytes()`` is called if the input is a str (bytes) object\n* ``transformUnicode()`` is called if the input is a unicode object\n* ``transformIterable()`` is called if the input is another type of iterable\n\nEach stage can return a byte string, a unicode string, or an iterable.\n\nMost transformers will have a \"natural\" representation of the result,\nand will implement the respective method to return another value of the same representation,\ne.g. implement transformUnicode() to transform and return a unicode object.\nThe other methods may then either be implemented to return None (do nothing) or convert the value to the appropriate type.\n\nThe first transformer in the chain is likely to get:\n\n* A byte string if the transformer is running under the standard Zope 2 ZPublisher.\n* An iterable if the transformer is running under repoze.zope2 or another WSGI pipeline.\n\nCheck ``self.request.response.getHeader('content-type')`` to see the type of result.\nThe iterable, when unwound, will conform to this type, e.g. for \"text/html\", ``''.join(result)`` should be an HTML string.\n\nThe return value is passed to the next transform in the chain.\nThe final transform should return a unicode string, an encoded string, or an iterable.\n\nIf a byte string or unicode string is returned by the last transform in the chain, the ``Content-Length`` header will be automatically updated.\n\nReturn ``None`` to signal that the result should not be changed from the previous transform.\n\nHere is an example that uppercases everything::\n\n    from zope.component import adapter\n    from zope.interface import implementer\n    from zope.interface import Interface\n    from plone.transformchain.interfaces import ITransform\n\n    @implementer(ITransform)\n    @adapter(Interface, Interface) # any context, any request\n    class UpperTransform(object):\n\n        order = 1000\n\n        def __init__(self, published, request):\n            self.published = published\n            self.request = request\n\n        def transformBytes(self, result, encoding):\n            return result.upper()\n\n        def transformUnicode(self, result, encoding):\n            return result.upper()\n\n        def transformIterable(self, result, encoding):\n            return [s.upper() for s in result]\n\nYou could register this in ZCML like so::\n\n    <adapter factory=\".transforms.UpperTransform\" name=\"example.uppertransform\" />\n\nIf you need to turn off transformations for a particular request,\nyou can set a key in ``request.environ``::\n\n    request.environ['plone.transformchain.disable'] = True\n\nThis will leave the response untouched and will not invoke any ``ITransform`` adapters at all.\n\nChangelog\n=========\n\n.. You should *NOT* be adding new change log entries to this file.\n   You should create a file in the news directory instead.\n   For helpful instructions, please see:\n   https://github.com/plone/plone.releaser/blob/master/ADD-A-NEWS-ITEM.rst\n\n.. towncrier release notes start\n\n3.0.0 (2023-04-27)\n------------------\n\nBreaking changes:\n\n\n- Drop Python 2 and Plone 5.2 compatibility.\n  [gforcada] (#6)\n\n\nInternal:\n\n\n- Update configuration files.\n  [plone devs] (3333c742)\n\n\n2.0.2 (2020-04-22)\n------------------\n\nBug fixes:\n\n\n- Minor packaging updates. (#1)\n\n\n2.0.1 (2018-11-04)\n------------------\n\nBug fixes:\n\n- More py3 test and functionality fixes.\n  [pbauer, thet]\n\n\n2.0.0 (2018-06-20)\n------------------\n\nBreaking changes:\n\n- Drop support for Python 2.6.\n  [jensens]\n\nNew features:\n\n- Make ZServer optional\n\nBug fixes:\n\n- More fixes for Python 2 / 3 compatibility.\n  [pbauer, thet]\n\n\n1.2.2 (2018-02-11)\n------------------\n\nBug fixes:\n\n- Add Python 2 / 3 compatibility\n  [vincero]\n\n\n1.2.1 (2017-06-28)\n------------------\n\nBug fixes:\n\n- Remove unittest2 dependency\n  [kakshay21]\n\n\n1.2.0 (2016-06-21)\n------------------\n\nNew:\n\n- Added events to notify before/after all/single transform(s) are executed.\n  [jensens]\n\n\n1.1.0 (2016-02-21)\n------------------\n\nNew:\n\n- Require Zope2 >= 2.13.23\n  [jensens]\n\nFixes:\n\n- PEP8 et al. use zca decorators, ...\n  [jensens]\n\n\n1.0.4 (2015-05-11)\n------------------\n\n- Minor cleanup: whitespace, git ignores, rst.\n  [gforcada, rnix, maurits]\n\n\n1.0.3 (2013-01-13)\n------------------\n\n- There was a problem with the charset regular expression, it expected one\n  space, and only one, between mimetype and charset. So a valid values like\n  \"text/html;charset=utf-8\" didn't match and default_encoding was returned.\n  We fixed it by allowing any number of spaces (including zero).\n  [jpgimenez]\n\n\n1.0.2 (2012-01-26)\n------------------\n\n- Fix packaging error.\n  [esteele]\n\n\n1.0.1 (2012-01-26)\n------------------\n\n- Compute error_status and store it on request.\n  Work around bug with Zope 2.13 publication events :\n  response.status is not set when IPubBeforeAbort is notified.\n  [gotcha]\n\n- Don't transform FTP requests\n  [rochecompaan]\n\n1.0 - 2011-05-13\n----------------\n\n- Release 1.0 Final.\n  [esteele]\n\n1.0b1 - 2010-04-21\n------------------\n\n- Initial release\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Hook into repoze.zope2 that allows third party packages to register a sequence of hooks that will be allowed to modify the response before it is returned to the browser",
    "version": "3.0.0",
    "split_keywords": [
        "zope2",
        "repoze",
        "transform"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0793fec58e337a3f21bad23b356d8003d9d17b0ae68609b9a582a507f10970cc",
                "md5": "a89ac7b318114ae7e1efe57d69536aa4",
                "sha256": "89346a6febfe25ae60cd573a98e79745b9910df5f16c7d6aac1ccf0c62a68870"
            },
            "downloads": -1,
            "filename": "plone.transformchain-3.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a89ac7b318114ae7e1efe57d69536aa4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 11109,
            "upload_time": "2023-04-26T22:05:42",
            "upload_time_iso_8601": "2023-04-26T22:05:42.776792Z",
            "url": "https://files.pythonhosted.org/packages/07/93/fec58e337a3f21bad23b356d8003d9d17b0ae68609b9a582a507f10970cc/plone.transformchain-3.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b1575b7e0ee4392a2545631795d4d47bb2917444d201a485847be5178f633e34",
                "md5": "1c1d82d46d4f573d9b81d533ddc56c5c",
                "sha256": "b7ae54b7efe956622de2df49d4d4ac80cc5d1727a50eec6b19906ae96ba4c6c5"
            },
            "downloads": -1,
            "filename": "plone.transformchain-3.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "1c1d82d46d4f573d9b81d533ddc56c5c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 13145,
            "upload_time": "2023-04-26T22:05:45",
            "upload_time_iso_8601": "2023-04-26T22:05:45.184527Z",
            "url": "https://files.pythonhosted.org/packages/b1/57/5b7e0ee4392a2545631795d4d47bb2917444d201a485847be5178f633e34/plone.transformchain-3.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-26 22:05:45",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "plone.transformchain"
}
        
Elapsed time: 0.05959s