Skip to content

Commit

Permalink
Upgrade to Chameleon 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
malthe committed Sep 21, 2011
1 parent 6f4500a commit 55ef9ea
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 1,004 deletions.
15 changes: 12 additions & 3 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
Changelog
=========

0.5.8 (unreleased)
------------------
1.0 (unreleased)
----------------

- Update implementation to use component-based template engine
configuration, plugging directly into the Zope Toolkit framework.

The package no longer provides template classes, or ZCML directives;
you should import directly from the ZTK codebase.

- Nothing changed yet.
Also, note that the ``PREFER_Z3C_PT`` environment option has been
rendered obsolete; instead, this is now managed via component
configuration.

- Upgrade to Chameleon 2.x.

0.5.7 (2010-11-25)
------------------
Expand Down
47 changes: 9 additions & 38 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,45 +1,16 @@
Overview
========

This package implements a compatibility-layer between the following
Zope Page Template engines:
This package provides a page template engine implementation based on
Chameleon. It plugs into the `zope.pagetemplate
<http://pypi.python.org/pypi/zope.pagetemplate>`_ package and has an
explicit dependency on this package.

* z3c.pt
* zope.pagetemplate
You can use the package to replace Zope's reference template engine
with Chameleon in an application based on the Zope Toolkit.

If the environment-variable ``PREFER_Z3C_PT`` is set to a true value,
the ``z3c.pt`` engine will be used instead of ``zope.pagetemplate``.
Configuration
-------------

Note: This package superseeds and replaces the old z3c.pt.compat,
please use z3c.ptcompat instead.
The package is configured via ZCML.

Usage
-----

When writing new code or modifying existing code, import page template
classes from the ``z3c.ptcompat`` package:

>>> from z3c.ptcompat import PageTemplateFile
>>> from z3c.ptcompat import ViewPageTemplateFile

Two methods are available to bind templates and template macros to a
view:

>>> from z3c.ptcompat import bind_template
>>> from z3c.ptcompat import bind_macro

Both function return render-methods that accept keyword arguments
which will be passed to the template.

>>> render = bind_template(template, view)
>>> render = bind_macro(template, view, request, macro)

Patches
-------

By loading the ``patches`` module, Zope view page template classes
will be monkey-patched to use the ``z3c.pt`` rendering facilities:

<include package="z3c.ptcompat.patches" />

This is an alternative to changing module import locations.
12 changes: 3 additions & 9 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ parts = test
versions = versions

[versions]
z3c.pt = 1.2.1
Chameleon = 1.2.13
z3c.pt = 2.1.4
Chameleon = 2.4.5

[test]
recipe = zc.recipe.testrunner
environment = test-environment
eggs =
z3c.ptcompat [test]
eggs = z3c.ptcompat

[test-environment]
PREFER_Z3C_PT = True
CHAMELEON_DEBUG = True
CHAMELEON_CACHE = False
28 changes: 8 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import os

from setuptools import setup, find_packages
import sys, os


def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()


version = '0.5.8dev'
version = '1.0-dev'

tests_require = [
'lxml',
'z3c.pt',
'zope.app.form',
'zope.app.pagetemplate',
'zope.app.publisher',
'zope.tal',
'zope.testing',
'zope.viewlet',
],


setup(name='z3c.ptcompat',
version=version,
description="Compatibility-layer for Zope Page Template engines.",
description="Zope-compatible page template engine based on Chameleon.",
long_description=(
".. contents::\n\n" +
read('README.txt')
+ "\n\n" +
read('src', 'z3c', 'ptcompat', 'zcml.txt')
+ "\n\n" +
read("CHANGES.txt")
),
classifiers=[
Expand All @@ -50,12 +41,9 @@ def read(*rnames):
zip_safe=False,
install_requires=[
'setuptools',
'z3c.pt >= 2.1',
'zope.testing',
'zope.pagetemplate >= 3.6.2',
],
extras_require = dict(
zpt = ['zope.app.pagetemplate', 'zope.tal'],
z3cpt = ['z3c.pt'],
test = tests_require, # used by buildout.cfg testrunner
),
tests_require = tests_require,
test_suite="z3c.ptcompat.tests.test_doctests.test_suite",
# test_suite="z3c.ptcompat.tests.test_doctests.test_suite",
)
50 changes: 1 addition & 49 deletions src/z3c/ptcompat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,6 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: __init__.py 81270 2007-10-31 14:02:39Z jukart $
"""
__docformat__ = "reStructuredText"

from StringIO import StringIO
import config

if config.PREFER_Z3C_PT:
from z3c.pt.pagetemplate import ViewPageTemplateFile
from z3c.pt.pagetemplate import PageTemplateFile

def bind_template(pt, view):
return pt.bind(view)

def bind_macro(template, view, request, macro):
return template.bind(
view, request=request, macro=macro)

else:
from zope.tal.talinterpreter import TALInterpreter
from zope.pagetemplate.pagetemplatefile import PageTemplateFile
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
from zope.app.pagetemplate.viewpagetemplatefile import BoundPageTemplate as \
bind_template

def bind_macro(template, view, request, macro):
program = template.macros[macro]

def render(content_type=None, **kwargs):
output = StringIO(u'')
namespace = template.pt_getContext(
view, request, options=kwargs)
context = template.pt_getEngineContext(namespace)
TALInterpreter(program, None,
context, output, tal=True, showtal=False,
strictinsert=0, sourceAnnotations=False)()
if not request.response.getHeader("Content-Type"):
request.response.setHeader(
"Content-Type", content_type)
return output.getvalue()

return render

class ViewPageTemplateFile(ViewPageTemplateFile):
"""View page template file."""
__docformat__ = "reStructuredText"

def __new__(cls, *args, **kwargs):
inst = object.__new__(cls)
config.REGISTRY[inst] = (args, kwargs)
return inst
44 changes: 0 additions & 44 deletions src/z3c/ptcompat/config.py

This file was deleted.

7 changes: 7 additions & 0 deletions src/z3c/ptcompat/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<configure xmlns="http://namespaces.zope.org/zope">

<include package="z3c.pt" />
<include package="zope.traversing" />
<utility component=".engine.Program" />

</configure>
48 changes: 48 additions & 0 deletions src/z3c/ptcompat/engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

__docformat__ = "reStructuredText"


from zope.interface import implements
from zope.interface import classProvides
from zope.pagetemplate.interfaces import IPageTemplateEngine
from zope.pagetemplate.interfaces import IPageTemplateProgram

from z3c.pt.pagetemplate import PageTemplate as ChameleonPageTemplate
from chameleon.tal import RepeatDict


class Program(object):
implements(IPageTemplateProgram)
classProvides(IPageTemplateEngine)

def __init__(self, template):
self.template = template

def __call__(self, context, macros, tal=True, **options):
if tal is False:
return self.template.body

context.vars['repeat'] = RepeatDict(context.repeat_vars)

return self.template.render(**context.vars)

@classmethod
def cook(cls, source_file, text, engine, content_type):
template = ChameleonPageTemplate(
text, filename=source_file, keep_body=True,
)

return cls(template), template.macros
Loading

0 comments on commit 55ef9ea

Please sign in to comment.