Skip to content

Commit

Permalink
Revert to old behavior of Python expression; it is now again a TALES …
Browse files Browse the repository at this point in the history
…expression (such that the pipe operator works as try-except).
  • Loading branch information
malthe committed Mar 2, 2011
1 parent 39fe250 commit faeb71c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
11 changes: 11 additions & 0 deletions CHANGES.rst
@@ -1,6 +1,17 @@
Changes
=======

Features:

- Python expression is now a TALES expression. That means that the
pipe operator can be used to chain two or more expressions in a
try-except sequence.

This behavior was ported from the 1.x series. Note that while it's
still possible to use the pipe character ("|") in an expression, it
must now be escaped.

2.0-rc3 (2011-03-02)
--------------------

Expand Down
23 changes: 16 additions & 7 deletions src/chameleon/tales.py
Expand Up @@ -213,20 +213,29 @@ def translate(self, expression, target):
"can be devised.")


class PythonExpr(object):
class PythonExpr(TalesExpr):
"""Python expression compiler.
>>> test(PythonExpr('2+2'))
>>> test(PythonExpr('2 + 2'))
4
The Python expression is a TALES expression. That means we can use
the pipe operator:
>>> test(PythonExpr('foo | 2 + 2 | 5'))
4
To include a pipe character, use a backslash escape sequence:
>>> test(PythonExpr('\"\|\"'))
'|'
"""

transform = ItemLookupOnAttributeErrorVisitor(transform_attribute)

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

def __call__(self, target, engine):
string = self.expression.strip().replace('\n', ' ')
def translate(self, expression, target):
string = expression.strip().replace('\n', ' ')
decoded = decode_htmlentities(string)

try:
Expand Down

0 comments on commit faeb71c

Please sign in to comment.